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