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

Side by Side Diff: chrome/browser/unload_browsertest.cc

Issue 2702503002: Block renderer-initiated main frame navigations to data URLs (Closed)
Patch Set: nasko comments, fix most tests Created 3 years, 8 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
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 #if defined(OS_POSIX) 5 #if defined(OS_POSIX)
6 #include <signal.h> 6 #include <signal.h>
7 #endif 7 #endif
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 21 matching lines...) Expand all
32 #include "net/url_request/url_request_test_util.h" 32 #include "net/url_request/url_request_test_util.h"
33 33
34 #if defined(OS_WIN) 34 #if defined(OS_WIN)
35 // For version specific disabled tests below (http://crbug.com/267597). 35 // For version specific disabled tests below (http://crbug.com/267597).
36 #include "base/win/windows_version.h" 36 #include "base/win/windows_version.h"
37 #endif 37 #endif
38 38
39 using base::TimeDelta; 39 using base::TimeDelta;
40 using content::BrowserThread; 40 using content::BrowserThread;
41 41
42 const std::string NOLISTENERS_HTML = 42 const char NOLISTENERS_HTML[] =
43 "<html><head><title>nolisteners</title></head><body></body></html>"; 43 "<html><head><title>nolisteners</title></head><body></body></html>";
44 44
45 const std::string UNLOAD_HTML = 45 const char UNLOAD_HTML[] =
46 "<html><head><title>unload</title></head><body>" 46 "<html><head><title>unload</title></head><body>"
47 "<script>window.onunload=function(e){}</script></body></html>"; 47 "<script>window.onunload=function(e){}</script></body></html>";
48 48
49 const std::string BEFORE_UNLOAD_HTML = 49 const char BEFORE_UNLOAD_HTML[] =
50 "<html><head><title>beforeunload</title></head><body>" 50 "<html><head><title>beforeunload</title></head><body>"
51 "<script>window.onbeforeunload=function(e){" 51 "<script>window.onbeforeunload=function(e){"
52 "setTimeout('document.title=\"cancelled\"', 0);return 'foo'}</script>" 52 "setTimeout('document.title=\"cancelled\"', 0);return 'foo'}</script>"
53 "</body></html>"; 53 "</body></html>";
54 54
55 const std::string INNER_FRAME_WITH_FOCUS_HTML = 55 const char INNER_FRAME_WITH_FOCUS_HTML[] =
56 "<html><head><title>innerframewithfocus</title></head><body>" 56 "<html><head><title>innerframewithfocus</title></head><body>"
57 "<script>window.onbeforeunload=function(e){return 'foo'}</script>" 57 "<script>window.onbeforeunload=function(e){return 'foo'}</script>"
58 "<iframe src=\"data:text/html,<html><head><script>window.onload=" 58 "<iframe src=\"data:text/html,<html><head><script>window.onload="
59 "function(){document.getElementById('box').focus()}</script>" 59 "function(){document.getElementById('box').focus()}</script>"
60 "<body><input id='box'></input></body></html>\"></iframe>" 60 "<body><input id='box'></input></body></html>\"></iframe>"
61 "</body></html>"; 61 "</body></html>";
62 62
63 const std::string INFINITE_UNLOAD_HTML = 63 const char INFINITE_UNLOAD_HTML[] =
64 "<html><head><title>infiniteunload</title></head><body>" 64 "<html><head><title>infiniteunload</title></head><body>"
65 "<script>window.onunload=function(e){while(true){}}</script>" 65 "<script>window.onunload=function(e){while(true){}}</script>"
66 "</body></html>"; 66 "</body></html>";
67 67
68 const std::string INFINITE_BEFORE_UNLOAD_HTML = 68 const char INFINITE_BEFORE_UNLOAD_HTML[] =
69 "<html><head><title>infinitebeforeunload</title></head><body>" 69 "<html><head><title>infinitebeforeunload</title></head><body>"
70 "<script>window.onbeforeunload=function(e){while(true){}}</script>" 70 "<script>window.onbeforeunload=function(e){while(true){}}</script>"
71 "</body></html>"; 71 "</body></html>";
72 72
73 const std::string INFINITE_UNLOAD_ALERT_HTML = 73 const char INFINITE_UNLOAD_ALERT_HTML[] =
74 "<html><head><title>infiniteunloadalert</title></head><body>" 74 "<html><head><title>infiniteunloadalert</title></head><body>"
75 "<script>window.onunload=function(e){" 75 "<script>window.onunload=function(e){"
76 "while(true){}" 76 "while(true){}"
77 "alert('foo');" 77 "alert('foo');"
78 "}</script></body></html>"; 78 "}</script></body></html>";
79 79
80 const std::string INFINITE_BEFORE_UNLOAD_ALERT_HTML = 80 const char INFINITE_BEFORE_UNLOAD_ALERT_HTML[] =
81 "<html><head><title>infinitebeforeunloadalert</title></head><body>" 81 "<html><head><title>infinitebeforeunloadalert</title></head><body>"
82 "<script>window.onbeforeunload=function(e){" 82 "<script>window.onbeforeunload=function(e){"
83 "while(true){}" 83 "while(true){}"
84 "alert('foo');" 84 "alert('foo');"
85 "}</script></body></html>"; 85 "}</script></body></html>";
86 86
87 const std::string TWO_SECOND_UNLOAD_ALERT_HTML = 87 const char TWO_SECOND_UNLOAD_ALERT_HTML[] =
88 "<html><head><title>twosecondunloadalert</title></head><body>" 88 "<html><head><title>twosecondunloadalert</title></head><body>"
89 "<script>window.onunload=function(e){" 89 "<script>window.onunload=function(e){"
90 "var start = new Date().getTime();" 90 "var start = new Date().getTime();"
91 "while(new Date().getTime() - start < 2000){}" 91 "while(new Date().getTime() - start < 2000){}"
92 "alert('foo');" 92 "alert('foo');"
93 "}</script></body></html>"; 93 "}</script></body></html>";
94 94
95 const std::string TWO_SECOND_BEFORE_UNLOAD_ALERT_HTML = 95 const char TWO_SECOND_BEFORE_UNLOAD_ALERT_HTML[] =
96 "<html><head><title>twosecondbeforeunloadalert</title></head><body>" 96 "<html><head><title>twosecondbeforeunloadalert</title></head><body>"
97 "<script>window.onbeforeunload=function(e){" 97 "<script>window.onbeforeunload=function(e){"
98 "var start = new Date().getTime();" 98 "var start = new Date().getTime();"
99 "while(new Date().getTime() - start < 2000){}" 99 "while(new Date().getTime() - start < 2000){}"
100 "alert('foo');" 100 "alert('foo');"
101 "}</script></body></html>"; 101 "}</script></body></html>";
102 102
103 const std::string CLOSE_TAB_WHEN_OTHER_TAB_HAS_LISTENER = 103 const char CLOSE_TAB_WHEN_OTHER_TAB_HAS_LISTENER[] =
104 "<html><head><title>only_one_unload</title></head>" 104 "<html><head><title>only_one_unload</title></head>"
105 "<body onclick=\"window.open('data:text/html," 105 "<script>"
106 "<html><head><title>popup</title></head></body>')\" " 106 "function openPopup() {"
107 "onbeforeunload='return;'>" 107 " var w = window.open('about:blank');"
108 " w.document.write('<html><head><title>popup</title></head></body>');"
109 "}"
110 "</script>"
111 "<body onclick='openPopup()' onbeforeunload='return;'>"
108 "</body></html>"; 112 "</body></html>";
109 113
110 class UnloadResults { 114 class UnloadResults {
111 public: 115 public:
112 UnloadResults() : successes_(0), aborts_(0) {} 116 UnloadResults() : successes_(0), aborts_(0) {}
113 117
114 void AddSuccess(const base::FilePath&) { successes_++; } 118 void AddSuccess(const base::FilePath&) { successes_++; }
115 void AddAbort(const base::FilePath&) { aborts_++; } 119 void AddAbort(const base::FilePath&) { aborts_++; }
116 void AddError(const base::FilePath&) { 120 void AddError(const base::FilePath&) {
117 ADD_FAILURE() << "AddError should not be called."; 121 ADD_FAILURE() << "AddError should not be called.";
(...skipping 27 matching lines...) Expand all
145 BrowserThread::IO, FROM_HERE, 149 BrowserThread::IO, FROM_HERE,
146 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 150 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
147 } 151 }
148 152
149 void CheckTitle(const char* expected_title) { 153 void CheckTitle(const char* expected_title) {
150 base::string16 expected = base::ASCIIToUTF16(expected_title); 154 base::string16 expected = base::ASCIIToUTF16(expected_title);
151 EXPECT_EQ(expected, 155 EXPECT_EQ(expected,
152 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); 156 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
153 } 157 }
154 158
155 void NavigateToDataURL(const std::string& html_content, 159 void NavigateToDataURL(const char* html_content, const char* expected_title) {
156 const char* expected_title) { 160 ui_test_utils::NavigateToURL(
157 ui_test_utils::NavigateToURL(browser(), 161 browser(), GURL(std::string("data:text/html,") + html_content));
158 GURL("data:text/html," + html_content));
159 CheckTitle(expected_title); 162 CheckTitle(expected_title);
160 } 163 }
161 164
162 void NavigateToNolistenersFileTwice() { 165 void NavigateToNolistenersFileTwice() {
163 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("title2.html")); 166 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("title2.html"));
164 ui_test_utils::NavigateToURL(browser(), url); 167 ui_test_utils::NavigateToURL(browser(), url);
165 CheckTitle("Title Of Awesomeness"); 168 CheckTitle("Title Of Awesomeness");
166 ui_test_utils::NavigateToURL(browser(), url); 169 ui_test_utils::NavigateToURL(browser(), url);
167 CheckTitle("Title Of Awesomeness"); 170 CheckTitle("Title Of Awesomeness");
168 } 171 }
169 172
170 // Navigates to a URL asynchronously, then again synchronously. The first 173 // Navigates to a URL asynchronously, then again synchronously. The first
171 // load is purposely async to test the case where the user loads another 174 // load is purposely async to test the case where the user loads another
172 // page without waiting for the first load to complete. 175 // page without waiting for the first load to complete.
173 void NavigateToNolistenersFileTwiceAsync() { 176 void NavigateToNolistenersFileTwiceAsync() {
174 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("title2.html")); 177 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("title2.html"));
175 ui_test_utils::NavigateToURLWithDisposition( 178 ui_test_utils::NavigateToURLWithDisposition(
176 browser(), url, WindowOpenDisposition::CURRENT_TAB, 0); 179 browser(), url, WindowOpenDisposition::CURRENT_TAB, 0);
177 ui_test_utils::NavigateToURL(browser(), url); 180 ui_test_utils::NavigateToURL(browser(), url);
178 CheckTitle("Title Of Awesomeness"); 181 CheckTitle("Title Of Awesomeness");
179 } 182 }
180 183
181 void LoadUrlAndQuitBrowser(const std::string& html_content, 184 void LoadUrlAndQuitBrowser(const char* html_content,
182 const char* expected_title) { 185 const char* expected_title) {
183 NavigateToDataURL(html_content, expected_title); 186 NavigateToDataURL(html_content, expected_title);
184 content::WindowedNotificationObserver window_observer( 187 content::WindowedNotificationObserver window_observer(
185 chrome::NOTIFICATION_BROWSER_CLOSED, 188 chrome::NOTIFICATION_BROWSER_CLOSED,
186 content::NotificationService::AllSources()); 189 content::NotificationService::AllSources());
187 chrome::CloseWindow(browser()); 190 chrome::CloseWindow(browser());
188 window_observer.Wait(); 191 window_observer.Wait();
189 } 192 }
190 193
191 // If |accept| is true, simulates user clicking OK, otherwise simulates 194 // If |accept| is true, simulates user clicking OK, otherwise simulates
(...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), 909 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)),
907 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), 910 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)),
908 true); 911 true);
909 window_observer.Wait(); 912 window_observer.Wait();
910 EXPECT_EQ(1, unload_results.get_successes()); 913 EXPECT_EQ(1, unload_results.get_successes());
911 EXPECT_EQ(0, unload_results.get_aborts()); 914 EXPECT_EQ(0, unload_results.get_aborts());
912 } 915 }
913 916
914 // TODO(ojan): Add tests for unload/beforeunload that have multiple tabs 917 // TODO(ojan): Add tests for unload/beforeunload that have multiple tabs
915 // and multiple windows. 918 // and multiple windows.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698