OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <queue> | 5 #include <queue> |
| 6 #include <set> |
6 #include <utility> | 7 #include <utility> |
7 | 8 |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
8 #include "base/location.h" | 11 #include "base/location.h" |
9 #include "base/macros.h" | 12 #include "base/macros.h" |
10 #include "base/path_service.h" | 13 #include "base/path_service.h" |
11 #include "base/process/process.h" | 14 #include "base/process/process.h" |
12 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
15 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
16 #include "build/build_config.h" | 19 #include "build/build_config.h" |
17 #include "chrome/app/chrome_command_ids.h" | 20 #include "chrome/app/chrome_command_ids.h" |
18 #include "chrome/browser/apps/app_browsertest_util.h" | 21 #include "chrome/browser/apps/app_browsertest_util.h" |
19 #include "chrome/browser/chrome_content_browser_client.h" | 22 #include "chrome/browser/chrome_content_browser_client.h" |
20 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 24 #include "chrome/browser/download/download_prefs.h" |
21 #include "chrome/browser/lifetime/application_lifetime.h" | 25 #include "chrome/browser/lifetime/application_lifetime.h" |
22 #include "chrome/browser/pdf/pdf_extension_test_util.h" | 26 #include "chrome/browser/pdf/pdf_extension_test_util.h" |
23 #include "chrome/browser/prerender/prerender_link_manager.h" | 27 #include "chrome/browser/prerender/prerender_link_manager.h" |
24 #include "chrome/browser/prerender/prerender_link_manager_factory.h" | 28 #include "chrome/browser/prerender/prerender_link_manager_factory.h" |
25 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 30 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
27 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" | 31 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
28 #include "chrome/browser/task_management/task_management_browsertest_util.h" | 32 #include "chrome/browser/task_management/task_management_browsertest_util.h" |
29 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 33 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
30 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
(...skipping 10 matching lines...) Expand all Loading... |
41 #include "content/public/browser/interstitial_page_delegate.h" | 45 #include "content/public/browser/interstitial_page_delegate.h" |
42 #include "content/public/browser/notification_service.h" | 46 #include "content/public/browser/notification_service.h" |
43 #include "content/public/browser/render_process_host.h" | 47 #include "content/public/browser/render_process_host.h" |
44 #include "content/public/browser/render_widget_host.h" | 48 #include "content/public/browser/render_widget_host.h" |
45 #include "content/public/browser/render_widget_host_view.h" | 49 #include "content/public/browser/render_widget_host_view.h" |
46 #include "content/public/browser/web_contents_delegate.h" | 50 #include "content/public/browser/web_contents_delegate.h" |
47 #include "content/public/common/browser_plugin_guest_mode.h" | 51 #include "content/public/common/browser_plugin_guest_mode.h" |
48 #include "content/public/common/child_process_host.h" | 52 #include "content/public/common/child_process_host.h" |
49 #include "content/public/common/content_switches.h" | 53 #include "content/public/common/content_switches.h" |
50 #include "content/public/test/browser_test_utils.h" | 54 #include "content/public/test/browser_test_utils.h" |
| 55 #include "content/public/test/download_test_observer.h" |
51 #include "content/public/test/fake_speech_recognition_manager.h" | 56 #include "content/public/test/fake_speech_recognition_manager.h" |
52 #include "content/public/test/test_renderer_host.h" | 57 #include "content/public/test/test_renderer_host.h" |
53 #include "extensions/browser/api/declarative/rules_registry.h" | 58 #include "extensions/browser/api/declarative/rules_registry.h" |
54 #include "extensions/browser/api/declarative/rules_registry_service.h" | 59 #include "extensions/browser/api/declarative/rules_registry_service.h" |
55 #include "extensions/browser/api/declarative/test_rules_registry.h" | 60 #include "extensions/browser/api/declarative/test_rules_registry.h" |
56 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 61 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
57 #include "extensions/browser/api/extensions_api_client.h" | 62 #include "extensions/browser/api/extensions_api_client.h" |
58 #include "extensions/browser/app_window/native_app_window.h" | 63 #include "extensions/browser/app_window/native_app_window.h" |
59 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 64 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
60 #include "extensions/common/extension.h" | 65 #include "extensions/common/extension.h" |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 decision_made_(false), | 307 decision_made_(false), |
303 last_download_allowed_(false) {} | 308 last_download_allowed_(false) {} |
304 ~MockDownloadWebContentsDelegate() override {} | 309 ~MockDownloadWebContentsDelegate() override {} |
305 | 310 |
306 void CanDownload(const GURL& url, | 311 void CanDownload(const GURL& url, |
307 const std::string& request_method, | 312 const std::string& request_method, |
308 const base::Callback<void(bool)>& callback) override { | 313 const base::Callback<void(bool)>& callback) override { |
309 orig_delegate_->CanDownload( | 314 orig_delegate_->CanDownload( |
310 url, request_method, | 315 url, request_method, |
311 base::Bind(&MockDownloadWebContentsDelegate::DownloadDecided, | 316 base::Bind(&MockDownloadWebContentsDelegate::DownloadDecided, |
312 base::Unretained(this))); | 317 base::Unretained(this), callback)); |
313 } | 318 } |
314 | 319 |
315 void WaitForCanDownload(bool expect_allow) { | 320 void WaitForCanDownload(bool expect_allow) { |
316 EXPECT_FALSE(waiting_for_decision_); | 321 EXPECT_FALSE(waiting_for_decision_); |
317 waiting_for_decision_ = true; | 322 waiting_for_decision_ = true; |
318 | 323 |
319 if (decision_made_) { | 324 if (decision_made_) { |
320 EXPECT_EQ(expect_allow, last_download_allowed_); | 325 EXPECT_EQ(expect_allow, last_download_allowed_); |
321 return; | 326 return; |
322 } | 327 } |
323 | 328 |
324 expect_allow_ = expect_allow; | 329 expect_allow_ = expect_allow; |
325 message_loop_runner_ = new content::MessageLoopRunner; | 330 message_loop_runner_ = new content::MessageLoopRunner; |
326 message_loop_runner_->Run(); | 331 message_loop_runner_->Run(); |
327 } | 332 } |
328 | 333 |
329 void DownloadDecided(bool allow) { | 334 void DownloadDecided(const base::Callback<void(bool)>& callback, bool allow) { |
330 EXPECT_FALSE(decision_made_); | 335 EXPECT_FALSE(decision_made_); |
331 decision_made_ = true; | 336 decision_made_ = true; |
332 | 337 |
333 if (waiting_for_decision_) { | 338 if (waiting_for_decision_) { |
334 EXPECT_EQ(expect_allow_, allow); | 339 EXPECT_EQ(expect_allow_, allow); |
335 if (message_loop_runner_.get()) | 340 if (message_loop_runner_.get()) |
336 message_loop_runner_->Quit(); | 341 message_loop_runner_->Quit(); |
| 342 callback.Run(allow); |
337 return; | 343 return; |
338 } | 344 } |
339 last_download_allowed_ = allow; | 345 last_download_allowed_ = allow; |
| 346 callback.Run(allow); |
340 } | 347 } |
341 | 348 |
342 void Reset() { | 349 void Reset() { |
343 waiting_for_decision_ = false; | 350 waiting_for_decision_ = false; |
344 decision_made_ = false; | 351 decision_made_ = false; |
345 } | 352 } |
346 | 353 |
347 private: | 354 private: |
348 content::WebContentsDelegate* orig_delegate_; | 355 content::WebContentsDelegate* orig_delegate_; |
349 bool waiting_for_decision_; | 356 bool waiting_for_decision_; |
(...skipping 1854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2204 << message_; | 2211 << message_; |
2205 } | 2212 } |
2206 | 2213 |
2207 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) { | 2214 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) { |
2208 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. | 2215 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
2209 content::WebContents* guest_web_contents = | 2216 content::WebContents* guest_web_contents = |
2210 LoadGuest("/extensions/platform_apps/web_view/download/guest.html", | 2217 LoadGuest("/extensions/platform_apps/web_view/download/guest.html", |
2211 "web_view/download"); | 2218 "web_view/download"); |
2212 ASSERT_TRUE(guest_web_contents); | 2219 ASSERT_TRUE(guest_web_contents); |
2213 | 2220 |
| 2221 base::ScopedTempDir temporary_download_dir; |
| 2222 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); |
| 2223 DownloadPrefs::FromBrowserContext(guest_web_contents->GetBrowserContext()) |
| 2224 ->SetDownloadPath(temporary_download_dir.path()); |
| 2225 |
| 2226 std::unique_ptr<content::DownloadTestObserver> completion_observer( |
| 2227 new content::DownloadTestObserverTerminal( |
| 2228 content::BrowserContext::GetDownloadManager( |
| 2229 guest_web_contents->GetBrowserContext()), |
| 2230 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2231 |
2214 // Replace WebContentsDelegate with mock version so we can intercept download | 2232 // Replace WebContentsDelegate with mock version so we can intercept download |
2215 // requests. | 2233 // requests. |
2216 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate(); | 2234 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate(); |
2217 std::unique_ptr<MockDownloadWebContentsDelegate> mock_delegate( | 2235 std::unique_ptr<MockDownloadWebContentsDelegate> mock_delegate( |
2218 new MockDownloadWebContentsDelegate(delegate)); | 2236 new MockDownloadWebContentsDelegate(delegate)); |
2219 guest_web_contents->SetDelegate(mock_delegate.get()); | 2237 guest_web_contents->SetDelegate(mock_delegate.get()); |
2220 | 2238 |
2221 // Start test. | 2239 // Start test. |
2222 // 1. Guest requests a download that its embedder denies. | 2240 // 1. Guest requests a download that its embedder denies. |
2223 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2241 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2224 "startDownload('download-link-1')")); | 2242 "startDownload('download-link-1')")); |
2225 mock_delegate->WaitForCanDownload(false); // Expect to not allow. | 2243 mock_delegate->WaitForCanDownload(false); // Expect to not allow. |
2226 mock_delegate->Reset(); | 2244 mock_delegate->Reset(); |
2227 | 2245 |
2228 // 2. Guest requests a download that its embedder allows. | 2246 // 2. Guest requests a download that its embedder allows. |
2229 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2247 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2230 "startDownload('download-link-2')")); | 2248 "startDownload('download-link-2')")); |
2231 mock_delegate->WaitForCanDownload(true); // Expect to allow. | 2249 mock_delegate->WaitForCanDownload(true); // Expect to allow. |
2232 mock_delegate->Reset(); | 2250 mock_delegate->Reset(); |
2233 | 2251 |
2234 // 3. Guest requests a download that its embedder ignores, this implies deny. | 2252 // 3. Guest requests a download that its embedder ignores, this implies deny. |
2235 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2253 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2236 "startDownload('download-link-3')")); | 2254 "startDownload('download-link-3')")); |
2237 mock_delegate->WaitForCanDownload(false); // Expect to not allow. | 2255 mock_delegate->WaitForCanDownload(false); // Expect to not allow. |
| 2256 completion_observer->WaitForFinished(); |
| 2257 } |
| 2258 |
| 2259 namespace { |
| 2260 |
| 2261 const char kDownloadPathPrefix[] = "/download_cookie_isolation_test"; |
| 2262 |
| 2263 // EmbeddedTestServer request handler for use with DownloadCookieIsolation test. |
| 2264 // Responds with the next status code in |status_codes| if the 'Cookie' header |
| 2265 // sent with the request matches the query() part of the URL. Otherwise, fails |
| 2266 // the request with an HTTP 403. The body of the response is the value of the |
| 2267 // Cookie header. |
| 2268 std::unique_ptr<net::test_server::HttpResponse> HandleDownloadRequestWithCookie( |
| 2269 std::queue<net::HttpStatusCode>* status_codes, |
| 2270 const net::test_server::HttpRequest& request) { |
| 2271 if (request.relative_url.find(kDownloadPathPrefix) != 0) { |
| 2272 return std::unique_ptr<net::test_server::HttpResponse>(); |
| 2273 } |
| 2274 |
| 2275 std::string cookie_to_expect = request.GetURL().query(); |
| 2276 const auto cookie_header_it = request.headers.find("cookie"); |
| 2277 std::unique_ptr<net::test_server::BasicHttpResponse> response; |
| 2278 |
| 2279 // Return a 403 if there's no cookie or if the cookie doesn't match. |
| 2280 if (cookie_header_it == request.headers.end() || |
| 2281 cookie_header_it->second != cookie_to_expect) { |
| 2282 response.reset(new net::test_server::BasicHttpResponse); |
| 2283 response->set_code(net::HTTP_FORBIDDEN); |
| 2284 response->set_content_type("text/plain"); |
| 2285 response->set_content("Forbidden"); |
| 2286 return std::move(response); |
| 2287 } |
| 2288 |
| 2289 DCHECK(!status_codes->empty()); |
| 2290 |
| 2291 // We have a cookie. Send some content along with the next status code. |
| 2292 response.reset(new net::test_server::BasicHttpResponse); |
| 2293 response->set_code(status_codes->front()); |
| 2294 response->set_content_type("application/octet-stream"); |
| 2295 response->set_content(cookie_to_expect); |
| 2296 status_codes->pop(); |
| 2297 return std::move(response); |
| 2298 } |
| 2299 |
| 2300 } // namespace |
| 2301 |
| 2302 // Downloads initiated from isolated guest parititons should use their |
| 2303 // respective cookie stores. In addition, if those downloads are resumed, they |
| 2304 // should continue to use their respective cookie stores. |
| 2305 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadCookieIsolation) { |
| 2306 // These are the status codes to be returned by |
| 2307 // HandleDownloadRequestWithCookie. The first two requests are going to result |
| 2308 // in interrupted downloads. The next two requests are going to succeed. |
| 2309 std::queue<net::HttpStatusCode> status_codes; |
| 2310 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); |
| 2311 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); |
| 2312 status_codes.push(net::HTTP_OK); |
| 2313 status_codes.push(net::HTTP_OK); |
| 2314 |
| 2315 embedded_test_server()->RegisterRequestHandler( |
| 2316 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); |
| 2317 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
| 2318 LoadAndLaunchPlatformApp("web_view/download_cookie_isolation", |
| 2319 "created-webviews"); |
| 2320 |
| 2321 content::WebContents* web_contents = GetFirstAppWindowWebContents(); |
| 2322 ASSERT_TRUE(web_contents); |
| 2323 |
| 2324 base::ScopedTempDir temporary_download_dir; |
| 2325 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); |
| 2326 DownloadPrefs::FromBrowserContext(web_contents->GetBrowserContext()) |
| 2327 ->SetDownloadPath(temporary_download_dir.path()); |
| 2328 |
| 2329 content::DownloadManager* download_manager = |
| 2330 content::BrowserContext::GetDownloadManager( |
| 2331 web_contents->GetBrowserContext()); |
| 2332 |
| 2333 std::unique_ptr<content::DownloadTestObserver> interrupted_observer( |
| 2334 new content::DownloadTestObserverInterrupted( |
| 2335 download_manager, 2, |
| 2336 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2337 |
| 2338 EXPECT_TRUE(content::ExecuteScript( |
| 2339 web_contents, |
| 2340 base::StringPrintf( |
| 2341 "startDownload('first', '%s?cookie=first')", |
| 2342 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); |
| 2343 |
| 2344 EXPECT_TRUE(content::ExecuteScript( |
| 2345 web_contents, |
| 2346 base::StringPrintf( |
| 2347 "startDownload('second', '%s?cookie=second')", |
| 2348 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); |
| 2349 |
| 2350 // Both downloads should fail due to the HTTP_INTERNAL_SERVER_ERROR that was |
| 2351 // injected above to the request handler. This maps to |
| 2352 // DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED. |
| 2353 interrupted_observer->WaitForFinished(); |
| 2354 |
| 2355 content::DownloadManager::DownloadVector downloads; |
| 2356 download_manager->GetAllDownloads(&downloads); |
| 2357 ASSERT_EQ(2u, downloads.size()); |
| 2358 |
| 2359 CloseAppWindow(GetFirstAppWindow()); |
| 2360 |
| 2361 std::unique_ptr<content::DownloadTestObserver> completion_observer( |
| 2362 new content::DownloadTestObserverTerminal( |
| 2363 download_manager, 2, |
| 2364 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2365 |
| 2366 for (auto& download : downloads) { |
| 2367 ASSERT_TRUE(download->CanResume()); |
| 2368 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
| 2369 download->GetLastReason()); |
| 2370 download->Resume(); |
| 2371 } |
| 2372 |
| 2373 completion_observer->WaitForFinished(); |
| 2374 |
| 2375 std::set<std::string> cookies; |
| 2376 for (auto& download : downloads) { |
| 2377 ASSERT_EQ(content::DownloadItem::COMPLETE, download->GetState()); |
| 2378 ASSERT_TRUE(base::PathExists(download->GetTargetFilePath())); |
| 2379 std::string content; |
| 2380 ASSERT_TRUE( |
| 2381 base::ReadFileToString(download->GetTargetFilePath(), &content)); |
| 2382 // Note that the contents of the file is the value of the cookie. |
| 2383 EXPECT_EQ(content, download->GetURL().query()); |
| 2384 cookies.insert(content); |
| 2385 } |
| 2386 |
| 2387 ASSERT_EQ(2u, cookies.size()); |
| 2388 ASSERT_TRUE(cookies.find("cookie=first") != cookies.end()); |
| 2389 ASSERT_TRUE(cookies.find("cookie=second") != cookies.end()); |
2238 } | 2390 } |
2239 | 2391 |
2240 // This test makes sure loading <webview> does not crash when there is an | 2392 // This test makes sure loading <webview> does not crash when there is an |
2241 // extension which has content script whitelisted/forced. | 2393 // extension which has content script whitelisted/forced. |
2242 IN_PROC_BROWSER_TEST_P(WebViewTest, WhitelistedContentScript) { | 2394 IN_PROC_BROWSER_TEST_P(WebViewTest, WhitelistedContentScript) { |
2243 // Whitelist the extension for running content script we are going to load. | 2395 // Whitelist the extension for running content script we are going to load. |
2244 extensions::ExtensionsClient::ScriptingWhitelist whitelist; | 2396 extensions::ExtensionsClient::ScriptingWhitelist whitelist; |
2245 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb"; | 2397 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb"; |
2246 whitelist.push_back(extension_id); | 2398 whitelist.push_back(extension_id); |
2247 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); | 2399 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); |
(...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3097 // 4 tasks expected. The order is arbitrary. | 3249 // 4 tasks expected. The order is arbitrary. |
3098 // Tab: about:blank, | 3250 // Tab: about:blank, |
3099 // Background Page: <webview> task manager test, | 3251 // Background Page: <webview> task manager test, |
3100 // App: <webview> task manager test, | 3252 // App: <webview> task manager test, |
3101 // Webview: WebViewed test content. | 3253 // Webview: WebViewed test content. |
3102 EXPECT_EQ(4U, task_manager.tasks().size()); | 3254 EXPECT_EQ(4U, task_manager.tasks().size()); |
3103 EXPECT_TRUE(HasExpectedGuestTask(task_manager, guest_contents)); | 3255 EXPECT_TRUE(HasExpectedGuestTask(task_manager, guest_contents)); |
3104 } | 3256 } |
3105 | 3257 |
3106 #endif // defined(ENABLE_TASK_MANAGER) | 3258 #endif // defined(ENABLE_TASK_MANAGER) |
OLD | NEW |