Chromium Code Reviews| 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/string_util.h" | |
| 13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 14 #include "chrome/browser/download/download_file_icon_extractor.h" | 15 #include "chrome/browser/download/download_file_icon_extractor.h" |
| 15 #include "chrome/browser/download/download_service.h" | 16 #include "chrome/browser/download/download_service.h" |
| 16 #include "chrome/browser/download/download_service_factory.h" | 17 #include "chrome/browser/download/download_service_factory.h" |
| 17 #include "chrome/browser/download/download_test_file_activity_observer.h" | 18 #include "chrome/browser/download/download_test_file_activity_observer.h" |
| 18 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 19 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
| 19 #include "chrome/browser/extensions/event_names.h" | 20 #include "chrome/browser/extensions/event_names.h" |
| 20 #include "chrome/browser/extensions/extension_apitest.h" | 21 #include "chrome/browser/extensions/extension_apitest.h" |
| 21 #include "chrome/browser/extensions/extension_function_test_utils.h" | 22 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 23 #include "chrome/browser/extensions/extension_service.h" | |
| 22 #include "chrome/browser/history/download_row.h" | 24 #include "chrome/browser/history/download_row.h" |
| 23 #include "chrome/browser/net/url_request_mock_util.h" | 25 #include "chrome/browser/net/url_request_mock_util.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
| 26 #include "chrome/browser/ui/browser_tabstrip.h" | 28 #include "chrome/browser/ui/browser_tabstrip.h" |
| 27 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
| 28 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 31 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 32 #include "chrome/test/base/ui_test_utils.h" |
| 31 #include "content/public/browser/browser_context.h" | 33 #include "content/public/browser/browser_context.h" |
| 32 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/download_item.h" | 35 #include "content/public/browser/download_item.h" |
| 34 #include "content/public/browser/download_manager.h" | 36 #include "content/public/browser/download_manager.h" |
| 35 #include "content/public/browser/notification_service.h" | 37 #include "content/public/browser/notification_service.h" |
| 36 #include "content/public/browser/storage_partition.h" | 38 #include "content/public/browser/storage_partition.h" |
| 37 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/common/page_transition_types.h" | 40 #include "content/public/common/page_transition_types.h" |
| 39 #include "content/public/test/download_test_observer.h" | 41 #include "content/public/test/download_test_observer.h" |
| 42 #include "content/public/test/test_file_error_injector.h" | |
| 40 #include "content/test/net/url_request_slow_download_job.h" | 43 #include "content/test/net/url_request_slow_download_job.h" |
| 41 #include "net/base/data_url.h" | 44 #include "net/base/data_url.h" |
| 42 #include "net/base/net_util.h" | 45 #include "net/base/net_util.h" |
| 43 #include "net/url_request/url_request.h" | 46 #include "net/url_request/url_request.h" |
| 44 #include "net/url_request/url_request_context.h" | 47 #include "net/url_request/url_request_context.h" |
| 45 #include "net/url_request/url_request_job.h" | 48 #include "net/url_request/url_request_job.h" |
| 46 #include "net/url_request/url_request_job_factory.h" | 49 #include "net/url_request/url_request_job_factory.h" |
| 47 #include "net/url_request/url_request_job_factory_impl.h" | 50 #include "net/url_request/url_request_job_factory_impl.h" |
| 48 #include "webkit/blob/blob_data.h" | 51 #include "webkit/blob/blob_data.h" |
| 49 #include "webkit/blob/blob_storage_controller.h" | 52 #include "webkit/blob/blob_storage_controller.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 args_(base::JSONReader::Read(json_args)), | 99 args_(base::JSONReader::Read(json_args)), |
| 97 caught_(caught) { | 100 caught_(caught) { |
| 98 } | 101 } |
| 99 | 102 |
| 100 const base::Time& caught() { return caught_; } | 103 const base::Time& caught() { return caught_; } |
| 101 | 104 |
| 102 bool Equals(const Event& other) { | 105 bool Equals(const Event& other) { |
| 103 if ((profile_ != other.profile_) || | 106 if ((profile_ != other.profile_) || |
| 104 (event_name_ != other.event_name_)) | 107 (event_name_ != other.event_name_)) |
| 105 return false; | 108 return false; |
| 106 if ((event_name_ == events::kOnDownloadCreated || | 109 if ((StartsWithASCII(event_name_, |
| 107 event_name_ == events::kOnDownloadChanged) && | 110 events::kOnDownloadDeterminingFilename, |
| 111 true) || | |
| 112 (event_name_ == events::kOnDownloadCreated) || | |
| 113 (event_name_ == events::kOnDownloadChanged)) && | |
| 108 args_.get() && | 114 args_.get() && |
| 109 other.args_.get()) { | 115 other.args_.get()) { |
| 110 base::ListValue* left_list = NULL; | 116 base::ListValue* left_list = NULL; |
| 111 base::DictionaryValue* left_dict = NULL; | 117 base::DictionaryValue* left_dict = NULL; |
| 112 base::ListValue* right_list = NULL; | 118 base::ListValue* right_list = NULL; |
| 113 base::DictionaryValue* right_dict = NULL; | 119 base::DictionaryValue* right_dict = NULL; |
| 114 if (!args_->GetAsList(&left_list) || | 120 if (!args_->GetAsList(&left_list) || |
| 115 !other.args_->GetAsList(&right_list) || | 121 !other.args_->GetAsList(&right_list) || |
| 116 !left_list->GetDictionary(0, &left_dict) || | 122 !left_list->GetDictionary(0, &left_dict) || |
| 117 !right_list->GetDictionary(0, &right_dict)) | 123 !right_list->GetDictionary(0, &right_dict)) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 const content::NotificationDetails& details) OVERRIDE { | 169 const content::NotificationDetails& details) OVERRIDE { |
| 164 switch (type) { | 170 switch (type) { |
| 165 case chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT: | 171 case chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT: |
| 166 { | 172 { |
| 167 DownloadsNotificationSource* dns = | 173 DownloadsNotificationSource* dns = |
| 168 content::Source<DownloadsNotificationSource>(source).ptr(); | 174 content::Source<DownloadsNotificationSource>(source).ptr(); |
| 169 Event* new_event = new Event( | 175 Event* new_event = new Event( |
| 170 dns->profile, | 176 dns->profile, |
| 171 dns->event_name, | 177 dns->event_name, |
| 172 *content::Details<std::string>(details).ptr(), base::Time::Now()); | 178 *content::Details<std::string>(details).ptr(), base::Time::Now()); |
| 179 LOG(INFO) << "occam caught " << new_event->Debug(); | |
|
asanka
2013/02/22 18:55:48
I'm guessing these LOG statements will be removed.
benjhayden
2013/02/22 20:43:23
Done.
| |
| 173 events_.push_back(new_event); | 180 events_.push_back(new_event); |
| 174 if (waiting_ && | 181 if (waiting_ && |
| 175 waiting_for_.get() && | 182 waiting_for_.get() && |
| 176 waiting_for_->Equals(*new_event)) { | 183 waiting_for_->Equals(*new_event)) { |
| 177 waiting_ = false; | 184 waiting_ = false; |
| 178 MessageLoopForUI::current()->Quit(); | 185 MessageLoopForUI::current()->Quit(); |
| 179 } | 186 } |
| 180 break; | 187 break; |
| 181 } | 188 } |
| 182 default: | 189 default: |
| 183 NOTREACHED(); | 190 NOTREACHED(); |
| 184 } | 191 } |
| 185 } | 192 } |
| 186 | 193 |
| 187 bool WaitFor(Profile* profile, | 194 bool WaitFor(Profile* profile, |
| 188 const std::string& event_name, | 195 const std::string& event_name, |
| 189 const std::string& json_args) { | 196 const std::string& json_args) { |
| 190 waiting_for_.reset(new Event(profile, event_name, json_args, base::Time())); | 197 waiting_for_.reset(new Event(profile, event_name, json_args, base::Time())); |
| 191 for (std::deque<Event*>::const_iterator iter = events_.begin(); | 198 for (std::deque<Event*>::const_iterator iter = events_.begin(); |
| 192 iter != events_.end(); ++iter) { | 199 iter != events_.end(); ++iter) { |
| 193 if ((*iter)->Equals(*waiting_for_.get())) | 200 if ((*iter)->Equals(*waiting_for_.get())) |
| 194 return true; | 201 return true; |
| 195 } | 202 } |
| 203 LOG(INFO) << "occam waiting " << waiting_for_->Debug(); | |
| 196 waiting_ = true; | 204 waiting_ = true; |
| 197 content::RunMessageLoop(); | 205 content::RunMessageLoop(); |
| 198 bool success = !waiting_; | 206 bool success = !waiting_; |
| 199 if (waiting_) { | 207 if (waiting_) { |
| 200 // Print the events that were caught since the last WaitFor() call to help | 208 // Print the events that were caught since the last WaitFor() call to help |
| 201 // find the erroneous event. | 209 // find the erroneous event. |
| 202 // TODO(benjhayden) Fuzzy-match and highlight the erroneous event. | 210 // TODO(benjhayden) Fuzzy-match and highlight the erroneous event. |
| 203 for (std::deque<Event*>::const_iterator iter = events_.begin(); | 211 for (std::deque<Event*>::const_iterator iter = events_.begin(); |
| 204 iter != events_.end(); ++iter) { | 212 iter != events_.end(); ++iter) { |
| 205 if ((*iter)->caught() > last_wait_) { | 213 if ((*iter)->caught() > last_wait_) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 " \"state\": {" | 314 " \"state\": {" |
| 307 " \"previous\": \"in_progress\"," | 315 " \"previous\": \"in_progress\"," |
| 308 " \"current\": \"interrupted\"}}]", | 316 " \"current\": \"interrupted\"}}]", |
| 309 item->GetId(), | 317 item->GetId(), |
| 310 expected_error)); | 318 expected_error)); |
| 311 } | 319 } |
| 312 | 320 |
| 313 std::string GetExtensionURL() { | 321 std::string GetExtensionURL() { |
| 314 return extension_->url().spec(); | 322 return extension_->url().spec(); |
| 315 } | 323 } |
| 324 std::string GetExtensionId() { | |
| 325 return extension_->id(); | |
| 326 } | |
| 327 void DisableExtension() { | |
| 328 browser()->profile()->GetExtensionService()->DisableExtension( | |
| 329 GetExtensionId(), extensions::Extension::DISABLE_USER_ACTION); | |
| 330 } | |
| 331 void EnableExtension() { | |
| 332 browser()->profile()->GetExtensionService()->EnableExtension( | |
| 333 GetExtensionId()); | |
| 334 } | |
| 316 | 335 |
| 317 std::string GetFilename(const char* path) { | 336 std::string GetFilename(const char* path) { |
| 318 std::string result = | 337 std::string result = |
| 319 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); | 338 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); |
| 320 #if defined(OS_WIN) | 339 #if defined(OS_WIN) |
| 321 for (std::string::size_type next = result.find("\\"); | 340 for (std::string::size_type next = result.find("\\"); |
| 322 next != std::string::npos; | 341 next != std::string::npos; |
| 323 next = result.find("\\", next)) { | 342 next = result.find("\\", next)) { |
| 324 result.replace(next, 1, "\\\\"); | 343 result.replace(next, 1, "\\\\"); |
| 325 next += 2; | 344 next += 2; |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 668 return blob_storage_controller_.get(); | 687 return blob_storage_controller_.get(); |
| 669 } | 688 } |
| 670 | 689 |
| 671 private: | 690 private: |
| 672 net::URLRequestJobFactoryImpl job_factory_; | 691 net::URLRequestJobFactoryImpl job_factory_; |
| 673 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; | 692 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; |
| 674 | 693 |
| 675 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext); | 694 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext); |
| 676 }; | 695 }; |
| 677 | 696 |
| 678 // TODO(benjhayden): Comment. | 697 // Writes an HTML5 file so that it can be downloaded. |
| 679 class HTML5FileWriter { | 698 class HTML5FileWriter { |
| 680 public: | 699 public: |
| 681 HTML5FileWriter( | 700 HTML5FileWriter( |
| 682 Profile* profile, | 701 Profile* profile, |
| 683 const std::string& filename, | 702 const std::string& filename, |
| 684 const std::string& origin, | 703 const std::string& origin, |
| 685 DownloadsEventsListener* events_listener, | 704 DownloadsEventsListener* events_listener, |
| 686 const std::string& payload) | 705 const std::string& payload) |
| 687 : profile_(profile), | 706 : profile_(profile), |
| 688 filename_(filename), | 707 filename_(filename), |
| (...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1455 | 1474 |
| 1456 // Test that we can start a download and that the correct sequence of events is | 1475 // Test that we can start a download and that the correct sequence of events is |
| 1457 // fired for it. | 1476 // fired for it. |
| 1458 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, | 1477 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, |
| 1459 DownloadExtensionTest_Download_Basic) { | 1478 DownloadExtensionTest_Download_Basic) { |
| 1460 LoadExtension("downloads_split"); | 1479 LoadExtension("downloads_split"); |
| 1461 CHECK(StartTestServer()); | 1480 CHECK(StartTestServer()); |
| 1462 std::string download_url = test_server()->GetURL("slow?0").spec(); | 1481 std::string download_url = test_server()->GetURL("slow?0").spec(); |
| 1463 GoOnTheRecord(); | 1482 GoOnTheRecord(); |
| 1464 | 1483 |
| 1484 // Start downloading a file. | |
| 1465 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | 1485 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
| 1466 new DownloadsDownloadFunction(), base::StringPrintf( | 1486 new DownloadsDownloadFunction(), base::StringPrintf( |
| 1467 "[{\"url\": \"%s\"}]", download_url.c_str()))); | 1487 "[{\"url\": \"%s\"}]", download_url.c_str()))); |
| 1468 ASSERT_TRUE(result.get()); | 1488 ASSERT_TRUE(result.get()); |
| 1469 int result_id = -1; | 1489 int result_id = -1; |
| 1470 ASSERT_TRUE(result->GetAsInteger(&result_id)); | 1490 ASSERT_TRUE(result->GetAsInteger(&result_id)); |
| 1471 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | 1491 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); |
| 1472 ASSERT_TRUE(item); | 1492 ASSERT_TRUE(item); |
| 1473 ScopedCancellingItem canceller(item); | 1493 ScopedCancellingItem canceller(item); |
| 1474 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | 1494 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 1495 | 1515 |
| 1496 // Test that we can start a download from an incognito context, and that the | 1516 // Test that we can start a download from an incognito context, and that the |
| 1497 // download knows that it's incognito. | 1517 // download knows that it's incognito. |
| 1498 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, | 1518 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, |
| 1499 DownloadExtensionTest_Download_Incognito) { | 1519 DownloadExtensionTest_Download_Incognito) { |
| 1500 LoadExtension("downloads_split"); | 1520 LoadExtension("downloads_split"); |
| 1501 CHECK(StartTestServer()); | 1521 CHECK(StartTestServer()); |
| 1502 GoOffTheRecord(); | 1522 GoOffTheRecord(); |
| 1503 std::string download_url = test_server()->GetURL("slow?0").spec(); | 1523 std::string download_url = test_server()->GetURL("slow?0").spec(); |
| 1504 | 1524 |
| 1525 // Start downloading a file. | |
| 1505 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | 1526 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
| 1506 new DownloadsDownloadFunction(), base::StringPrintf( | 1527 new DownloadsDownloadFunction(), base::StringPrintf( |
| 1507 "[{\"url\": \"%s\"}]", download_url.c_str()))); | 1528 "[{\"url\": \"%s\"}]", download_url.c_str()))); |
| 1508 ASSERT_TRUE(result.get()); | 1529 ASSERT_TRUE(result.get()); |
| 1509 int result_id = -1; | 1530 int result_id = -1; |
| 1510 ASSERT_TRUE(result->GetAsInteger(&result_id)); | 1531 ASSERT_TRUE(result->GetAsInteger(&result_id)); |
| 1511 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | 1532 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); |
| 1512 ASSERT_TRUE(item); | 1533 ASSERT_TRUE(item); |
| 1513 ScopedCancellingItem canceller(item); | 1534 ScopedCancellingItem canceller(item); |
| 1514 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | 1535 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1683 " \"current\": \"complete\"}}]", | 1704 " \"current\": \"complete\"}}]", |
| 1684 result_id, | 1705 result_id, |
| 1685 GetFilename("slow.txt.crdownload").c_str(), | 1706 GetFilename("slow.txt.crdownload").c_str(), |
| 1686 GetFilename("slow.txt").c_str()))); | 1707 GetFilename("slow.txt").c_str()))); |
| 1687 } | 1708 } |
| 1688 | 1709 |
| 1689 // Valid data URLs are valid URLs. | 1710 // Valid data URLs are valid URLs. |
| 1690 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, | 1711 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, |
| 1691 DownloadExtensionTest_Download_DataURL) { | 1712 DownloadExtensionTest_Download_DataURL) { |
| 1692 LoadExtension("downloads_split"); | 1713 LoadExtension("downloads_split"); |
| 1693 CHECK(StartTestServer()); | |
| 1694 std::string download_url = "data:text/plain,hello"; | 1714 std::string download_url = "data:text/plain,hello"; |
| 1695 GoOnTheRecord(); | 1715 GoOnTheRecord(); |
| 1696 | 1716 |
| 1697 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | 1717 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
| 1698 new DownloadsDownloadFunction(), base::StringPrintf( | 1718 new DownloadsDownloadFunction(), base::StringPrintf( |
| 1699 "[{\"url\": \"%s\"," | 1719 "[{\"url\": \"%s\"," |
| 1700 " \"filename\": \"data.txt\"}]", download_url.c_str()))); | 1720 " \"filename\": \"data.txt\"}]", download_url.c_str()))); |
| 1701 ASSERT_TRUE(result.get()); | 1721 ASSERT_TRUE(result.get()); |
| 1702 int result_id = -1; | 1722 int result_id = -1; |
| 1703 ASSERT_TRUE(result->GetAsInteger(&result_id)); | 1723 ASSERT_TRUE(result->GetAsInteger(&result_id)); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 1731 // Disabled due to crbug.com/175711 | 1751 // Disabled due to crbug.com/175711 |
| 1732 #define MAYBE_DownloadExtensionTest_Download_File \ | 1752 #define MAYBE_DownloadExtensionTest_Download_File \ |
| 1733 DISABLED_DownloadExtensionTest_Download_File | 1753 DISABLED_DownloadExtensionTest_Download_File |
| 1734 #else | 1754 #else |
| 1735 #define MAYBE_DownloadExtensionTest_Download_File \ | 1755 #define MAYBE_DownloadExtensionTest_Download_File \ |
| 1736 DownloadExtensionTest_Download_File | 1756 DownloadExtensionTest_Download_File |
| 1737 #endif | 1757 #endif |
| 1738 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, | 1758 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, |
| 1739 MAYBE_DownloadExtensionTest_Download_File) { | 1759 MAYBE_DownloadExtensionTest_Download_File) { |
| 1740 GoOnTheRecord(); | 1760 GoOnTheRecord(); |
| 1741 CHECK(StartTestServer()); | |
| 1742 LoadExtension("downloads_split"); | 1761 LoadExtension("downloads_split"); |
| 1743 std::string download_url = "file:///"; | 1762 std::string download_url = "file:///"; |
| 1744 #if defined(OS_WIN) | 1763 #if defined(OS_WIN) |
| 1745 download_url += "C:/"; | 1764 download_url += "C:/"; |
| 1746 #endif | 1765 #endif |
| 1747 | 1766 |
| 1748 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | 1767 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
| 1749 new DownloadsDownloadFunction(), base::StringPrintf( | 1768 new DownloadsDownloadFunction(), base::StringPrintf( |
| 1750 "[{\"url\": \"%s\"," | 1769 "[{\"url\": \"%s\"," |
| 1751 " \"filename\": \"file.txt\"}]", download_url.c_str()))); | 1770 " \"filename\": \"file.txt\"}]", download_url.c_str()))); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2138 " \"state\": {" | 2157 " \"state\": {" |
| 2139 " \"previous\": \"in_progress\"," | 2158 " \"previous\": \"in_progress\"," |
| 2140 " \"current\": \"complete\"}}]", | 2159 " \"current\": \"complete\"}}]", |
| 2141 result_id, | 2160 result_id, |
| 2142 GetFilename("on_record.txt.crdownload").c_str(), | 2161 GetFilename("on_record.txt.crdownload").c_str(), |
| 2143 GetFilename("on_record.txt").c_str()))); | 2162 GetFilename("on_record.txt").c_str()))); |
| 2144 std::string disk_data; | 2163 std::string disk_data; |
| 2145 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); | 2164 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); |
| 2146 EXPECT_STREQ(kPayloadData, disk_data.c_str()); | 2165 EXPECT_STREQ(kPayloadData, disk_data.c_str()); |
| 2147 } | 2166 } |
| 2167 | |
| 2168 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, | |
| 2169 DownloadExtensionTest_OnDeterminingFilename_NoChange) { | |
| 2170 GoOnTheRecord(); | |
| 2171 LoadExtension("downloads_split"); | |
| 2172 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2173 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2174 CHECK(StartTestServer()); | |
| 2175 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2176 | |
| 2177 // Start downloading a file. | |
| 2178 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2179 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2180 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2181 ASSERT_TRUE(result.get()); | |
| 2182 int result_id = -1; | |
| 2183 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2184 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2185 ASSERT_TRUE(item); | |
| 2186 ScopedCancellingItem canceller(item); | |
| 2187 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2188 | |
| 2189 // Wait for the onCreated and onDeterminingFilename events. | |
| 2190 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2191 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2192 " \"incognito\": false," | |
| 2193 " \"id\": %d," | |
| 2194 " \"mime\": \"text/plain\"," | |
| 2195 " \"paused\": false," | |
| 2196 " \"url\": \"%s\"}]", | |
| 2197 result_id, | |
| 2198 download_url.c_str()))); | |
| 2199 ASSERT_TRUE(WaitFor( | |
| 2200 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2201 base::StringPrintf("[{\"id\": %d," | |
| 2202 " \"filename\":\"slow.txt\"}]", | |
| 2203 result_id))); | |
| 2204 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2205 ASSERT_TRUE(item->IsInProgress()); | |
| 2206 | |
| 2207 // Respond to the onDeterminingFilename. | |
| 2208 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2209 browser()->profile(), | |
| 2210 false, | |
| 2211 GetExtensionId(), | |
| 2212 42, | |
| 2213 result_id, | |
| 2214 base::FilePath(), | |
| 2215 false)); | |
| 2216 | |
| 2217 // The download should complete successfully. | |
| 2218 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2219 base::StringPrintf("[{\"id\": %d," | |
| 2220 " \"filename\": {" | |
| 2221 " \"previous\": \"%s\"," | |
| 2222 " \"current\": \"%s\"}," | |
| 2223 " \"state\": {" | |
| 2224 " \"previous\": \"in_progress\"," | |
| 2225 " \"current\": \"complete\"}}]", | |
| 2226 result_id, | |
| 2227 GetFilename("slow.txt.crdownload").c_str(), | |
| 2228 GetFilename("slow.txt").c_str()))); | |
| 2229 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2230 base::StringPrintf("[{\"id\": %d," | |
| 2231 " \"state\": {" | |
| 2232 " \"previous\": \"in_progress\"," | |
| 2233 " \"current\": \"complete\"}}]", | |
| 2234 result_id))); | |
| 2235 } | |
| 2236 | |
| 2237 IN_PROC_BROWSER_TEST_F( | |
| 2238 DownloadExtensionTest, | |
| 2239 DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) { | |
| 2240 GoOnTheRecord(); | |
| 2241 LoadExtension("downloads_split"); | |
| 2242 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2243 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2244 CHECK(StartTestServer()); | |
| 2245 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2246 | |
| 2247 // Start downloading a file. | |
| 2248 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2249 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2250 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2251 ASSERT_TRUE(result.get()); | |
| 2252 int result_id = -1; | |
| 2253 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2254 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2255 ASSERT_TRUE(item); | |
| 2256 ScopedCancellingItem canceller(item); | |
| 2257 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2258 | |
| 2259 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2260 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2261 " \"incognito\": false," | |
| 2262 " \"id\": %d," | |
| 2263 " \"mime\": \"text/plain\"," | |
| 2264 " \"paused\": false," | |
| 2265 " \"url\": \"%s\"}]", | |
| 2266 result_id, | |
| 2267 download_url.c_str()))); | |
| 2268 ASSERT_TRUE(WaitFor( | |
| 2269 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2270 base::StringPrintf("[{\"id\": %d," | |
| 2271 " \"filename\":\"slow.txt\"}]", | |
| 2272 result_id))); | |
| 2273 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2274 ASSERT_TRUE(item->IsInProgress()); | |
| 2275 | |
| 2276 // Respond to the onDeterminingFilename. | |
| 2277 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2278 browser()->profile(), | |
| 2279 false, | |
| 2280 GetExtensionId(), | |
| 2281 42, | |
| 2282 result_id, | |
| 2283 base::FilePath(FILE_PATH_LITERAL("overridden.swf")), | |
| 2284 false)); | |
| 2285 | |
| 2286 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2287 base::StringPrintf("[{\"id\": %d," | |
| 2288 " \"danger\": {" | |
| 2289 " \"previous\":\"safe\"," | |
| 2290 " \"current\":\"file\"}," | |
| 2291 " \"dangerAccepted\": {" | |
| 2292 " \"current\":false}}]", | |
| 2293 result_id))); | |
| 2294 | |
| 2295 item->DangerousDownloadValidated(); | |
| 2296 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2297 base::StringPrintf("[{\"id\": %d," | |
| 2298 " \"dangerAccepted\": {" | |
| 2299 " \"previous\":false," | |
| 2300 " \"current\":true}}]", | |
| 2301 result_id))); | |
| 2302 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2303 base::StringPrintf("[{\"id\": %d," | |
| 2304 " \"filename\": {" | |
| 2305 " \"previous\": \"%s\"," | |
| 2306 " \"current\": \"%s\"}," | |
| 2307 " \"state\": {" | |
| 2308 " \"previous\": \"in_progress\"," | |
| 2309 " \"current\": \"complete\"}}]", | |
| 2310 result_id, | |
| 2311 GetFilename("overridden.swf.crdownload").c_str(), | |
| 2312 GetFilename("overridden.swf").c_str()))); | |
| 2313 } | |
| 2314 | |
| 2315 IN_PROC_BROWSER_TEST_F( | |
| 2316 DownloadExtensionTest, | |
| 2317 DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) { | |
| 2318 GoOnTheRecord(); | |
| 2319 LoadExtension("downloads_split"); | |
| 2320 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2321 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2322 CHECK(StartTestServer()); | |
| 2323 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2324 | |
| 2325 // Start downloading a file. | |
| 2326 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2327 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2328 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2329 ASSERT_TRUE(result.get()); | |
| 2330 int result_id = -1; | |
| 2331 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2332 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2333 ASSERT_TRUE(item); | |
| 2334 ScopedCancellingItem canceller(item); | |
| 2335 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2336 | |
| 2337 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2338 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2339 " \"incognito\": false," | |
| 2340 " \"id\": %d," | |
| 2341 " \"mime\": \"text/plain\"," | |
| 2342 " \"paused\": false," | |
| 2343 " \"url\": \"%s\"}]", | |
| 2344 result_id, | |
| 2345 download_url.c_str()))); | |
| 2346 ASSERT_TRUE(WaitFor( | |
| 2347 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2348 base::StringPrintf("[{\"id\": %d," | |
| 2349 " \"filename\":\"slow.txt\"}]", | |
| 2350 result_id))); | |
| 2351 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2352 ASSERT_TRUE(item->IsInProgress()); | |
| 2353 | |
| 2354 // Respond to the onDeterminingFilename. | |
| 2355 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2356 browser()->profile(), | |
| 2357 false, | |
| 2358 GetExtensionId(), | |
| 2359 42, | |
| 2360 result_id, | |
| 2361 base::FilePath(FILE_PATH_LITERAL("sneaky/../../sneaky.txt")), | |
| 2362 false)); | |
| 2363 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2364 base::StringPrintf("[{\"id\": %d," | |
| 2365 " \"filename\": {" | |
| 2366 " \"previous\": \"%s\"," | |
| 2367 " \"current\": \"%s\"}," | |
| 2368 " \"state\": {" | |
| 2369 " \"previous\": \"in_progress\"," | |
| 2370 " \"current\": \"complete\"}}]", | |
| 2371 result_id, | |
| 2372 GetFilename("slow.txt.crdownload").c_str(), | |
| 2373 GetFilename("slow.txt").c_str()))); | |
| 2374 } | |
| 2375 | |
| 2376 IN_PROC_BROWSER_TEST_F( | |
| 2377 DownloadExtensionTest, | |
| 2378 DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) { | |
| 2379 GoOnTheRecord(); | |
| 2380 LoadExtension("downloads_split"); | |
| 2381 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2382 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2383 CHECK(StartTestServer()); | |
| 2384 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2385 | |
| 2386 // Start downloading a file. | |
| 2387 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2388 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2389 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2390 ASSERT_TRUE(result.get()); | |
| 2391 int result_id = -1; | |
| 2392 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2393 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2394 ASSERT_TRUE(item); | |
| 2395 ScopedCancellingItem canceller(item); | |
| 2396 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2397 | |
| 2398 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2399 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2400 " \"incognito\": false," | |
| 2401 " \"id\": %d," | |
| 2402 " \"mime\": \"text/plain\"," | |
| 2403 " \"paused\": false," | |
| 2404 " \"url\": \"%s\"}]", | |
| 2405 result_id, | |
| 2406 download_url.c_str()))); | |
| 2407 ASSERT_TRUE(WaitFor( | |
| 2408 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2409 base::StringPrintf("[{\"id\": %d," | |
| 2410 " \"filename\":\"slow.txt\"}]", | |
| 2411 result_id))); | |
| 2412 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2413 ASSERT_TRUE(item->IsInProgress()); | |
| 2414 | |
| 2415 // Respond to the onDeterminingFilename. | |
| 2416 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2417 browser()->profile(), | |
| 2418 false, | |
| 2419 GetExtensionId(), | |
| 2420 42, | |
| 2421 result_id, | |
| 2422 base::FilePath(FILE_PATH_LITERAL(".")), | |
| 2423 false)); | |
| 2424 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2425 base::StringPrintf("[{\"id\": %d," | |
| 2426 " \"filename\": {" | |
| 2427 " \"previous\": \"%s\"," | |
| 2428 " \"current\": \"%s\"}," | |
| 2429 " \"state\": {" | |
| 2430 " \"previous\": \"in_progress\"," | |
| 2431 " \"current\": \"complete\"}}]", | |
| 2432 result_id, | |
| 2433 GetFilename("slow.txt.crdownload").c_str(), | |
| 2434 GetFilename("slow.txt").c_str()))); | |
| 2435 } | |
| 2436 | |
| 2437 IN_PROC_BROWSER_TEST_F( | |
| 2438 DownloadExtensionTest, | |
| 2439 DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) { | |
| 2440 CHECK(StartTestServer()); | |
| 2441 GoOnTheRecord(); | |
| 2442 LoadExtension("downloads_split"); | |
| 2443 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2444 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2445 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2446 | |
| 2447 // Start downloading a file. | |
| 2448 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2449 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2450 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2451 ASSERT_TRUE(result.get()); | |
| 2452 int result_id = -1; | |
| 2453 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2454 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2455 ASSERT_TRUE(item); | |
| 2456 ScopedCancellingItem canceller(item); | |
| 2457 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2458 | |
| 2459 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2460 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2461 " \"incognito\": false," | |
| 2462 " \"id\": %d," | |
| 2463 " \"mime\": \"text/plain\"," | |
| 2464 " \"paused\": false," | |
| 2465 " \"url\": \"%s\"}]", | |
| 2466 result_id, | |
| 2467 download_url.c_str()))); | |
| 2468 ASSERT_TRUE(WaitFor( | |
| 2469 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2470 base::StringPrintf("[{\"id\": %d," | |
| 2471 " \"filename\":\"slow.txt\"}]", | |
| 2472 result_id))); | |
| 2473 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2474 ASSERT_TRUE(item->IsInProgress()); | |
| 2475 | |
| 2476 // Respond to the onDeterminingFilename. | |
| 2477 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2478 browser()->profile(), | |
| 2479 false, | |
| 2480 GetExtensionId(), | |
| 2481 42, | |
| 2482 result_id, | |
| 2483 base::FilePath(FILE_PATH_LITERAL("..")), | |
| 2484 false)); | |
| 2485 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2486 base::StringPrintf("[{\"id\": %d," | |
| 2487 " \"filename\": {" | |
| 2488 " \"previous\": \"%s\"," | |
| 2489 " \"current\": \"%s\"}," | |
| 2490 " \"state\": {" | |
| 2491 " \"previous\": \"in_progress\"," | |
| 2492 " \"current\": \"complete\"}}]", | |
| 2493 result_id, | |
| 2494 GetFilename("slow.txt.crdownload").c_str(), | |
| 2495 GetFilename("slow.txt").c_str()))); | |
| 2496 } | |
| 2497 | |
| 2498 IN_PROC_BROWSER_TEST_F( | |
| 2499 DownloadExtensionTest, | |
| 2500 DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) { | |
| 2501 GoOnTheRecord(); | |
| 2502 LoadExtension("downloads_split"); | |
| 2503 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2504 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2505 CHECK(StartTestServer()); | |
| 2506 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2507 | |
| 2508 // Start downloading a file. | |
| 2509 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2510 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2511 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2512 ASSERT_TRUE(result.get()); | |
| 2513 int result_id = -1; | |
| 2514 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2515 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2516 ASSERT_TRUE(item); | |
| 2517 ScopedCancellingItem canceller(item); | |
| 2518 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2519 | |
| 2520 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2521 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2522 " \"incognito\": false," | |
| 2523 " \"id\": %d," | |
| 2524 " \"mime\": \"text/plain\"," | |
| 2525 " \"paused\": false," | |
| 2526 " \"url\": \"%s\"}]", | |
| 2527 result_id, | |
| 2528 download_url.c_str()))); | |
| 2529 ASSERT_TRUE(WaitFor( | |
| 2530 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2531 base::StringPrintf("[{\"id\": %d," | |
| 2532 " \"filename\":\"slow.txt\"}]", | |
| 2533 result_id))); | |
| 2534 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2535 ASSERT_TRUE(item->IsInProgress()); | |
| 2536 | |
| 2537 // Respond to the onDeterminingFilename. Absolute paths should be rejected. | |
| 2538 base::FilePath override; | |
| 2539 #if defined(OS_WIN) | |
| 2540 override = base::FilePath(FILE_PATH_LITERAL("C:\\sneaky.dll")); | |
| 2541 #else | |
| 2542 override = base::FilePath(FILE_PATH_LITERAL("/sneaky")); | |
| 2543 #endif | |
| 2544 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2545 browser()->profile(), | |
| 2546 false, | |
| 2547 GetExtensionId(), | |
| 2548 42, | |
| 2549 result_id, | |
| 2550 override, | |
| 2551 false)); | |
| 2552 | |
| 2553 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2554 base::StringPrintf("[{\"id\": %d," | |
| 2555 " \"filename\": {" | |
| 2556 " \"previous\": \"%s\"," | |
| 2557 " \"current\": \"%s\"}," | |
| 2558 " \"state\": {" | |
| 2559 " \"previous\": \"in_progress\"," | |
| 2560 " \"current\": \"complete\"}}]", | |
| 2561 result_id, | |
| 2562 GetFilename("slow.txt.crdownload").c_str(), | |
| 2563 GetFilename("slow.txt").c_str()))); | |
| 2564 } | |
| 2565 | |
| 2566 IN_PROC_BROWSER_TEST_F( | |
| 2567 DownloadExtensionTest, | |
| 2568 DownloadExtensionTest_OnDeterminingFilename_Override) { | |
| 2569 GoOnTheRecord(); | |
| 2570 LoadExtension("downloads_split"); | |
| 2571 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2572 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2573 CHECK(StartTestServer()); | |
| 2574 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2575 | |
| 2576 // Start downloading a file. | |
| 2577 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2578 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2579 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2580 ASSERT_TRUE(result.get()); | |
| 2581 int result_id = -1; | |
| 2582 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2583 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2584 ASSERT_TRUE(item); | |
| 2585 ScopedCancellingItem canceller(item); | |
| 2586 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2587 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2588 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2589 " \"incognito\": false," | |
| 2590 " \"id\": %d," | |
| 2591 " \"mime\": \"text/plain\"," | |
| 2592 " \"paused\": false," | |
| 2593 " \"url\": \"%s\"}]", | |
| 2594 result_id, | |
| 2595 download_url.c_str()))); | |
| 2596 ASSERT_TRUE(WaitFor( | |
| 2597 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2598 base::StringPrintf("[{\"id\": %d," | |
| 2599 " \"filename\":\"slow.txt\"}]", | |
| 2600 result_id))); | |
| 2601 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2602 ASSERT_TRUE(item->IsInProgress()); | |
| 2603 | |
| 2604 // Respond to the onDeterminingFilename. | |
| 2605 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2606 browser()->profile(), | |
| 2607 false, | |
| 2608 GetExtensionId(), | |
| 2609 42, | |
| 2610 result_id, | |
| 2611 base::FilePath(), | |
| 2612 false)); | |
| 2613 | |
| 2614 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2615 base::StringPrintf("[{\"id\": %d," | |
| 2616 " \"filename\": {" | |
| 2617 " \"previous\": \"%s\"," | |
| 2618 " \"current\": \"%s\"}," | |
| 2619 " \"state\": {" | |
| 2620 " \"previous\": \"in_progress\"," | |
| 2621 " \"current\": \"complete\"}}]", | |
| 2622 result_id, | |
| 2623 GetFilename("slow.txt.crdownload").c_str(), | |
| 2624 GetFilename("slow.txt").c_str()))); | |
| 2625 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2626 base::StringPrintf("[{\"id\": %d," | |
| 2627 " \"state\": {" | |
| 2628 " \"previous\": \"in_progress\"," | |
| 2629 " \"current\": \"complete\"}}]", | |
| 2630 result_id))); | |
| 2631 | |
| 2632 // Start downloading a file. | |
| 2633 result.reset(RunFunctionAndReturnResult( | |
| 2634 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2635 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2636 ASSERT_TRUE(result.get()); | |
| 2637 result_id = -1; | |
| 2638 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2639 item = GetCurrentManager()->GetDownload(result_id); | |
| 2640 ASSERT_TRUE(item); | |
| 2641 ScopedCancellingItem canceller2(item); | |
| 2642 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2643 | |
| 2644 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2645 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2646 " \"incognito\": false," | |
| 2647 " \"id\": %d," | |
| 2648 " \"mime\": \"text/plain\"," | |
| 2649 " \"paused\": false," | |
| 2650 " \"url\": \"%s\"}]", | |
| 2651 result_id, | |
| 2652 download_url.c_str()))); | |
| 2653 ASSERT_TRUE(WaitFor( | |
| 2654 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2655 base::StringPrintf("[{\"id\": %d," | |
| 2656 " \"filename\":\"slow.txt\"}]", | |
| 2657 result_id))); | |
| 2658 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2659 ASSERT_TRUE(item->IsInProgress()); | |
| 2660 | |
| 2661 // Respond to the onDeterminingFilename. | |
| 2662 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2663 browser()->profile(), | |
| 2664 false, | |
| 2665 GetExtensionId(), | |
| 2666 42, | |
| 2667 result_id, | |
| 2668 base::FilePath(FILE_PATH_LITERAL("slow.txt")), | |
| 2669 true)); | |
| 2670 | |
| 2671 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2672 base::StringPrintf("[{\"id\": %d," | |
| 2673 " \"filename\": {" | |
| 2674 " \"previous\": \"%s\"," | |
| 2675 " \"current\": \"%s\"}," | |
| 2676 " \"state\": {" | |
| 2677 " \"previous\": \"in_progress\"," | |
| 2678 " \"current\": \"complete\"}}]", | |
| 2679 result_id, | |
| 2680 GetFilename("slow.txt.crdownload").c_str(), | |
| 2681 GetFilename("slow.txt").c_str()))); | |
| 2682 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2683 base::StringPrintf("[{\"id\": %d," | |
| 2684 " \"state\": {" | |
| 2685 " \"previous\": \"in_progress\"," | |
| 2686 " \"current\": \"complete\"}}]", | |
| 2687 result_id))); | |
| 2688 } | |
| 2689 | |
| 2690 IN_PROC_BROWSER_TEST_F( | |
| 2691 DownloadExtensionTest, | |
| 2692 DownloadExtensionTest_OnDeterminingFilename_LaterDeterminerPrecedence) { | |
| 2693 CHECK(StartTestServer()); | |
| 2694 GoOnTheRecord(); | |
| 2695 LoadExtension("downloads_split"); | |
| 2696 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2697 browser()->profile(), false, GetExtensionId(), 5)); | |
| 2698 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2699 browser()->profile(), false, GetExtensionId(), 43)); | |
| 2700 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2701 | |
| 2702 // Start downloading a file. | |
| 2703 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2704 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2705 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2706 ASSERT_TRUE(result.get()); | |
| 2707 int result_id = -1; | |
| 2708 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2709 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2710 ASSERT_TRUE(item); | |
| 2711 ScopedCancellingItem canceller(item); | |
| 2712 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2713 | |
| 2714 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2715 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2716 " \"incognito\": false," | |
| 2717 " \"id\": %d," | |
| 2718 " \"mime\": \"text/plain\"," | |
| 2719 " \"paused\": false," | |
| 2720 " \"url\": \"%s\"}]", | |
| 2721 result_id, | |
| 2722 download_url.c_str()))); | |
| 2723 ASSERT_TRUE(WaitFor( | |
| 2724 std::string(events::kOnDownloadDeterminingFilename) + "/5", | |
| 2725 base::StringPrintf("[{\"id\": %d," | |
| 2726 " \"filename\":\"slow.txt\"}]", | |
| 2727 result_id))); | |
| 2728 ASSERT_TRUE(WaitFor( | |
| 2729 std::string(events::kOnDownloadDeterminingFilename) + "/43", | |
| 2730 base::StringPrintf("[{\"id\": %d," | |
| 2731 " \"filename\":\"slow.txt\"}]", | |
| 2732 result_id))); | |
| 2733 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2734 ASSERT_TRUE(item->IsInProgress()); | |
| 2735 | |
| 2736 // Respond to the onDeterminingFilename. | |
| 2737 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2738 browser()->profile(), | |
| 2739 false, | |
| 2740 GetExtensionId(), | |
| 2741 5, | |
| 2742 result_id, | |
| 2743 base::FilePath(FILE_PATH_LITERAL("5.txt")), | |
| 2744 false)); | |
| 2745 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2746 browser()->profile(), | |
| 2747 false, | |
| 2748 GetExtensionId(), | |
| 2749 43, | |
| 2750 result_id, | |
| 2751 base::FilePath(FILE_PATH_LITERAL("43.txt")), | |
| 2752 false)); | |
| 2753 | |
| 2754 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2755 base::StringPrintf("[{\"id\": %d," | |
| 2756 " \"filename\": {" | |
| 2757 " \"previous\": \"%s\"," | |
| 2758 " \"current\": \"%s\"}," | |
| 2759 " \"state\": {" | |
| 2760 " \"previous\": \"in_progress\"," | |
| 2761 " \"current\": \"complete\"}}]", | |
| 2762 result_id, | |
| 2763 GetFilename("43.txt.crdownload").c_str(), | |
| 2764 GetFilename("43.txt").c_str()))); | |
| 2765 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2766 base::StringPrintf("[{\"id\": %d," | |
| 2767 " \"state\": {" | |
| 2768 " \"previous\": \"in_progress\"," | |
| 2769 " \"current\": \"complete\"}}]", | |
| 2770 result_id))); | |
| 2771 | |
| 2772 // Start downloading a file. | |
| 2773 result.reset(RunFunctionAndReturnResult( | |
| 2774 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2775 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2776 ASSERT_TRUE(result.get()); | |
| 2777 result_id = -1; | |
| 2778 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2779 item = GetCurrentManager()->GetDownload(result_id); | |
| 2780 ASSERT_TRUE(item); | |
| 2781 ScopedCancellingItem canceller2(item); | |
| 2782 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2783 | |
| 2784 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2785 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2786 " \"incognito\": false," | |
| 2787 " \"id\": %d," | |
| 2788 " \"mime\": \"text/plain\"," | |
| 2789 " \"paused\": false," | |
| 2790 " \"url\": \"%s\"}]", | |
| 2791 result_id, | |
| 2792 download_url.c_str()))); | |
| 2793 ASSERT_TRUE(WaitFor( | |
| 2794 std::string(events::kOnDownloadDeterminingFilename) + "/5", | |
| 2795 base::StringPrintf("[{\"id\": %d," | |
| 2796 " \"filename\":\"slow.txt\"}]", | |
| 2797 result_id))); | |
| 2798 ASSERT_TRUE(WaitFor( | |
| 2799 std::string(events::kOnDownloadDeterminingFilename) + "/43", | |
| 2800 base::StringPrintf("[{\"id\": %d," | |
| 2801 " \"filename\":\"slow.txt\"}]", | |
| 2802 result_id))); | |
| 2803 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2804 ASSERT_TRUE(item->IsInProgress()); | |
| 2805 | |
| 2806 // Respond to the onDeterminingFilename. | |
| 2807 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2808 browser()->profile(), | |
| 2809 false, | |
| 2810 GetExtensionId(), | |
| 2811 43, | |
| 2812 result_id, | |
| 2813 base::FilePath(FILE_PATH_LITERAL("43b.txt")), | |
| 2814 false)); | |
| 2815 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2816 browser()->profile(), | |
| 2817 false, | |
| 2818 GetExtensionId(), | |
| 2819 5, | |
| 2820 result_id, | |
| 2821 base::FilePath(FILE_PATH_LITERAL("5b.txt")), | |
| 2822 false)); | |
| 2823 | |
| 2824 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2825 base::StringPrintf("[{\"id\": %d," | |
| 2826 " \"filename\": {" | |
| 2827 " \"previous\": \"%s\"," | |
| 2828 " \"current\": \"%s\"}," | |
| 2829 " \"state\": {" | |
| 2830 " \"previous\": \"in_progress\"," | |
| 2831 " \"current\": \"complete\"}}]", | |
| 2832 result_id, | |
| 2833 GetFilename("43b.txt.crdownload").c_str(), | |
| 2834 GetFilename("43b.txt").c_str()))); | |
| 2835 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2836 base::StringPrintf("[{\"id\": %d," | |
| 2837 " \"state\": {" | |
| 2838 " \"previous\": \"in_progress\"," | |
| 2839 " \"current\": \"complete\"}}]", | |
| 2840 result_id))); | |
| 2841 } | |
| 2842 | |
| 2843 // TODO test precedence rules: install_time | |
| 2844 | |
| 2845 IN_PROC_BROWSER_TEST_F( | |
| 2846 DownloadExtensionTest, | |
| 2847 DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) { | |
| 2848 CHECK(StartTestServer()); | |
| 2849 GoOnTheRecord(); | |
| 2850 LoadExtension("downloads_split"); | |
| 2851 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2852 browser()->profile(), false, GetExtensionId(), 42)); | |
| 2853 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2854 browser()->profile(), false, GetExtensionId(), 43)); | |
| 2855 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2856 | |
| 2857 // Start downloading a file. | |
| 2858 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2859 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2860 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2861 ASSERT_TRUE(result.get()); | |
| 2862 int result_id = -1; | |
| 2863 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2864 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2865 ASSERT_TRUE(item); | |
| 2866 ScopedCancellingItem canceller(item); | |
| 2867 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2868 | |
| 2869 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2870 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2871 " \"incognito\": false," | |
| 2872 " \"id\": %d," | |
| 2873 " \"mime\": \"text/plain\"," | |
| 2874 " \"paused\": false," | |
| 2875 " \"url\": \"%s\"}]", | |
| 2876 result_id, | |
| 2877 download_url.c_str()))); | |
| 2878 ASSERT_TRUE(WaitFor( | |
| 2879 std::string(events::kOnDownloadDeterminingFilename) + "/43", | |
| 2880 base::StringPrintf("[{\"id\": %d," | |
| 2881 " \"filename\":\"slow.txt\"}]", | |
| 2882 result_id))); | |
| 2883 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2884 ASSERT_TRUE(item->IsInProgress()); | |
| 2885 | |
| 2886 // Respond to the onDeterminingFilename. | |
| 2887 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2888 browser()->profile(), | |
| 2889 false, | |
| 2890 GetExtensionId(), | |
| 2891 43, | |
| 2892 result_id, | |
| 2893 base::FilePath(), | |
| 2894 false)); | |
| 2895 | |
| 2896 // Remove a determiner while waiting for it. | |
| 2897 ASSERT_TRUE(ExtensionDownloadsEventRouter::RemoveFilenameDeterminer( | |
| 2898 browser()->profile(), GetExtensionId(), 42)); | |
| 2899 | |
| 2900 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2901 base::StringPrintf("[{\"id\": %d," | |
| 2902 " \"state\": {" | |
| 2903 " \"previous\": \"in_progress\"," | |
| 2904 " \"current\": \"complete\"}}]", | |
| 2905 result_id))); | |
| 2906 | |
| 2907 // Start downloading a file. | |
| 2908 result.reset(RunFunctionAndReturnResult( | |
| 2909 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2910 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2911 ASSERT_TRUE(result.get()); | |
| 2912 result_id = -1; | |
| 2913 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2914 item = GetCurrentManager()->GetDownload(result_id); | |
| 2915 ASSERT_TRUE(item); | |
| 2916 ScopedCancellingItem canceller2(item); | |
| 2917 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2918 | |
| 2919 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2920 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2921 " \"incognito\": false," | |
| 2922 " \"id\": %d," | |
| 2923 " \"mime\": \"text/plain\"," | |
| 2924 " \"paused\": false," | |
| 2925 " \"url\": \"%s\"}]", | |
| 2926 result_id, | |
| 2927 download_url.c_str()))); | |
| 2928 ASSERT_TRUE(WaitFor( | |
| 2929 std::string(events::kOnDownloadDeterminingFilename) + "/43", | |
| 2930 base::StringPrintf("[{\"id\": %d," | |
| 2931 " \"filename\":\"slow.txt\"}]", | |
| 2932 result_id))); | |
| 2933 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 2934 ASSERT_TRUE(item->IsInProgress()); | |
| 2935 | |
| 2936 // Respond to the onDeterminingFilename. | |
| 2937 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 2938 browser()->profile(), | |
| 2939 false, | |
| 2940 GetExtensionId(), | |
| 2941 43, | |
| 2942 result_id, | |
| 2943 base::FilePath(), | |
| 2944 false)); | |
| 2945 // Should not wait for the 42 determiner. | |
| 2946 | |
| 2947 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 2948 base::StringPrintf("[{\"id\": %d," | |
| 2949 " \"state\": {" | |
| 2950 " \"previous\": \"in_progress\"," | |
| 2951 " \"current\": \"complete\"}}]", | |
| 2952 result_id))); | |
| 2953 } | |
| 2954 | |
| 2955 IN_PROC_BROWSER_TEST_F( | |
| 2956 DownloadExtensionTest, | |
| 2957 DownloadExtensionTest_OnDeterminingFilename_IncognitoSplit) { | |
| 2958 LoadExtension("downloads_split"); | |
| 2959 CHECK(StartTestServer()); | |
| 2960 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 2961 | |
| 2962 GoOnTheRecord(); | |
| 2963 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2964 current_browser()->profile(), false, GetExtensionId(), 42)); | |
| 2965 | |
| 2966 GoOffTheRecord(); | |
| 2967 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 2968 current_browser()->profile(), false, GetExtensionId(), 5)); | |
| 2969 | |
| 2970 // Start an on-record download. | |
| 2971 GoOnTheRecord(); | |
| 2972 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 2973 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 2974 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 2975 ASSERT_TRUE(result.get()); | |
| 2976 int result_id = -1; | |
| 2977 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 2978 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 2979 ASSERT_TRUE(item); | |
| 2980 ScopedCancellingItem canceller(item); | |
| 2981 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 2982 | |
| 2983 // Wait for the onCreated and onDeterminingFilename events. | |
| 2984 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 2985 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 2986 " \"incognito\": false," | |
| 2987 " \"id\": %d," | |
| 2988 " \"mime\": \"text/plain\"," | |
| 2989 " \"paused\": false," | |
| 2990 " \"url\": \"%s\"}]", | |
| 2991 result_id, | |
| 2992 download_url.c_str()))); | |
| 2993 ASSERT_TRUE(WaitFor( | |
| 2994 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 2995 base::StringPrintf("[{\"id\": %d," | |
| 2996 " \"incognito\": false," | |
| 2997 " \"filename\":\"slow.txt\"}]", | |
| 2998 result_id))); | |
| 2999 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3000 ASSERT_TRUE(item->IsInProgress()); | |
| 3001 | |
| 3002 // Respond to the onDeterminingFilename events. | |
| 3003 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 3004 current_browser()->profile(), | |
| 3005 false, | |
| 3006 GetExtensionId(), | |
| 3007 42, | |
| 3008 result_id, | |
| 3009 base::FilePath(FILE_PATH_LITERAL("42.txt")), | |
| 3010 false)); | |
| 3011 | |
| 3012 // The download should complete successfully. | |
| 3013 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3014 base::StringPrintf("[{\"id\": %d," | |
| 3015 " \"filename\": {" | |
| 3016 " \"previous\": \"%s\"," | |
| 3017 " \"current\": \"%s\"}," | |
| 3018 " \"state\": {" | |
| 3019 " \"previous\": \"in_progress\"," | |
| 3020 " \"current\": \"complete\"}}]", | |
| 3021 result_id, | |
| 3022 GetFilename("42.txt.crdownload").c_str(), | |
| 3023 GetFilename("42.txt").c_str()))); | |
| 3024 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3025 base::StringPrintf("[{\"id\": %d," | |
| 3026 " \"state\": {" | |
| 3027 " \"previous\": \"in_progress\"," | |
| 3028 " \"current\": \"complete\"}}]", | |
| 3029 result_id))); | |
| 3030 | |
| 3031 // Start an incognito download for comparison. | |
| 3032 GoOffTheRecord(); | |
| 3033 result.reset(RunFunctionAndReturnResult( | |
| 3034 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 3035 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 3036 ASSERT_TRUE(result.get()); | |
| 3037 result_id = -1; | |
| 3038 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 3039 item = GetCurrentManager()->GetDownload(result_id); | |
| 3040 ASSERT_TRUE(item); | |
| 3041 ScopedCancellingItem canceller2(item); | |
| 3042 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 3043 | |
| 3044 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 3045 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 3046 " \"incognito\": true," | |
| 3047 " \"id\": %d," | |
| 3048 " \"mime\": \"text/plain\"," | |
| 3049 " \"paused\": false," | |
| 3050 " \"url\": \"%s\"}]", | |
| 3051 result_id, | |
| 3052 download_url.c_str()))); | |
| 3053 // On-Record renderers should not see events for off-record items. | |
| 3054 ASSERT_TRUE(WaitFor( | |
| 3055 std::string(events::kOnDownloadDeterminingFilename) + "/5", | |
| 3056 base::StringPrintf("[{\"id\": %d," | |
| 3057 " \"incognito\": true," | |
| 3058 " \"filename\":\"slow.txt\"}]", | |
| 3059 result_id))); | |
| 3060 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3061 ASSERT_TRUE(item->IsInProgress()); | |
| 3062 | |
| 3063 // Respond to the onDeterminingFilename. | |
| 3064 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 3065 current_browser()->profile(), | |
| 3066 false, | |
| 3067 GetExtensionId(), | |
| 3068 5, | |
| 3069 result_id, | |
| 3070 base::FilePath(FILE_PATH_LITERAL("5.txt")), | |
| 3071 false)); | |
| 3072 | |
| 3073 // The download should complete successfully. | |
| 3074 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3075 base::StringPrintf("[{\"id\": %d," | |
| 3076 " \"filename\": {" | |
| 3077 " \"previous\": \"%s\"," | |
| 3078 " \"current\": \"%s\"}," | |
| 3079 " \"state\": {" | |
| 3080 " \"previous\": \"in_progress\"," | |
| 3081 " \"current\": \"complete\"}}]", | |
| 3082 result_id, | |
| 3083 GetFilename("5.txt.crdownload").c_str(), | |
| 3084 GetFilename("5.txt").c_str()))); | |
| 3085 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3086 base::StringPrintf("[{\"id\": %d," | |
| 3087 " \"state\": {" | |
| 3088 " \"previous\": \"in_progress\"," | |
| 3089 " \"current\": \"complete\"}}]", | |
| 3090 result_id))); | |
| 3091 } | |
| 3092 | |
| 3093 IN_PROC_BROWSER_TEST_F( | |
| 3094 DownloadExtensionTest, | |
| 3095 DownloadExtensionTest_OnDeterminingFilename_IncognitoSpanning) { | |
| 3096 LoadExtension("downloads_spanning"); | |
| 3097 CHECK(StartTestServer()); | |
| 3098 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 3099 | |
| 3100 GoOnTheRecord(); | |
| 3101 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 3102 current_browser()->profile(), true, GetExtensionId(), 42)); | |
| 3103 | |
| 3104 // There is a single extension renderer that sees both on-record and | |
| 3105 // off-record events. The extension functions see the on-record profile with | |
| 3106 // include_incognito=true. | |
| 3107 | |
| 3108 // Start an on-record download. | |
| 3109 GoOnTheRecord(); | |
| 3110 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 3111 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 3112 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 3113 ASSERT_TRUE(result.get()); | |
| 3114 int result_id = -1; | |
| 3115 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 3116 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 3117 ASSERT_TRUE(item); | |
| 3118 ScopedCancellingItem canceller(item); | |
| 3119 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 3120 | |
| 3121 // Wait for the onCreated and onDeterminingFilename events. | |
| 3122 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 3123 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 3124 " \"incognito\": false," | |
| 3125 " \"id\": %d," | |
| 3126 " \"mime\": \"text/plain\"," | |
| 3127 " \"paused\": false," | |
| 3128 " \"url\": \"%s\"}]", | |
| 3129 result_id, | |
| 3130 download_url.c_str()))); | |
| 3131 ASSERT_TRUE(WaitFor( | |
| 3132 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 3133 base::StringPrintf("[{\"id\": %d," | |
| 3134 " \"incognito\": false," | |
| 3135 " \"filename\":\"slow.txt\"}]", | |
| 3136 result_id))); | |
| 3137 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3138 ASSERT_TRUE(item->IsInProgress()); | |
| 3139 | |
| 3140 // Respond to the onDeterminingFilename events. | |
| 3141 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 3142 current_browser()->profile(), | |
| 3143 true, | |
| 3144 GetExtensionId(), | |
| 3145 42, | |
| 3146 result_id, | |
| 3147 base::FilePath(FILE_PATH_LITERAL("42.txt")), | |
| 3148 false)); | |
| 3149 | |
| 3150 // The download should complete successfully. | |
| 3151 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3152 base::StringPrintf("[{\"id\": %d," | |
| 3153 " \"filename\": {" | |
| 3154 " \"previous\": \"%s\"," | |
| 3155 " \"current\": \"%s\"}," | |
| 3156 " \"state\": {" | |
| 3157 " \"previous\": \"in_progress\"," | |
| 3158 " \"current\": \"complete\"}}]", | |
| 3159 result_id, | |
| 3160 GetFilename("42.txt.crdownload").c_str(), | |
| 3161 GetFilename("42.txt").c_str()))); | |
| 3162 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3163 base::StringPrintf("[{\"id\": %d," | |
| 3164 " \"state\": {" | |
| 3165 " \"previous\": \"in_progress\"," | |
| 3166 " \"current\": \"complete\"}}]", | |
| 3167 result_id))); | |
| 3168 | |
| 3169 // Start an incognito download for comparison. | |
| 3170 GoOffTheRecord(); | |
| 3171 result.reset(RunFunctionAndReturnResult( | |
| 3172 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 3173 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 3174 ASSERT_TRUE(result.get()); | |
| 3175 result_id = -1; | |
| 3176 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 3177 item = GetCurrentManager()->GetDownload(result_id); | |
| 3178 ASSERT_TRUE(item); | |
| 3179 ScopedCancellingItem canceller2(item); | |
| 3180 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 3181 | |
| 3182 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 3183 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 3184 " \"incognito\": true," | |
| 3185 " \"id\": %d," | |
| 3186 " \"mime\": \"text/plain\"," | |
| 3187 " \"paused\": false," | |
| 3188 " \"url\": \"%s\"}]", | |
| 3189 result_id, | |
| 3190 download_url.c_str()))); | |
| 3191 ASSERT_TRUE(WaitFor( | |
| 3192 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 3193 base::StringPrintf("[{\"id\": %d," | |
| 3194 " \"incognito\": true," | |
| 3195 " \"filename\":\"slow.txt\"}]", | |
| 3196 result_id))); | |
| 3197 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3198 ASSERT_TRUE(item->IsInProgress()); | |
| 3199 | |
| 3200 // Respond to the onDeterminingFilename. | |
| 3201 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 3202 current_browser()->profile(), | |
| 3203 true, | |
| 3204 GetExtensionId(), | |
| 3205 42, | |
| 3206 result_id, | |
| 3207 base::FilePath(FILE_PATH_LITERAL("42.txt")), | |
| 3208 false)); | |
| 3209 | |
| 3210 // The download should complete successfully. | |
| 3211 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3212 base::StringPrintf("[{\"id\": %d," | |
| 3213 " \"filename\": {" | |
| 3214 " \"previous\": \"%s\"," | |
| 3215 " \"current\": \"%s\"}," | |
| 3216 " \"state\": {" | |
| 3217 " \"previous\": \"in_progress\"," | |
| 3218 " \"current\": \"complete\"}}]", | |
| 3219 result_id, | |
| 3220 GetFilename("42 (1).txt.crdownload").c_str(), | |
| 3221 GetFilename("42 (1).txt").c_str()))); | |
| 3222 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3223 base::StringPrintf("[{\"id\": %d," | |
| 3224 " \"state\": {" | |
| 3225 " \"previous\": \"in_progress\"," | |
| 3226 " \"current\": \"complete\"}}]", | |
| 3227 result_id))); | |
| 3228 } | |
| 3229 | |
| 3230 // Test download interruption while extensions determining filename, re-run | |
| 3231 // through fan-out and fan-in. | |
| 3232 // TODO(rdsmith): FILE_OPERATION_INITIALIZE is not right for this test. | |
| 3233 IN_PROC_BROWSER_TEST_F( | |
| 3234 DownloadExtensionTest, | |
| 3235 FAILS_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) { | |
| 3236 LoadExtension("downloads_split"); | |
| 3237 CHECK(StartTestServer()); | |
| 3238 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 3239 GoOnTheRecord(); | |
| 3240 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 3241 current_browser()->profile(), false, GetExtensionId(), 42)); | |
| 3242 | |
| 3243 // TODO Interrupt the download instead of responding to onDeterminingFilename. | |
| 3244 scoped_refptr<content::TestFileErrorInjector> injector( | |
| 3245 content::TestFileErrorInjector::Create( | |
| 3246 GetCurrentManager())); | |
| 3247 content::TestFileErrorInjector::FileErrorInfo error_info = { | |
| 3248 download_url, | |
| 3249 content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | |
| 3250 0, | |
| 3251 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; | |
| 3252 injector->AddError(error_info); | |
| 3253 injector->InjectErrors(); | |
| 3254 | |
| 3255 // Start a download. | |
| 3256 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 3257 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 3258 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 3259 ASSERT_TRUE(result.get()); | |
| 3260 int result_id = -1; | |
| 3261 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 3262 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 3263 ASSERT_TRUE(item); | |
| 3264 ScopedCancellingItem canceller(item); | |
| 3265 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 3266 | |
| 3267 // Wait for the onCreated and onDeterminingFilename event. | |
| 3268 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 3269 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 3270 " \"incognito\": false," | |
| 3271 " \"id\": %d," | |
| 3272 " \"mime\": \"text/plain\"," | |
| 3273 " \"paused\": false," | |
| 3274 " \"url\": \"%s\"}]", | |
| 3275 result_id, | |
| 3276 download_url.c_str()))); | |
| 3277 ASSERT_TRUE(WaitFor( | |
| 3278 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 3279 base::StringPrintf("[{\"id\": %d," | |
| 3280 " \"incognito\": false," | |
| 3281 " \"filename\":\"slow.txt\"}]", | |
| 3282 result_id))); | |
| 3283 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3284 ASSERT_TRUE(item->IsInProgress()); | |
| 3285 | |
| 3286 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3287 base::StringPrintf("[{\"id\": %d," | |
| 3288 " \"state\": {" | |
| 3289 " \"previous\": \"in_progress\"," | |
| 3290 " \"current\": \"interrupted\"}}]", | |
| 3291 result_id))); | |
| 3292 ASSERT_TRUE(item->IsInterrupted()); | |
| 3293 item->ResumeInterruptedDownload(); | |
| 3294 | |
| 3295 // Wait for and respond to the onDeterminingFilename event. | |
| 3296 ASSERT_TRUE(WaitFor( | |
| 3297 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 3298 base::StringPrintf("[{\"id\": %d," | |
| 3299 " \"incognito\": false," | |
| 3300 " \"filename\":\"slow.txt\"}]", | |
| 3301 result_id))); | |
| 3302 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3303 ASSERT_TRUE(item->IsInProgress()); | |
| 3304 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( | |
| 3305 current_browser()->profile(), | |
| 3306 false, | |
| 3307 GetExtensionId(), | |
| 3308 42, | |
| 3309 result_id, | |
| 3310 base::FilePath(FILE_PATH_LITERAL("42.txt")), | |
| 3311 false)); | |
| 3312 | |
| 3313 // The download should complete successfully. | |
| 3314 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3315 base::StringPrintf("[{\"id\": %d," | |
| 3316 " \"filename\": {" | |
| 3317 " \"previous\": \"%s\"," | |
| 3318 " \"current\": \"%s\"}," | |
| 3319 " \"state\": {" | |
| 3320 " \"previous\": \"in_progress\"," | |
| 3321 " \"current\": \"complete\"}}]", | |
| 3322 result_id, | |
| 3323 GetFilename("42.txt.crdownload").c_str(), | |
| 3324 GetFilename("42.txt").c_str()))); | |
| 3325 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3326 base::StringPrintf("[{\"id\": %d," | |
| 3327 " \"state\": {" | |
| 3328 " \"previous\": \"in_progress\"," | |
| 3329 " \"current\": \"complete\"}}]", | |
| 3330 result_id))); | |
| 3331 } | |
| 3332 | |
| 3333 IN_PROC_BROWSER_TEST_F( | |
| 3334 DownloadExtensionTest, | |
| 3335 DownloadExtensionTest_OnDeterminingFilename_DisableExtension) { | |
| 3336 GoOnTheRecord(); | |
| 3337 LoadExtension("downloads_split"); | |
| 3338 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer( | |
| 3339 browser()->profile(), false, GetExtensionId(), 42)); | |
| 3340 CHECK(StartTestServer()); | |
| 3341 std::string download_url = test_server()->GetURL("slow?0").spec(); | |
| 3342 | |
| 3343 // Start downloading a file. | |
| 3344 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | |
| 3345 new DownloadsDownloadFunction(), base::StringPrintf( | |
| 3346 "[{\"url\": \"%s\"}]", download_url.c_str()))); | |
| 3347 ASSERT_TRUE(result.get()); | |
| 3348 int result_id = -1; | |
| 3349 ASSERT_TRUE(result->GetAsInteger(&result_id)); | |
| 3350 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); | |
| 3351 ASSERT_TRUE(item); | |
| 3352 ScopedCancellingItem canceller(item); | |
| 3353 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); | |
| 3354 | |
| 3355 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, | |
| 3356 base::StringPrintf("[{\"danger\": \"safe\"," | |
| 3357 " \"incognito\": false," | |
| 3358 " \"id\": %d," | |
| 3359 " \"mime\": \"text/plain\"," | |
| 3360 " \"paused\": false," | |
| 3361 " \"url\": \"%s\"}]", | |
| 3362 result_id, | |
| 3363 download_url.c_str()))); | |
| 3364 ASSERT_TRUE(WaitFor( | |
| 3365 std::string(events::kOnDownloadDeterminingFilename) + "/42", | |
| 3366 base::StringPrintf("[{\"id\": %d," | |
| 3367 " \"filename\":\"slow.txt\"}]", | |
| 3368 result_id))); | |
| 3369 ASSERT_TRUE(item->GetTargetFilePath().empty()); | |
| 3370 ASSERT_TRUE(item->IsInProgress()); | |
| 3371 | |
| 3372 DisableExtension(); | |
| 3373 | |
| 3374 // The download should complete because it isn't waiting for any other | |
| 3375 // determiners. | |
| 3376 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, | |
| 3377 base::StringPrintf("[{\"id\": %d," | |
| 3378 " \"state\": {" | |
| 3379 " \"previous\": \"in_progress\"," | |
| 3380 " \"current\": \"complete\"}}]", | |
| 3381 result_id))); | |
| 3382 } | |
| OLD | NEW |