Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/browser/extensions/api/downloads/downloads_api_unittest.cc

Issue 11574006: Implement chrome.downloads.onDeterminingFilename() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r185803 Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "chrome/browser/download/download_file_icon_extractor.h" 14 #include "chrome/browser/download/download_file_icon_extractor.h"
15 #include "chrome/browser/download/download_service.h" 15 #include "chrome/browser/download/download_service.h"
16 #include "chrome/browser/download/download_service_factory.h" 16 #include "chrome/browser/download/download_service_factory.h"
17 #include "chrome/browser/download/download_test_file_activity_observer.h" 17 #include "chrome/browser/download/download_test_file_activity_observer.h"
18 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 18 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
19 #include "chrome/browser/extensions/event_names.h" 19 #include "chrome/browser/extensions/event_names.h"
20 #include "chrome/browser/extensions/extension_apitest.h" 20 #include "chrome/browser/extensions/extension_apitest.h"
21 #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"
22 #include "chrome/browser/history/download_row.h" 23 #include "chrome/browser/history/download_row.h"
23 #include "chrome/browser/net/url_request_mock_util.h" 24 #include "chrome/browser/net/url_request_mock_util.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h" 26 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_tabstrip.h" 27 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/common/chrome_notification_types.h" 28 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
29 #include "chrome/test/base/in_process_browser_test.h" 30 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h" 31 #include "chrome/test/base/ui_test_utils.h"
31 #include "content/public/browser/browser_context.h" 32 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/download_item.h" 34 #include "content/public/browser/download_item.h"
34 #include "content/public/browser/download_manager.h" 35 #include "content/public/browser/download_manager.h"
35 #include "content/public/browser/notification_service.h" 36 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/storage_partition.h" 37 #include "content/public/browser/storage_partition.h"
37 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
38 #include "content/public/common/page_transition_types.h" 39 #include "content/public/common/page_transition_types.h"
39 #include "content/public/test/download_test_observer.h" 40 #include "content/public/test/download_test_observer.h"
41 #include "content/public/test/test_file_error_injector.h"
40 #include "content/test/net/url_request_slow_download_job.h" 42 #include "content/test/net/url_request_slow_download_job.h"
41 #include "net/base/data_url.h" 43 #include "net/base/data_url.h"
42 #include "net/base/net_util.h" 44 #include "net/base/net_util.h"
43 #include "net/url_request/url_request.h" 45 #include "net/url_request/url_request.h"
44 #include "net/url_request/url_request_context.h" 46 #include "net/url_request/url_request_context.h"
45 #include "net/url_request/url_request_job.h" 47 #include "net/url_request/url_request_job.h"
46 #include "net/url_request/url_request_job_factory.h" 48 #include "net/url_request/url_request_job_factory.h"
47 #include "net/url_request/url_request_job_factory_impl.h" 49 #include "net/url_request/url_request_job_factory_impl.h"
48 #include "webkit/blob/blob_data.h" 50 #include "webkit/blob/blob_data.h"
49 #include "webkit/blob/blob_storage_controller.h" 51 #include "webkit/blob/blob_storage_controller.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 args_(base::JSONReader::Read(json_args)), 98 args_(base::JSONReader::Read(json_args)),
97 caught_(caught) { 99 caught_(caught) {
98 } 100 }
99 101
100 const base::Time& caught() { return caught_; } 102 const base::Time& caught() { return caught_; }
101 103
102 bool Equals(const Event& other) { 104 bool Equals(const Event& other) {
103 if ((profile_ != other.profile_) || 105 if ((profile_ != other.profile_) ||
104 (event_name_ != other.event_name_)) 106 (event_name_ != other.event_name_))
105 return false; 107 return false;
106 if ((event_name_ == events::kOnDownloadCreated || 108 if (((event_name_ == events::kOnDownloadDeterminingFilename) ||
107 event_name_ == events::kOnDownloadChanged) && 109 (event_name_ == events::kOnDownloadCreated) ||
110 (event_name_ == events::kOnDownloadChanged)) &&
108 args_.get() && 111 args_.get() &&
109 other.args_.get()) { 112 other.args_.get()) {
110 base::ListValue* left_list = NULL; 113 base::ListValue* left_list = NULL;
111 base::DictionaryValue* left_dict = NULL; 114 base::DictionaryValue* left_dict = NULL;
112 base::ListValue* right_list = NULL; 115 base::ListValue* right_list = NULL;
113 base::DictionaryValue* right_dict = NULL; 116 base::DictionaryValue* right_dict = NULL;
114 if (!args_->GetAsList(&left_list) || 117 if (!args_->GetAsList(&left_list) ||
115 !other.args_->GetAsList(&right_list) || 118 !other.args_->GetAsList(&right_list) ||
116 !left_list->GetDictionary(0, &left_dict) || 119 !left_list->GetDictionary(0, &left_dict) ||
117 !right_list->GetDictionary(0, &right_dict)) 120 !right_list->GetDictionary(0, &right_dict))
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 }; 254 };
252 255
253 void LoadExtension(const char* name) { 256 void LoadExtension(const char* name) {
254 // Store the created Extension object so that we can attach it to 257 // Store the created Extension object so that we can attach it to
255 // ExtensionFunctions. Also load the extension in incognito profiles for 258 // ExtensionFunctions. Also load the extension in incognito profiles for
256 // testing incognito. 259 // testing incognito.
257 extension_ = LoadExtensionIncognito(test_data_dir_.AppendASCII(name)); 260 extension_ = LoadExtensionIncognito(test_data_dir_.AppendASCII(name));
258 CHECK(extension_); 261 CHECK(extension_);
259 } 262 }
260 263
264 content::RenderProcessHost* AddFilenameDeterminer() {
265 content::WebContents* tab = chrome::AddSelectedTabWithURL(
266 current_browser(),
267 extension_->GetResourceURL("empty.html"),
268 content::PAGE_TRANSITION_LINK);
269 extensions::ExtensionSystem::Get(current_browser()->profile())->
270 event_router()->AddEventListener(
271 extensions::event_names::kOnDownloadDeterminingFilename,
272 tab->GetRenderProcessHost(),
273 GetExtensionId());
274 return tab->GetRenderProcessHost();
275 }
276
277 void RemoveFilenameDeterminer(content::RenderProcessHost* host) {
278 extensions::ExtensionSystem::Get(current_browser()->profile())->
279 event_router()->RemoveEventListener(
280 extensions::event_names::kOnDownloadDeterminingFilename,
281 host,
282 GetExtensionId());
283 }
284
261 Browser* current_browser() { return current_browser_; } 285 Browser* current_browser() { return current_browser_; }
262 286
263 // InProcessBrowserTest 287 // InProcessBrowserTest
264 virtual void SetUpOnMainThread() OVERRIDE { 288 virtual void SetUpOnMainThread() OVERRIDE {
265 BrowserThread::PostTask( 289 BrowserThread::PostTask(
266 BrowserThread::IO, FROM_HERE, 290 BrowserThread::IO, FROM_HERE,
267 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 291 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
268 InProcessBrowserTest::SetUpOnMainThread(); 292 InProcessBrowserTest::SetUpOnMainThread();
269 GoOnTheRecord(); 293 GoOnTheRecord();
270 CreateAndSetDownloadsDirectory(); 294 CreateAndSetDownloadsDirectory();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 " \"state\": {" 330 " \"state\": {"
307 " \"previous\": \"in_progress\"," 331 " \"previous\": \"in_progress\","
308 " \"current\": \"interrupted\"}}]", 332 " \"current\": \"interrupted\"}}]",
309 item->GetId(), 333 item->GetId(),
310 expected_error)); 334 expected_error));
311 } 335 }
312 336
313 std::string GetExtensionURL() { 337 std::string GetExtensionURL() {
314 return extension_->url().spec(); 338 return extension_->url().spec();
315 } 339 }
340 std::string GetExtensionId() {
341 return extension_->id();
342 }
316 343
317 std::string GetFilename(const char* path) { 344 std::string GetFilename(const char* path) {
318 std::string result = 345 std::string result =
319 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); 346 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe();
320 #if defined(OS_WIN) 347 #if defined(OS_WIN)
321 for (std::string::size_type next = result.find("\\"); 348 for (std::string::size_type next = result.find("\\");
322 next != std::string::npos; 349 next != std::string::npos;
323 next = result.find("\\", next)) { 350 next = result.find("\\", next)) {
324 result.replace(next, 1, "\\\\"); 351 result.replace(next, 1, "\\\\");
325 next += 2; 352 next += 2;
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 return blob_storage_controller_.get(); 695 return blob_storage_controller_.get();
669 } 696 }
670 697
671 private: 698 private:
672 net::URLRequestJobFactoryImpl job_factory_; 699 net::URLRequestJobFactoryImpl job_factory_;
673 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; 700 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_;
674 701
675 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext); 702 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext);
676 }; 703 };
677 704
678 // TODO(benjhayden): Comment. 705 // Writes an HTML5 file so that it can be downloaded.
679 class HTML5FileWriter { 706 class HTML5FileWriter {
680 public: 707 public:
681 HTML5FileWriter( 708 HTML5FileWriter(
682 Profile* profile, 709 Profile* profile,
683 const std::string& filename, 710 const std::string& filename,
684 const std::string& origin, 711 const std::string& origin,
685 DownloadsEventsListener* events_listener, 712 DownloadsEventsListener* events_listener,
686 const std::string& payload) 713 const std::string& payload)
687 : profile_(profile), 714 : profile_(profile),
688 filename_(filename), 715 filename_(filename),
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 1482
1456 // Test that we can start a download and that the correct sequence of events is 1483 // Test that we can start a download and that the correct sequence of events is
1457 // fired for it. 1484 // fired for it.
1458 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1485 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1459 DownloadExtensionTest_Download_Basic) { 1486 DownloadExtensionTest_Download_Basic) {
1460 LoadExtension("downloads_split"); 1487 LoadExtension("downloads_split");
1461 CHECK(StartTestServer()); 1488 CHECK(StartTestServer());
1462 std::string download_url = test_server()->GetURL("slow?0").spec(); 1489 std::string download_url = test_server()->GetURL("slow?0").spec();
1463 GoOnTheRecord(); 1490 GoOnTheRecord();
1464 1491
1492 // Start downloading a file.
1465 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1493 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1466 new DownloadsDownloadFunction(), base::StringPrintf( 1494 new DownloadsDownloadFunction(), base::StringPrintf(
1467 "[{\"url\": \"%s\"}]", download_url.c_str()))); 1495 "[{\"url\": \"%s\"}]", download_url.c_str())));
1468 ASSERT_TRUE(result.get()); 1496 ASSERT_TRUE(result.get());
1469 int result_id = -1; 1497 int result_id = -1;
1470 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1498 ASSERT_TRUE(result->GetAsInteger(&result_id));
1471 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 1499 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
1472 ASSERT_TRUE(item); 1500 ASSERT_TRUE(item);
1473 ScopedCancellingItem canceller(item); 1501 ScopedCancellingItem canceller(item);
1474 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1502 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
(...skipping 20 matching lines...) Expand all
1495 1523
1496 // Test that we can start a download from an incognito context, and that the 1524 // Test that we can start a download from an incognito context, and that the
1497 // download knows that it's incognito. 1525 // download knows that it's incognito.
1498 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1526 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1499 DownloadExtensionTest_Download_Incognito) { 1527 DownloadExtensionTest_Download_Incognito) {
1500 LoadExtension("downloads_split"); 1528 LoadExtension("downloads_split");
1501 CHECK(StartTestServer()); 1529 CHECK(StartTestServer());
1502 GoOffTheRecord(); 1530 GoOffTheRecord();
1503 std::string download_url = test_server()->GetURL("slow?0").spec(); 1531 std::string download_url = test_server()->GetURL("slow?0").spec();
1504 1532
1533 // Start downloading a file.
1505 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1534 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1506 new DownloadsDownloadFunction(), base::StringPrintf( 1535 new DownloadsDownloadFunction(), base::StringPrintf(
1507 "[{\"url\": \"%s\"}]", download_url.c_str()))); 1536 "[{\"url\": \"%s\"}]", download_url.c_str())));
1508 ASSERT_TRUE(result.get()); 1537 ASSERT_TRUE(result.get());
1509 int result_id = -1; 1538 int result_id = -1;
1510 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1539 ASSERT_TRUE(result->GetAsInteger(&result_id));
1511 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 1540 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
1512 ASSERT_TRUE(item); 1541 ASSERT_TRUE(item);
1513 ScopedCancellingItem canceller(item); 1542 ScopedCancellingItem canceller(item);
1514 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1543 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1586 1615
1587 // Test that subdirectories (slashes) are disallowed in filenames. 1616 // Test that subdirectories (slashes) are disallowed in filenames.
1588 // TODO(benjhayden) Update this when subdirectories are supported. 1617 // TODO(benjhayden) Update this when subdirectories are supported.
1589 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1618 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1590 DownloadExtensionTest_Download_Subdirectory) { 1619 DownloadExtensionTest_Download_Subdirectory) {
1591 LoadExtension("downloads_split"); 1620 LoadExtension("downloads_split");
1592 CHECK(StartTestServer()); 1621 CHECK(StartTestServer());
1593 std::string download_url = test_server()->GetURL("slow?0").spec(); 1622 std::string download_url = test_server()->GetURL("slow?0").spec();
1594 GoOnTheRecord(); 1623 GoOnTheRecord();
1595 1624
1596 EXPECT_STREQ(download_extension_errors::kGenericError, 1625 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError,
1597 RunFunctionAndReturnError(new DownloadsDownloadFunction(), 1626 RunFunctionAndReturnError(new DownloadsDownloadFunction(),
1598 base::StringPrintf( 1627 base::StringPrintf(
1599 "[{\"url\": \"%s\"," 1628 "[{\"url\": \"%s\","
1600 " \"filename\": \"sub/dir/ect/ory.txt\"}]", 1629 " \"filename\": \"sub/dir/ect/ory.txt\"}]",
1601 download_url.c_str())).c_str()); 1630 download_url.c_str())).c_str());
1602 } 1631 }
1603 1632
1604 // Test that invalid filenames are disallowed. 1633 // Test that invalid filenames are disallowed.
1605 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1634 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1606 DownloadExtensionTest_Download_InvalidFilename) { 1635 DownloadExtensionTest_Download_InvalidFilename) {
1607 LoadExtension("downloads_split"); 1636 LoadExtension("downloads_split");
1608 CHECK(StartTestServer()); 1637 CHECK(StartTestServer());
1609 std::string download_url = test_server()->GetURL("slow?0").spec(); 1638 std::string download_url = test_server()->GetURL("slow?0").spec();
1610 GoOnTheRecord(); 1639 GoOnTheRecord();
1611 1640
1612 EXPECT_STREQ(download_extension_errors::kGenericError, 1641 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError,
1613 RunFunctionAndReturnError(new DownloadsDownloadFunction(), 1642 RunFunctionAndReturnError(new DownloadsDownloadFunction(),
1614 base::StringPrintf( 1643 base::StringPrintf(
1615 "[{\"url\": \"%s\"," 1644 "[{\"url\": \"%s\","
1616 " \"filename\": \"../../../../../etc/passwd\"}]", 1645 " \"filename\": \"../../../../../etc/passwd\"}]",
1617 download_url.c_str())).c_str()); 1646 download_url.c_str())).c_str());
1618 } 1647 }
1619 1648
1620 // Test that downloading invalid URLs immediately returns kInvalidURLError. 1649 // Test that downloading invalid URLs immediately returns kInvalidURLError.
1621 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1650 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1622 DownloadExtensionTest_Download_InvalidURLs) { 1651 DownloadExtensionTest_Download_InvalidURLs) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1683 " \"current\": \"complete\"}}]", 1712 " \"current\": \"complete\"}}]",
1684 result_id, 1713 result_id,
1685 GetFilename("slow.txt.crdownload").c_str(), 1714 GetFilename("slow.txt.crdownload").c_str(),
1686 GetFilename("slow.txt").c_str()))); 1715 GetFilename("slow.txt").c_str())));
1687 } 1716 }
1688 1717
1689 // Valid data URLs are valid URLs. 1718 // Valid data URLs are valid URLs.
1690 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1719 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1691 DownloadExtensionTest_Download_DataURL) { 1720 DownloadExtensionTest_Download_DataURL) {
1692 LoadExtension("downloads_split"); 1721 LoadExtension("downloads_split");
1693 CHECK(StartTestServer());
1694 std::string download_url = "data:text/plain,hello"; 1722 std::string download_url = "data:text/plain,hello";
1695 GoOnTheRecord(); 1723 GoOnTheRecord();
1696 1724
1697 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1725 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1698 new DownloadsDownloadFunction(), base::StringPrintf( 1726 new DownloadsDownloadFunction(), base::StringPrintf(
1699 "[{\"url\": \"%s\"," 1727 "[{\"url\": \"%s\","
1700 " \"filename\": \"data.txt\"}]", download_url.c_str()))); 1728 " \"filename\": \"data.txt\"}]", download_url.c_str())));
1701 ASSERT_TRUE(result.get()); 1729 ASSERT_TRUE(result.get());
1702 int result_id = -1; 1730 int result_id = -1;
1703 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1731 ASSERT_TRUE(result->GetAsInteger(&result_id));
(...skipping 27 matching lines...) Expand all
1731 // Disabled due to crbug.com/175711 1759 // Disabled due to crbug.com/175711
1732 #define MAYBE_DownloadExtensionTest_Download_File \ 1760 #define MAYBE_DownloadExtensionTest_Download_File \
1733 DISABLED_DownloadExtensionTest_Download_File 1761 DISABLED_DownloadExtensionTest_Download_File
1734 #else 1762 #else
1735 #define MAYBE_DownloadExtensionTest_Download_File \ 1763 #define MAYBE_DownloadExtensionTest_Download_File \
1736 DownloadExtensionTest_Download_File 1764 DownloadExtensionTest_Download_File
1737 #endif 1765 #endif
1738 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1766 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1739 MAYBE_DownloadExtensionTest_Download_File) { 1767 MAYBE_DownloadExtensionTest_Download_File) {
1740 GoOnTheRecord(); 1768 GoOnTheRecord();
1741 CHECK(StartTestServer());
1742 LoadExtension("downloads_split"); 1769 LoadExtension("downloads_split");
1743 std::string download_url = "file:///"; 1770 std::string download_url = "file:///";
1744 #if defined(OS_WIN) 1771 #if defined(OS_WIN)
1745 download_url += "C:/"; 1772 download_url += "C:/";
1746 #endif 1773 #endif
1747 1774
1748 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1775 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1749 new DownloadsDownloadFunction(), base::StringPrintf( 1776 new DownloadsDownloadFunction(), base::StringPrintf(
1750 "[{\"url\": \"%s\"," 1777 "[{\"url\": \"%s\","
1751 " \"filename\": \"file.txt\"}]", download_url.c_str()))); 1778 " \"filename\": \"file.txt\"}]", download_url.c_str())));
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2138 " \"state\": {" 2165 " \"state\": {"
2139 " \"previous\": \"in_progress\"," 2166 " \"previous\": \"in_progress\","
2140 " \"current\": \"complete\"}}]", 2167 " \"current\": \"complete\"}}]",
2141 result_id, 2168 result_id,
2142 GetFilename("on_record.txt.crdownload").c_str(), 2169 GetFilename("on_record.txt.crdownload").c_str(),
2143 GetFilename("on_record.txt").c_str()))); 2170 GetFilename("on_record.txt").c_str())));
2144 std::string disk_data; 2171 std::string disk_data;
2145 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2172 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2146 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2173 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2147 } 2174 }
2175
2176 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2177 DownloadExtensionTest_OnDeterminingFilename_NoChange) {
2178 GoOnTheRecord();
2179 LoadExtension("downloads_split");
2180 AddFilenameDeterminer();
2181 CHECK(StartTestServer());
2182 std::string download_url = test_server()->GetURL("slow?0").spec();
2183
2184 // Start downloading a file.
2185 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2186 new DownloadsDownloadFunction(), base::StringPrintf(
2187 "[{\"url\": \"%s\"}]", download_url.c_str())));
2188 ASSERT_TRUE(result.get());
2189 int result_id = -1;
2190 ASSERT_TRUE(result->GetAsInteger(&result_id));
2191 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2192 ASSERT_TRUE(item);
2193 ScopedCancellingItem canceller(item);
2194 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2195
2196 // Wait for the onCreated and onDeterminingFilename events.
2197 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2198 base::StringPrintf("[{\"danger\": \"safe\","
2199 " \"incognito\": false,"
2200 " \"id\": %d,"
2201 " \"mime\": \"text/plain\","
2202 " \"paused\": false,"
2203 " \"url\": \"%s\"}]",
2204 result_id,
2205 download_url.c_str())));
2206 ASSERT_TRUE(WaitFor(
2207 events::kOnDownloadDeterminingFilename,
2208 base::StringPrintf("[{\"id\": %d,"
2209 " \"filename\":\"slow.txt\"}]",
2210 result_id)));
2211 ASSERT_TRUE(item->GetTargetFilePath().empty());
2212 ASSERT_TRUE(item->IsInProgress());
2213
2214 // Respond to the onDeterminingFilename.
2215 std::string error;
2216 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2217 browser()->profile(),
2218 false,
2219 GetExtensionId(),
2220 result_id,
2221 base::FilePath(),
2222 false,
2223 &error));
2224 EXPECT_EQ("", error);
2225
2226 // The download should complete successfully.
2227 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2228 base::StringPrintf("[{\"id\": %d,"
2229 " \"filename\": {"
2230 " \"previous\": \"%s\","
2231 " \"current\": \"%s\"},"
2232 " \"state\": {"
2233 " \"previous\": \"in_progress\","
2234 " \"current\": \"complete\"}}]",
2235 result_id,
2236 GetFilename("slow.txt.crdownload").c_str(),
2237 GetFilename("slow.txt").c_str())));
2238 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2239 base::StringPrintf("[{\"id\": %d,"
2240 " \"state\": {"
2241 " \"previous\": \"in_progress\","
2242 " \"current\": \"complete\"}}]",
2243 result_id)));
2244 }
2245
2246 IN_PROC_BROWSER_TEST_F(
2247 DownloadExtensionTest,
2248 DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) {
2249 GoOnTheRecord();
2250 LoadExtension("downloads_split");
2251 AddFilenameDeterminer();
2252 CHECK(StartTestServer());
2253 std::string download_url = test_server()->GetURL("slow?0").spec();
2254
2255 // Start downloading a file.
2256 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2257 new DownloadsDownloadFunction(), base::StringPrintf(
2258 "[{\"url\": \"%s\"}]", download_url.c_str())));
2259 ASSERT_TRUE(result.get());
2260 int result_id = -1;
2261 ASSERT_TRUE(result->GetAsInteger(&result_id));
2262 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2263 ASSERT_TRUE(item);
2264 ScopedCancellingItem canceller(item);
2265 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2266
2267 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2268 base::StringPrintf("[{\"danger\": \"safe\","
2269 " \"incognito\": false,"
2270 " \"id\": %d,"
2271 " \"mime\": \"text/plain\","
2272 " \"paused\": false,"
2273 " \"url\": \"%s\"}]",
2274 result_id,
2275 download_url.c_str())));
2276 ASSERT_TRUE(WaitFor(
2277 events::kOnDownloadDeterminingFilename,
2278 base::StringPrintf("[{\"id\": %d,"
2279 " \"filename\":\"slow.txt\"}]",
2280 result_id)));
2281 ASSERT_TRUE(item->GetTargetFilePath().empty());
2282 ASSERT_TRUE(item->IsInProgress());
2283
2284 // Respond to the onDeterminingFilename.
2285 std::string error;
2286 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2287 browser()->profile(),
2288 false,
2289 GetExtensionId(),
2290 result_id,
2291 base::FilePath(FILE_PATH_LITERAL("overridden.swf")),
2292 false,
2293 &error));
2294 EXPECT_EQ("", error);
2295
2296 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2297 base::StringPrintf("[{\"id\": %d,"
2298 " \"danger\": {"
2299 " \"previous\":\"safe\","
2300 " \"current\":\"file\"},"
2301 " \"dangerAccepted\": {"
2302 " \"current\":false}}]",
2303 result_id)));
2304
2305 item->DangerousDownloadValidated();
2306 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2307 base::StringPrintf("[{\"id\": %d,"
2308 " \"dangerAccepted\": {"
2309 " \"previous\":false,"
2310 " \"current\":true}}]",
2311 result_id)));
2312 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2313 base::StringPrintf("[{\"id\": %d,"
2314 " \"filename\": {"
2315 " \"previous\": \"%s\","
2316 " \"current\": \"%s\"},"
2317 " \"state\": {"
2318 " \"previous\": \"in_progress\","
2319 " \"current\": \"complete\"}}]",
2320 result_id,
2321 GetFilename("overridden.swf.crdownload").c_str(),
2322 GetFilename("overridden.swf").c_str())));
2323 }
2324
2325 IN_PROC_BROWSER_TEST_F(
2326 DownloadExtensionTest,
2327 DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) {
2328 GoOnTheRecord();
2329 LoadExtension("downloads_split");
2330 AddFilenameDeterminer();
2331 CHECK(StartTestServer());
2332 std::string download_url = test_server()->GetURL("slow?0").spec();
2333
2334 // Start downloading a file.
2335 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2336 new DownloadsDownloadFunction(), base::StringPrintf(
2337 "[{\"url\": \"%s\"}]", download_url.c_str())));
2338 ASSERT_TRUE(result.get());
2339 int result_id = -1;
2340 ASSERT_TRUE(result->GetAsInteger(&result_id));
2341 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2342 ASSERT_TRUE(item);
2343 ScopedCancellingItem canceller(item);
2344 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2345
2346 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2347 base::StringPrintf("[{\"danger\": \"safe\","
2348 " \"incognito\": false,"
2349 " \"id\": %d,"
2350 " \"mime\": \"text/plain\","
2351 " \"paused\": false,"
2352 " \"url\": \"%s\"}]",
2353 result_id,
2354 download_url.c_str())));
2355 ASSERT_TRUE(WaitFor(
2356 events::kOnDownloadDeterminingFilename,
2357 base::StringPrintf("[{\"id\": %d,"
2358 " \"filename\":\"slow.txt\"}]",
2359 result_id)));
2360 ASSERT_TRUE(item->GetTargetFilePath().empty());
2361 ASSERT_TRUE(item->IsInProgress());
2362
2363 // Respond to the onDeterminingFilename.
2364 std::string error;
2365 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2366 browser()->profile(),
2367 false,
2368 GetExtensionId(),
2369 result_id,
2370 base::FilePath(FILE_PATH_LITERAL("sneaky/../../sneaky.txt")),
2371 false,
2372 &error));
2373 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError, error.c_str());
2374 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2375 base::StringPrintf("[{\"id\": %d,"
2376 " \"filename\": {"
2377 " \"previous\": \"%s\","
2378 " \"current\": \"%s\"},"
2379 " \"state\": {"
2380 " \"previous\": \"in_progress\","
2381 " \"current\": \"complete\"}}]",
2382 result_id,
2383 GetFilename("slow.txt.crdownload").c_str(),
2384 GetFilename("slow.txt").c_str())));
2385 }
2386
2387 IN_PROC_BROWSER_TEST_F(
2388 DownloadExtensionTest,
2389 DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) {
2390 GoOnTheRecord();
2391 LoadExtension("downloads_split");
2392 AddFilenameDeterminer();
2393 CHECK(StartTestServer());
2394 std::string download_url = test_server()->GetURL("slow?0").spec();
2395
2396 // Start downloading a file.
2397 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2398 new DownloadsDownloadFunction(), base::StringPrintf(
2399 "[{\"url\": \"%s\"}]", download_url.c_str())));
2400 ASSERT_TRUE(result.get());
2401 int result_id = -1;
2402 ASSERT_TRUE(result->GetAsInteger(&result_id));
2403 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2404 ASSERT_TRUE(item);
2405 ScopedCancellingItem canceller(item);
2406 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2407
2408 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2409 base::StringPrintf("[{\"danger\": \"safe\","
2410 " \"incognito\": false,"
2411 " \"id\": %d,"
2412 " \"mime\": \"text/plain\","
2413 " \"paused\": false,"
2414 " \"url\": \"%s\"}]",
2415 result_id,
2416 download_url.c_str())));
2417 ASSERT_TRUE(WaitFor(
2418 events::kOnDownloadDeterminingFilename,
2419 base::StringPrintf("[{\"id\": %d,"
2420 " \"filename\":\"slow.txt\"}]",
2421 result_id)));
2422 ASSERT_TRUE(item->GetTargetFilePath().empty());
2423 ASSERT_TRUE(item->IsInProgress());
2424
2425 // Respond to the onDeterminingFilename.
2426 std::string error;
2427 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2428 browser()->profile(),
2429 false,
2430 GetExtensionId(),
2431 result_id,
2432 base::FilePath(FILE_PATH_LITERAL(".")),
2433 false,
2434 &error));
2435 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError, error.c_str());
2436 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2437 base::StringPrintf("[{\"id\": %d,"
2438 " \"filename\": {"
2439 " \"previous\": \"%s\","
2440 " \"current\": \"%s\"},"
2441 " \"state\": {"
2442 " \"previous\": \"in_progress\","
2443 " \"current\": \"complete\"}}]",
2444 result_id,
2445 GetFilename("slow.txt.crdownload").c_str(),
2446 GetFilename("slow.txt").c_str())));
2447 }
2448
2449 IN_PROC_BROWSER_TEST_F(
2450 DownloadExtensionTest,
2451 DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) {
2452 CHECK(StartTestServer());
2453 GoOnTheRecord();
2454 LoadExtension("downloads_split");
2455 AddFilenameDeterminer();
2456 std::string download_url = test_server()->GetURL("slow?0").spec();
2457
2458 // Start downloading a file.
2459 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2460 new DownloadsDownloadFunction(), base::StringPrintf(
2461 "[{\"url\": \"%s\"}]", download_url.c_str())));
2462 ASSERT_TRUE(result.get());
2463 int result_id = -1;
2464 ASSERT_TRUE(result->GetAsInteger(&result_id));
2465 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2466 ASSERT_TRUE(item);
2467 ScopedCancellingItem canceller(item);
2468 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2469
2470 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2471 base::StringPrintf("[{\"danger\": \"safe\","
2472 " \"incognito\": false,"
2473 " \"id\": %d,"
2474 " \"mime\": \"text/plain\","
2475 " \"paused\": false,"
2476 " \"url\": \"%s\"}]",
2477 result_id,
2478 download_url.c_str())));
2479 ASSERT_TRUE(WaitFor(
2480 events::kOnDownloadDeterminingFilename,
2481 base::StringPrintf("[{\"id\": %d,"
2482 " \"filename\":\"slow.txt\"}]",
2483 result_id)));
2484 ASSERT_TRUE(item->GetTargetFilePath().empty());
2485 ASSERT_TRUE(item->IsInProgress());
2486
2487 // Respond to the onDeterminingFilename.
2488 std::string error;
2489 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2490 browser()->profile(),
2491 false,
2492 GetExtensionId(),
2493 result_id,
2494 base::FilePath(FILE_PATH_LITERAL("..")),
2495 false,
2496 &error));
2497 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError, error.c_str());
2498 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2499 base::StringPrintf("[{\"id\": %d,"
2500 " \"filename\": {"
2501 " \"previous\": \"%s\","
2502 " \"current\": \"%s\"},"
2503 " \"state\": {"
2504 " \"previous\": \"in_progress\","
2505 " \"current\": \"complete\"}}]",
2506 result_id,
2507 GetFilename("slow.txt.crdownload").c_str(),
2508 GetFilename("slow.txt").c_str())));
2509 }
2510
2511 IN_PROC_BROWSER_TEST_F(
2512 DownloadExtensionTest,
2513 DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) {
2514 GoOnTheRecord();
2515 LoadExtension("downloads_split");
2516 AddFilenameDeterminer();
2517 CHECK(StartTestServer());
2518 std::string download_url = test_server()->GetURL("slow?0").spec();
2519
2520 // Start downloading a file.
2521 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2522 new DownloadsDownloadFunction(), base::StringPrintf(
2523 "[{\"url\": \"%s\"}]", download_url.c_str())));
2524 ASSERT_TRUE(result.get());
2525 int result_id = -1;
2526 ASSERT_TRUE(result->GetAsInteger(&result_id));
2527 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2528 ASSERT_TRUE(item);
2529 ScopedCancellingItem canceller(item);
2530 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2531
2532 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2533 base::StringPrintf("[{\"danger\": \"safe\","
2534 " \"incognito\": false,"
2535 " \"id\": %d,"
2536 " \"mime\": \"text/plain\","
2537 " \"paused\": false,"
2538 " \"url\": \"%s\"}]",
2539 result_id,
2540 download_url.c_str())));
2541 ASSERT_TRUE(WaitFor(
2542 events::kOnDownloadDeterminingFilename,
2543 base::StringPrintf("[{\"id\": %d,"
2544 " \"filename\":\"slow.txt\"}]",
2545 result_id)));
2546 ASSERT_TRUE(item->GetTargetFilePath().empty());
2547 ASSERT_TRUE(item->IsInProgress());
2548
2549 // Respond to the onDeterminingFilename. Absolute paths should be rejected.
2550 std::string error;
2551 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2552 browser()->profile(),
2553 false,
2554 GetExtensionId(),
2555 result_id,
2556 downloads_directory().Append(FILE_PATH_LITERAL("sneaky.txt")),
2557 false,
2558 &error));
2559 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError, error.c_str());
2560
2561 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2562 base::StringPrintf("[{\"id\": %d,"
2563 " \"filename\": {"
2564 " \"previous\": \"%s\","
2565 " \"current\": \"%s\"},"
2566 " \"state\": {"
2567 " \"previous\": \"in_progress\","
2568 " \"current\": \"complete\"}}]",
2569 result_id,
2570 GetFilename("slow.txt.crdownload").c_str(),
2571 GetFilename("slow.txt").c_str())));
2572 }
2573
2574 IN_PROC_BROWSER_TEST_F(
2575 DownloadExtensionTest,
2576 DownloadExtensionTest_OnDeterminingFilename_EmptyBasenameInvalid) {
2577 GoOnTheRecord();
2578 LoadExtension("downloads_split");
2579 AddFilenameDeterminer();
2580 CHECK(StartTestServer());
2581 std::string download_url = test_server()->GetURL("slow?0").spec();
2582
2583 // Start downloading a file.
2584 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2585 new DownloadsDownloadFunction(), base::StringPrintf(
2586 "[{\"url\": \"%s\"}]", download_url.c_str())));
2587 ASSERT_TRUE(result.get());
2588 int result_id = -1;
2589 ASSERT_TRUE(result->GetAsInteger(&result_id));
2590 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2591 ASSERT_TRUE(item);
2592 ScopedCancellingItem canceller(item);
2593 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2594
2595 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2596 base::StringPrintf("[{\"danger\": \"safe\","
2597 " \"incognito\": false,"
2598 " \"id\": %d,"
2599 " \"mime\": \"text/plain\","
2600 " \"paused\": false,"
2601 " \"url\": \"%s\"}]",
2602 result_id,
2603 download_url.c_str())));
2604 ASSERT_TRUE(WaitFor(
2605 events::kOnDownloadDeterminingFilename,
2606 base::StringPrintf("[{\"id\": %d,"
2607 " \"filename\":\"slow.txt\"}]",
2608 result_id)));
2609 ASSERT_TRUE(item->GetTargetFilePath().empty());
2610 ASSERT_TRUE(item->IsInProgress());
2611
2612 // Respond to the onDeterminingFilename. Empty basenames should be rejected.
2613 std::string error;
2614 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2615 browser()->profile(),
2616 false,
2617 GetExtensionId(),
2618 result_id,
2619 base::FilePath(FILE_PATH_LITERAL("foo/")),
2620 false,
2621 &error));
2622 EXPECT_STREQ(download_extension_errors::kInvalidFilenameError, error.c_str());
2623
2624 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2625 base::StringPrintf("[{\"id\": %d,"
2626 " \"filename\": {"
2627 " \"previous\": \"%s\","
2628 " \"current\": \"%s\"},"
2629 " \"state\": {"
2630 " \"previous\": \"in_progress\","
2631 " \"current\": \"complete\"}}]",
2632 result_id,
2633 GetFilename("slow.txt.crdownload").c_str(),
2634 GetFilename("slow.txt").c_str())));
2635 }
2636
2637 IN_PROC_BROWSER_TEST_F(
2638 DownloadExtensionTest,
2639 DownloadExtensionTest_OnDeterminingFilename_Override) {
2640 GoOnTheRecord();
2641 LoadExtension("downloads_split");
2642 AddFilenameDeterminer();
2643 CHECK(StartTestServer());
2644 std::string download_url = test_server()->GetURL("slow?0").spec();
2645
2646 // Start downloading a file.
2647 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2648 new DownloadsDownloadFunction(), base::StringPrintf(
2649 "[{\"url\": \"%s\"}]", download_url.c_str())));
2650 ASSERT_TRUE(result.get());
2651 int result_id = -1;
2652 ASSERT_TRUE(result->GetAsInteger(&result_id));
2653 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2654 ASSERT_TRUE(item);
2655 ScopedCancellingItem canceller(item);
2656 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2657 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2658 base::StringPrintf("[{\"danger\": \"safe\","
2659 " \"incognito\": false,"
2660 " \"id\": %d,"
2661 " \"mime\": \"text/plain\","
2662 " \"paused\": false,"
2663 " \"url\": \"%s\"}]",
2664 result_id,
2665 download_url.c_str())));
2666 ASSERT_TRUE(WaitFor(
2667 events::kOnDownloadDeterminingFilename,
2668 base::StringPrintf("[{\"id\": %d,"
2669 " \"filename\":\"slow.txt\"}]",
2670 result_id)));
2671 ASSERT_TRUE(item->GetTargetFilePath().empty());
2672 ASSERT_TRUE(item->IsInProgress());
2673
2674 // Respond to the onDeterminingFilename.
2675 std::string error;
2676 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2677 browser()->profile(),
2678 false,
2679 GetExtensionId(),
2680 result_id,
2681 base::FilePath(),
2682 false,
2683 &error));
2684 EXPECT_EQ("", error);
2685
2686 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2687 base::StringPrintf("[{\"id\": %d,"
2688 " \"filename\": {"
2689 " \"previous\": \"%s\","
2690 " \"current\": \"%s\"},"
2691 " \"state\": {"
2692 " \"previous\": \"in_progress\","
2693 " \"current\": \"complete\"}}]",
2694 result_id,
2695 GetFilename("slow.txt.crdownload").c_str(),
2696 GetFilename("slow.txt").c_str())));
2697 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2698 base::StringPrintf("[{\"id\": %d,"
2699 " \"state\": {"
2700 " \"previous\": \"in_progress\","
2701 " \"current\": \"complete\"}}]",
2702 result_id)));
2703
2704 // Start downloading a file.
2705 result.reset(RunFunctionAndReturnResult(
2706 new DownloadsDownloadFunction(), base::StringPrintf(
2707 "[{\"url\": \"%s\"}]", download_url.c_str())));
2708 ASSERT_TRUE(result.get());
2709 result_id = -1;
2710 ASSERT_TRUE(result->GetAsInteger(&result_id));
2711 item = GetCurrentManager()->GetDownload(result_id);
2712 ASSERT_TRUE(item);
2713 ScopedCancellingItem canceller2(item);
2714 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2715
2716 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2717 base::StringPrintf("[{\"danger\": \"safe\","
2718 " \"incognito\": false,"
2719 " \"id\": %d,"
2720 " \"mime\": \"text/plain\","
2721 " \"paused\": false,"
2722 " \"url\": \"%s\"}]",
2723 result_id,
2724 download_url.c_str())));
2725 ASSERT_TRUE(WaitFor(
2726 events::kOnDownloadDeterminingFilename,
2727 base::StringPrintf("[{\"id\": %d,"
2728 " \"filename\":\"slow.txt\"}]",
2729 result_id)));
2730 ASSERT_TRUE(item->GetTargetFilePath().empty());
2731 ASSERT_TRUE(item->IsInProgress());
2732
2733 // Respond to the onDeterminingFilename.
2734 // Also test that DetermineFilename allows (chrome) extensions to set
2735 // filenames without (filename) extensions. (Don't ask about v8 extensions or
2736 // python extensions or kernel extensions or firefox extensions...)
2737 error = "";
2738 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2739 browser()->profile(),
2740 false,
2741 GetExtensionId(),
2742 result_id,
2743 base::FilePath(FILE_PATH_LITERAL("foo")),
2744 true,
2745 &error));
2746 EXPECT_EQ("", error);
2747
2748 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2749 base::StringPrintf("[{\"id\": %d,"
2750 " \"filename\": {"
2751 " \"previous\": \"%s\","
2752 " \"current\": \"%s\"},"
2753 " \"state\": {"
2754 " \"previous\": \"in_progress\","
2755 " \"current\": \"complete\"}}]",
2756 result_id,
2757 GetFilename("foo.crdownload").c_str(),
2758 GetFilename("foo").c_str())));
2759 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2760 base::StringPrintf("[{\"id\": %d,"
2761 " \"state\": {"
2762 " \"previous\": \"in_progress\","
2763 " \"current\": \"complete\"}}]",
2764 result_id)));
2765 }
2766
2767 // TODO test precedence rules: install_time
2768
2769 IN_PROC_BROWSER_TEST_F(
2770 DownloadExtensionTest,
2771 DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) {
2772 CHECK(StartTestServer());
2773 GoOnTheRecord();
2774 LoadExtension("downloads_split");
2775 content::RenderProcessHost* host = AddFilenameDeterminer();
2776 std::string download_url = test_server()->GetURL("slow?0").spec();
2777
2778 // Start downloading a file.
2779 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2780 new DownloadsDownloadFunction(), base::StringPrintf(
2781 "[{\"url\": \"%s\"}]", download_url.c_str())));
2782 ASSERT_TRUE(result.get());
2783 int result_id = -1;
2784 ASSERT_TRUE(result->GetAsInteger(&result_id));
2785 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2786 ASSERT_TRUE(item);
2787 ScopedCancellingItem canceller(item);
2788 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2789
2790 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2791 base::StringPrintf("[{\"danger\": \"safe\","
2792 " \"incognito\": false,"
2793 " \"id\": %d,"
2794 " \"mime\": \"text/plain\","
2795 " \"paused\": false,"
2796 " \"url\": \"%s\"}]",
2797 result_id,
2798 download_url.c_str())));
2799 ASSERT_TRUE(WaitFor(
2800 events::kOnDownloadDeterminingFilename,
2801 base::StringPrintf("[{\"id\": %d,"
2802 " \"filename\":\"slow.txt\"}]",
2803 result_id)));
2804 ASSERT_TRUE(item->GetTargetFilePath().empty());
2805 ASSERT_TRUE(item->IsInProgress());
2806
2807 // Remove a determiner while waiting for it.
2808 RemoveFilenameDeterminer(host);
2809
2810 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2811 base::StringPrintf("[{\"id\": %d,"
2812 " \"state\": {"
2813 " \"previous\": \"in_progress\","
2814 " \"current\": \"complete\"}}]",
2815 result_id)));
2816 }
2817
2818 IN_PROC_BROWSER_TEST_F(
2819 DownloadExtensionTest,
2820 DownloadExtensionTest_OnDeterminingFilename_IncognitoSplit) {
2821 LoadExtension("downloads_split");
2822 CHECK(StartTestServer());
2823 std::string download_url = test_server()->GetURL("slow?0").spec();
2824
2825 GoOnTheRecord();
2826 AddFilenameDeterminer();
2827
2828 GoOffTheRecord();
2829 AddFilenameDeterminer();
2830
2831 // Start an on-record download.
2832 GoOnTheRecord();
2833 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2834 new DownloadsDownloadFunction(), base::StringPrintf(
2835 "[{\"url\": \"%s\"}]", download_url.c_str())));
2836 ASSERT_TRUE(result.get());
2837 int result_id = -1;
2838 ASSERT_TRUE(result->GetAsInteger(&result_id));
2839 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2840 ASSERT_TRUE(item);
2841 ScopedCancellingItem canceller(item);
2842 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2843
2844 // Wait for the onCreated and onDeterminingFilename events.
2845 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2846 base::StringPrintf("[{\"danger\": \"safe\","
2847 " \"incognito\": false,"
2848 " \"id\": %d,"
2849 " \"mime\": \"text/plain\","
2850 " \"paused\": false,"
2851 " \"url\": \"%s\"}]",
2852 result_id,
2853 download_url.c_str())));
2854 ASSERT_TRUE(WaitFor(
2855 events::kOnDownloadDeterminingFilename,
2856 base::StringPrintf("[{\"id\": %d,"
2857 " \"incognito\": false,"
2858 " \"filename\":\"slow.txt\"}]",
2859 result_id)));
2860 ASSERT_TRUE(item->GetTargetFilePath().empty());
2861 ASSERT_TRUE(item->IsInProgress());
2862
2863 // Respond to the onDeterminingFilename events.
2864 std::string error;
2865 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2866 current_browser()->profile(),
2867 false,
2868 GetExtensionId(),
2869 result_id,
2870 base::FilePath(FILE_PATH_LITERAL("42.txt")),
2871 false,
2872 &error));
2873 EXPECT_EQ("", error);
2874
2875 // The download should complete successfully.
2876 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2877 base::StringPrintf("[{\"id\": %d,"
2878 " \"filename\": {"
2879 " \"previous\": \"%s\","
2880 " \"current\": \"%s\"},"
2881 " \"state\": {"
2882 " \"previous\": \"in_progress\","
2883 " \"current\": \"complete\"}}]",
2884 result_id,
2885 GetFilename("42.txt.crdownload").c_str(),
2886 GetFilename("42.txt").c_str())));
2887 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2888 base::StringPrintf("[{\"id\": %d,"
2889 " \"state\": {"
2890 " \"previous\": \"in_progress\","
2891 " \"current\": \"complete\"}}]",
2892 result_id)));
2893
2894 // Start an incognito download for comparison.
2895 GoOffTheRecord();
2896 result.reset(RunFunctionAndReturnResult(
2897 new DownloadsDownloadFunction(), base::StringPrintf(
2898 "[{\"url\": \"%s\"}]", download_url.c_str())));
2899 ASSERT_TRUE(result.get());
2900 result_id = -1;
2901 ASSERT_TRUE(result->GetAsInteger(&result_id));
2902 item = GetCurrentManager()->GetDownload(result_id);
2903 ASSERT_TRUE(item);
2904 ScopedCancellingItem canceller2(item);
2905 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2906
2907 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2908 base::StringPrintf("[{\"danger\": \"safe\","
2909 " \"incognito\": true,"
2910 " \"id\": %d,"
2911 " \"mime\": \"text/plain\","
2912 " \"paused\": false,"
2913 " \"url\": \"%s\"}]",
2914 result_id,
2915 download_url.c_str())));
2916 // On-Record renderers should not see events for off-record items.
2917 ASSERT_TRUE(WaitFor(
2918 events::kOnDownloadDeterminingFilename,
2919 base::StringPrintf("[{\"id\": %d,"
2920 " \"incognito\": true,"
2921 " \"filename\":\"slow.txt\"}]",
2922 result_id)));
2923 ASSERT_TRUE(item->GetTargetFilePath().empty());
2924 ASSERT_TRUE(item->IsInProgress());
2925
2926 // Respond to the onDeterminingFilename.
2927 error = "";
2928 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2929 current_browser()->profile(),
2930 false,
2931 GetExtensionId(),
2932 result_id,
2933 base::FilePath(FILE_PATH_LITERAL("5.txt")),
2934 false,
2935 &error));
2936 EXPECT_EQ("", error);
2937
2938 // The download should complete successfully.
2939 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2940 base::StringPrintf("[{\"id\": %d,"
2941 " \"filename\": {"
2942 " \"previous\": \"%s\","
2943 " \"current\": \"%s\"},"
2944 " \"state\": {"
2945 " \"previous\": \"in_progress\","
2946 " \"current\": \"complete\"}}]",
2947 result_id,
2948 GetFilename("5.txt.crdownload").c_str(),
2949 GetFilename("5.txt").c_str())));
2950 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2951 base::StringPrintf("[{\"id\": %d,"
2952 " \"state\": {"
2953 " \"previous\": \"in_progress\","
2954 " \"current\": \"complete\"}}]",
2955 result_id)));
2956 }
2957
2958 IN_PROC_BROWSER_TEST_F(
2959 DownloadExtensionTest,
2960 DownloadExtensionTest_OnDeterminingFilename_IncognitoSpanning) {
2961 LoadExtension("downloads_spanning");
2962 CHECK(StartTestServer());
2963 std::string download_url = test_server()->GetURL("slow?0").spec();
2964
2965 GoOnTheRecord();
2966 AddFilenameDeterminer();
2967
2968 // There is a single extension renderer that sees both on-record and
2969 // off-record events. The extension functions see the on-record profile with
2970 // include_incognito=true.
2971
2972 // Start an on-record download.
2973 GoOnTheRecord();
2974 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2975 new DownloadsDownloadFunction(), base::StringPrintf(
2976 "[{\"url\": \"%s\"}]", download_url.c_str())));
2977 ASSERT_TRUE(result.get());
2978 int result_id = -1;
2979 ASSERT_TRUE(result->GetAsInteger(&result_id));
2980 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2981 ASSERT_TRUE(item);
2982 ScopedCancellingItem canceller(item);
2983 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2984
2985 // Wait for the onCreated and onDeterminingFilename events.
2986 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2987 base::StringPrintf("[{\"danger\": \"safe\","
2988 " \"incognito\": false,"
2989 " \"id\": %d,"
2990 " \"mime\": \"text/plain\","
2991 " \"paused\": false,"
2992 " \"url\": \"%s\"}]",
2993 result_id,
2994 download_url.c_str())));
2995 ASSERT_TRUE(WaitFor(
2996 events::kOnDownloadDeterminingFilename,
2997 base::StringPrintf("[{\"id\": %d,"
2998 " \"incognito\": false,"
2999 " \"filename\":\"slow.txt\"}]",
3000 result_id)));
3001 ASSERT_TRUE(item->GetTargetFilePath().empty());
3002 ASSERT_TRUE(item->IsInProgress());
3003
3004 // Respond to the onDeterminingFilename events.
3005 std::string error;
3006 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3007 current_browser()->profile(),
3008 true,
3009 GetExtensionId(),
3010 result_id,
3011 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3012 false,
3013 &error));
3014 EXPECT_EQ("", error);
3015
3016 // The download should complete successfully.
3017 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3018 base::StringPrintf("[{\"id\": %d,"
3019 " \"filename\": {"
3020 " \"previous\": \"%s\","
3021 " \"current\": \"%s\"},"
3022 " \"state\": {"
3023 " \"previous\": \"in_progress\","
3024 " \"current\": \"complete\"}}]",
3025 result_id,
3026 GetFilename("42.txt.crdownload").c_str(),
3027 GetFilename("42.txt").c_str())));
3028 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3029 base::StringPrintf("[{\"id\": %d,"
3030 " \"state\": {"
3031 " \"previous\": \"in_progress\","
3032 " \"current\": \"complete\"}}]",
3033 result_id)));
3034
3035 // Start an incognito download for comparison.
3036 GoOffTheRecord();
3037 result.reset(RunFunctionAndReturnResult(
3038 new DownloadsDownloadFunction(), base::StringPrintf(
3039 "[{\"url\": \"%s\"}]", download_url.c_str())));
3040 ASSERT_TRUE(result.get());
3041 result_id = -1;
3042 ASSERT_TRUE(result->GetAsInteger(&result_id));
3043 item = GetCurrentManager()->GetDownload(result_id);
3044 ASSERT_TRUE(item);
3045 ScopedCancellingItem canceller2(item);
3046 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3047
3048 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3049 base::StringPrintf("[{\"danger\": \"safe\","
3050 " \"incognito\": true,"
3051 " \"id\": %d,"
3052 " \"mime\": \"text/plain\","
3053 " \"paused\": false,"
3054 " \"url\": \"%s\"}]",
3055 result_id,
3056 download_url.c_str())));
3057 ASSERT_TRUE(WaitFor(
3058 events::kOnDownloadDeterminingFilename,
3059 base::StringPrintf("[{\"id\": %d,"
3060 " \"incognito\": true,"
3061 " \"filename\":\"slow.txt\"}]",
3062 result_id)));
3063 ASSERT_TRUE(item->GetTargetFilePath().empty());
3064 ASSERT_TRUE(item->IsInProgress());
3065
3066 // Respond to the onDeterminingFilename.
3067 error = "";
3068 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3069 current_browser()->profile(),
3070 true,
3071 GetExtensionId(),
3072 result_id,
3073 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3074 false,
3075 &error));
3076 EXPECT_EQ("", error);
3077
3078 // The download should complete successfully.
3079 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3080 base::StringPrintf("[{\"id\": %d,"
3081 " \"filename\": {"
3082 " \"previous\": \"%s\","
3083 " \"current\": \"%s\"},"
3084 " \"state\": {"
3085 " \"previous\": \"in_progress\","
3086 " \"current\": \"complete\"}}]",
3087 result_id,
3088 GetFilename("42 (1).txt.crdownload").c_str(),
3089 GetFilename("42 (1).txt").c_str())));
3090 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3091 base::StringPrintf("[{\"id\": %d,"
3092 " \"state\": {"
3093 " \"previous\": \"in_progress\","
3094 " \"current\": \"complete\"}}]",
3095 result_id)));
3096 }
3097
3098 // Test download interruption while extensions determining filename, re-run
3099 // through fan-out and fan-in.
3100 // TODO(rdsmith): FILE_OPERATION_INITIALIZE is not right for this test.
3101 IN_PROC_BROWSER_TEST_F(
3102 DownloadExtensionTest,
3103 FAILS_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) {
3104 LoadExtension("downloads_split");
3105 CHECK(StartTestServer());
3106 std::string download_url = test_server()->GetURL("slow?0").spec();
3107 GoOnTheRecord();
3108 AddFilenameDeterminer();
3109
3110 // TODO Interrupt the download instead of responding to onDeterminingFilename.
3111 scoped_refptr<content::TestFileErrorInjector> injector(
3112 content::TestFileErrorInjector::Create(
3113 GetCurrentManager()));
3114 content::TestFileErrorInjector::FileErrorInfo error_info = {
3115 download_url,
3116 content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE,
3117 0,
3118 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
3119 injector->AddError(error_info);
3120 injector->InjectErrors();
3121
3122 // Start a download.
3123 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
3124 new DownloadsDownloadFunction(), base::StringPrintf(
3125 "[{\"url\": \"%s\"}]", download_url.c_str())));
3126 ASSERT_TRUE(result.get());
3127 int result_id = -1;
3128 ASSERT_TRUE(result->GetAsInteger(&result_id));
3129 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
3130 ASSERT_TRUE(item);
3131 ScopedCancellingItem canceller(item);
3132 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3133
3134 // Wait for the onCreated and onDeterminingFilename event.
3135 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3136 base::StringPrintf("[{\"danger\": \"safe\","
3137 " \"incognito\": false,"
3138 " \"id\": %d,"
3139 " \"mime\": \"text/plain\","
3140 " \"paused\": false,"
3141 " \"url\": \"%s\"}]",
3142 result_id,
3143 download_url.c_str())));
3144 ASSERT_TRUE(WaitFor(
3145 events::kOnDownloadDeterminingFilename,
3146 base::StringPrintf("[{\"id\": %d,"
3147 " \"incognito\": false,"
3148 " \"filename\":\"slow.txt\"}]",
3149 result_id)));
3150 ASSERT_TRUE(item->GetTargetFilePath().empty());
3151 ASSERT_TRUE(item->IsInProgress());
3152
3153 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3154 base::StringPrintf("[{\"id\": %d,"
3155 " \"state\": {"
3156 " \"previous\": \"in_progress\","
3157 " \"current\": \"interrupted\"}}]",
3158 result_id)));
3159 ASSERT_TRUE(item->IsInterrupted());
3160 item->ResumeInterruptedDownload();
3161
3162 // Wait for and respond to the onDeterminingFilename event.
3163 ASSERT_TRUE(WaitFor(
3164 events::kOnDownloadDeterminingFilename,
3165 base::StringPrintf("[{\"id\": %d,"
3166 " \"incognito\": false,"
3167 " \"filename\":\"slow.txt\"}]",
3168 result_id)));
3169 ASSERT_TRUE(item->GetTargetFilePath().empty());
3170 ASSERT_TRUE(item->IsInProgress());
3171 std::string error;
3172 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3173 current_browser()->profile(),
3174 false,
3175 GetExtensionId(),
3176 result_id,
3177 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3178 false,
3179 &error));
3180 EXPECT_EQ("", error);
3181
3182 // The download should complete successfully.
3183 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3184 base::StringPrintf("[{\"id\": %d,"
3185 " \"filename\": {"
3186 " \"previous\": \"%s\","
3187 " \"current\": \"%s\"},"
3188 " \"state\": {"
3189 " \"previous\": \"in_progress\","
3190 " \"current\": \"complete\"}}]",
3191 result_id,
3192 GetFilename("42.txt.crdownload").c_str(),
3193 GetFilename("42.txt").c_str())));
3194 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3195 base::StringPrintf("[{\"id\": %d,"
3196 " \"state\": {"
3197 " \"previous\": \"in_progress\","
3198 " \"current\": \"complete\"}}]",
3199 result_id)));
3200 }
3201
3202 // TODO(benjhayden) Figure out why DisableExtension() does not fire
3203 // OnListenerRemoved.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698