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

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

Powered by Google App Engine
This is Rietveld 408576698