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/callback_helpers.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/guid.h" |
8 #include "base/location.h" | 13 #include "base/location.h" |
9 #include "base/macros.h" | 14 #include "base/macros.h" |
10 #include "base/path_service.h" | 15 #include "base/path_service.h" |
11 #include "base/process/process.h" | 16 #include "base/process/process.h" |
12 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
15 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
16 #include "build/build_config.h" | 22 #include "build/build_config.h" |
17 #include "chrome/app/chrome_command_ids.h" | 23 #include "chrome/app/chrome_command_ids.h" |
18 #include "chrome/browser/apps/app_browsertest_util.h" | 24 #include "chrome/browser/apps/app_browsertest_util.h" |
19 #include "chrome/browser/chrome_content_browser_client.h" | 25 #include "chrome/browser/chrome_content_browser_client.h" |
20 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 26 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 27 #include "chrome/browser/download/download_history.h" |
| 28 #include "chrome/browser/download/download_prefs.h" |
| 29 #include "chrome/browser/download/download_service.h" |
| 30 #include "chrome/browser/download/download_service_factory.h" |
21 #include "chrome/browser/lifetime/application_lifetime.h" | 31 #include "chrome/browser/lifetime/application_lifetime.h" |
22 #include "chrome/browser/pdf/pdf_extension_test_util.h" | 32 #include "chrome/browser/pdf/pdf_extension_test_util.h" |
23 #include "chrome/browser/prerender/prerender_link_manager.h" | 33 #include "chrome/browser/prerender/prerender_link_manager.h" |
24 #include "chrome/browser/prerender/prerender_link_manager_factory.h" | 34 #include "chrome/browser/prerender/prerender_link_manager_factory.h" |
25 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 36 #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" | 37 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
28 #include "chrome/browser/task_management/mock_web_contents_task_manager.h" | 38 #include "chrome/browser/task_management/mock_web_contents_task_manager.h" |
29 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 39 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
30 #include "chrome/browser/ui/browser.h" | 40 #include "chrome/browser/ui/browser.h" |
(...skipping 10 matching lines...) Expand all Loading... |
41 #include "content/public/browser/interstitial_page_delegate.h" | 51 #include "content/public/browser/interstitial_page_delegate.h" |
42 #include "content/public/browser/notification_service.h" | 52 #include "content/public/browser/notification_service.h" |
43 #include "content/public/browser/render_process_host.h" | 53 #include "content/public/browser/render_process_host.h" |
44 #include "content/public/browser/render_widget_host.h" | 54 #include "content/public/browser/render_widget_host.h" |
45 #include "content/public/browser/render_widget_host_view.h" | 55 #include "content/public/browser/render_widget_host_view.h" |
46 #include "content/public/browser/web_contents_delegate.h" | 56 #include "content/public/browser/web_contents_delegate.h" |
47 #include "content/public/common/browser_plugin_guest_mode.h" | 57 #include "content/public/common/browser_plugin_guest_mode.h" |
48 #include "content/public/common/child_process_host.h" | 58 #include "content/public/common/child_process_host.h" |
49 #include "content/public/common/content_switches.h" | 59 #include "content/public/common/content_switches.h" |
50 #include "content/public/test/browser_test_utils.h" | 60 #include "content/public/test/browser_test_utils.h" |
| 61 #include "content/public/test/download_test_observer.h" |
51 #include "content/public/test/fake_speech_recognition_manager.h" | 62 #include "content/public/test/fake_speech_recognition_manager.h" |
52 #include "content/public/test/test_renderer_host.h" | 63 #include "content/public/test/test_renderer_host.h" |
53 #include "extensions/browser/api/declarative/rules_registry.h" | 64 #include "extensions/browser/api/declarative/rules_registry.h" |
54 #include "extensions/browser/api/declarative/rules_registry_service.h" | 65 #include "extensions/browser/api/declarative/rules_registry_service.h" |
55 #include "extensions/browser/api/declarative/test_rules_registry.h" | 66 #include "extensions/browser/api/declarative/test_rules_registry.h" |
56 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 67 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
57 #include "extensions/browser/api/extensions_api_client.h" | 68 #include "extensions/browser/api/extensions_api_client.h" |
58 #include "extensions/browser/app_window/native_app_window.h" | 69 #include "extensions/browser/app_window/native_app_window.h" |
59 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 70 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
60 #include "extensions/common/extension.h" | 71 #include "extensions/common/extension.h" |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 decision_made_(false), | 313 decision_made_(false), |
303 last_download_allowed_(false) {} | 314 last_download_allowed_(false) {} |
304 ~MockDownloadWebContentsDelegate() override {} | 315 ~MockDownloadWebContentsDelegate() override {} |
305 | 316 |
306 void CanDownload(const GURL& url, | 317 void CanDownload(const GURL& url, |
307 const std::string& request_method, | 318 const std::string& request_method, |
308 const base::Callback<void(bool)>& callback) override { | 319 const base::Callback<void(bool)>& callback) override { |
309 orig_delegate_->CanDownload( | 320 orig_delegate_->CanDownload( |
310 url, request_method, | 321 url, request_method, |
311 base::Bind(&MockDownloadWebContentsDelegate::DownloadDecided, | 322 base::Bind(&MockDownloadWebContentsDelegate::DownloadDecided, |
312 base::Unretained(this))); | 323 base::Unretained(this), callback)); |
313 } | 324 } |
314 | 325 |
315 void WaitForCanDownload(bool expect_allow) { | 326 void WaitForCanDownload(bool expect_allow) { |
316 EXPECT_FALSE(waiting_for_decision_); | 327 EXPECT_FALSE(waiting_for_decision_); |
317 waiting_for_decision_ = true; | 328 waiting_for_decision_ = true; |
318 | 329 |
319 if (decision_made_) { | 330 if (decision_made_) { |
320 EXPECT_EQ(expect_allow, last_download_allowed_); | 331 EXPECT_EQ(expect_allow, last_download_allowed_); |
321 return; | 332 return; |
322 } | 333 } |
323 | 334 |
324 expect_allow_ = expect_allow; | 335 expect_allow_ = expect_allow; |
325 message_loop_runner_ = new content::MessageLoopRunner; | 336 message_loop_runner_ = new content::MessageLoopRunner; |
326 message_loop_runner_->Run(); | 337 message_loop_runner_->Run(); |
327 } | 338 } |
328 | 339 |
329 void DownloadDecided(bool allow) { | 340 void DownloadDecided(const base::Callback<void(bool)>& callback, bool allow) { |
330 EXPECT_FALSE(decision_made_); | 341 EXPECT_FALSE(decision_made_); |
331 decision_made_ = true; | 342 decision_made_ = true; |
332 | 343 |
333 if (waiting_for_decision_) { | 344 if (waiting_for_decision_) { |
334 EXPECT_EQ(expect_allow_, allow); | 345 EXPECT_EQ(expect_allow_, allow); |
335 if (message_loop_runner_.get()) | 346 if (message_loop_runner_.get()) |
336 message_loop_runner_->Quit(); | 347 message_loop_runner_->Quit(); |
| 348 callback.Run(allow); |
337 return; | 349 return; |
338 } | 350 } |
339 last_download_allowed_ = allow; | 351 last_download_allowed_ = allow; |
| 352 callback.Run(allow); |
340 } | 353 } |
341 | 354 |
342 void Reset() { | 355 void Reset() { |
343 waiting_for_decision_ = false; | 356 waiting_for_decision_ = false; |
344 decision_made_ = false; | 357 decision_made_ = false; |
345 } | 358 } |
346 | 359 |
347 private: | 360 private: |
348 content::WebContentsDelegate* orig_delegate_; | 361 content::WebContentsDelegate* orig_delegate_; |
349 bool waiting_for_decision_; | 362 bool waiting_for_decision_; |
(...skipping 1846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2196 << message_; | 2209 << message_; |
2197 } | 2210 } |
2198 | 2211 |
2199 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) { | 2212 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) { |
2200 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. | 2213 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
2201 content::WebContents* guest_web_contents = | 2214 content::WebContents* guest_web_contents = |
2202 LoadGuest("/extensions/platform_apps/web_view/download/guest.html", | 2215 LoadGuest("/extensions/platform_apps/web_view/download/guest.html", |
2203 "web_view/download"); | 2216 "web_view/download"); |
2204 ASSERT_TRUE(guest_web_contents); | 2217 ASSERT_TRUE(guest_web_contents); |
2205 | 2218 |
| 2219 base::ScopedTempDir temporary_download_dir; |
| 2220 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); |
| 2221 DownloadPrefs::FromBrowserContext(guest_web_contents->GetBrowserContext()) |
| 2222 ->SetDownloadPath(temporary_download_dir.path()); |
| 2223 |
| 2224 std::unique_ptr<content::DownloadTestObserver> completion_observer( |
| 2225 new content::DownloadTestObserverTerminal( |
| 2226 content::BrowserContext::GetDownloadManager( |
| 2227 guest_web_contents->GetBrowserContext()), |
| 2228 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2229 |
2206 // Replace WebContentsDelegate with mock version so we can intercept download | 2230 // Replace WebContentsDelegate with mock version so we can intercept download |
2207 // requests. | 2231 // requests. |
2208 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate(); | 2232 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate(); |
2209 std::unique_ptr<MockDownloadWebContentsDelegate> mock_delegate( | 2233 std::unique_ptr<MockDownloadWebContentsDelegate> mock_delegate( |
2210 new MockDownloadWebContentsDelegate(delegate)); | 2234 new MockDownloadWebContentsDelegate(delegate)); |
2211 guest_web_contents->SetDelegate(mock_delegate.get()); | 2235 guest_web_contents->SetDelegate(mock_delegate.get()); |
2212 | 2236 |
2213 // Start test. | 2237 // Start test. |
2214 // 1. Guest requests a download that its embedder denies. | 2238 // 1. Guest requests a download that its embedder denies. |
2215 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2239 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2216 "startDownload('download-link-1')")); | 2240 "startDownload('download-link-1')")); |
2217 mock_delegate->WaitForCanDownload(false); // Expect to not allow. | 2241 mock_delegate->WaitForCanDownload(false); // Expect to not allow. |
2218 mock_delegate->Reset(); | 2242 mock_delegate->Reset(); |
2219 | 2243 |
2220 // 2. Guest requests a download that its embedder allows. | 2244 // 2. Guest requests a download that its embedder allows. |
2221 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2245 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2222 "startDownload('download-link-2')")); | 2246 "startDownload('download-link-2')")); |
2223 mock_delegate->WaitForCanDownload(true); // Expect to allow. | 2247 mock_delegate->WaitForCanDownload(true); // Expect to allow. |
2224 mock_delegate->Reset(); | 2248 mock_delegate->Reset(); |
2225 | 2249 |
2226 // 3. Guest requests a download that its embedder ignores, this implies deny. | 2250 // 3. Guest requests a download that its embedder ignores, this implies deny. |
2227 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2251 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2228 "startDownload('download-link-3')")); | 2252 "startDownload('download-link-3')")); |
2229 mock_delegate->WaitForCanDownload(false); // Expect to not allow. | 2253 mock_delegate->WaitForCanDownload(false); // Expect to not allow. |
| 2254 completion_observer->WaitForFinished(); |
| 2255 } |
| 2256 |
| 2257 namespace { |
| 2258 |
| 2259 const char kDownloadPathPrefix[] = "/download_cookie_isolation_test"; |
| 2260 |
| 2261 // EmbeddedTestServer request handler for use with DownloadCookieIsolation test. |
| 2262 // Responds with the next status code in |status_codes| if the 'Cookie' header |
| 2263 // sent with the request matches the query() part of the URL. Otherwise, fails |
| 2264 // the request with an HTTP 403. The body of the response is the value of the |
| 2265 // Cookie header. |
| 2266 std::unique_ptr<net::test_server::HttpResponse> HandleDownloadRequestWithCookie( |
| 2267 std::queue<net::HttpStatusCode>* status_codes, |
| 2268 const net::test_server::HttpRequest& request) { |
| 2269 if (request.relative_url.find(kDownloadPathPrefix) != 0) { |
| 2270 return std::unique_ptr<net::test_server::HttpResponse>(); |
| 2271 } |
| 2272 |
| 2273 std::string cookie_to_expect = request.GetURL().query(); |
| 2274 const auto cookie_header_it = request.headers.find("cookie"); |
| 2275 std::unique_ptr<net::test_server::BasicHttpResponse> response; |
| 2276 |
| 2277 // Return a 403 if there's no cookie or if the cookie doesn't match. |
| 2278 if (cookie_header_it == request.headers.end() || |
| 2279 cookie_header_it->second != cookie_to_expect) { |
| 2280 response.reset(new net::test_server::BasicHttpResponse); |
| 2281 response->set_code(net::HTTP_FORBIDDEN); |
| 2282 response->set_content_type("text/plain"); |
| 2283 response->set_content("Forbidden"); |
| 2284 return std::move(response); |
| 2285 } |
| 2286 |
| 2287 DCHECK(!status_codes->empty()); |
| 2288 |
| 2289 // We have a cookie. Send some content along with the next status code. |
| 2290 response.reset(new net::test_server::BasicHttpResponse); |
| 2291 response->set_code(status_codes->front()); |
| 2292 response->set_content_type("application/octet-stream"); |
| 2293 response->set_content(cookie_to_expect); |
| 2294 status_codes->pop(); |
| 2295 return std::move(response); |
| 2296 } |
| 2297 |
| 2298 class DownloadHistoryWaiter : public DownloadHistory::Observer { |
| 2299 public: |
| 2300 explicit DownloadHistoryWaiter(content::BrowserContext* browser_context) { |
| 2301 DownloadService* service = |
| 2302 DownloadServiceFactory::GetForBrowserContext(browser_context); |
| 2303 download_history_ = service->GetDownloadHistory(); |
| 2304 download_history_->AddObserver(this); |
| 2305 } |
| 2306 |
| 2307 ~DownloadHistoryWaiter() override { download_history_->RemoveObserver(this); } |
| 2308 |
| 2309 void WaitForStored(size_t download_count) { |
| 2310 if (stored_downloads_.size() >= download_count) |
| 2311 return; |
| 2312 stored_download_target_ = download_count; |
| 2313 Wait(); |
| 2314 } |
| 2315 |
| 2316 void WaitForHistoryLoad() { |
| 2317 if (history_query_complete_) |
| 2318 return; |
| 2319 Wait(); |
| 2320 } |
| 2321 |
| 2322 private: |
| 2323 void Wait() { |
| 2324 base::RunLoop run_loop; |
| 2325 quit_closure_ = run_loop.QuitClosure(); |
| 2326 run_loop.Run(); |
| 2327 } |
| 2328 |
| 2329 void OnDownloadStored(content::DownloadItem* item, |
| 2330 const history::DownloadRow& info) override { |
| 2331 stored_downloads_.insert(item); |
| 2332 if (!quit_closure_.is_null() && |
| 2333 stored_downloads_.size() >= stored_download_target_) { |
| 2334 base::ResetAndReturn(&quit_closure_).Run(); |
| 2335 } |
| 2336 } |
| 2337 |
| 2338 void OnHistoryQueryComplete() override { |
| 2339 history_query_complete_ = true; |
| 2340 if (!quit_closure_.is_null()) |
| 2341 base::ResetAndReturn(&quit_closure_).Run(); |
| 2342 } |
| 2343 |
| 2344 std::unordered_set<content::DownloadItem*> stored_downloads_; |
| 2345 size_t stored_download_target_ = 0; |
| 2346 bool history_query_complete_ = false; |
| 2347 base::Closure quit_closure_; |
| 2348 DownloadHistory* download_history_ = nullptr; |
| 2349 }; |
| 2350 |
| 2351 } // namespace |
| 2352 |
| 2353 // Downloads initiated from isolated guest parititons should use their |
| 2354 // respective cookie stores. In addition, if those downloads are resumed, they |
| 2355 // should continue to use their respective cookie stores. |
| 2356 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadCookieIsolation) { |
| 2357 // These are the status codes to be returned by |
| 2358 // HandleDownloadRequestWithCookie. The first two requests are going to result |
| 2359 // in interrupted downloads. The next two requests are going to succeed. |
| 2360 std::queue<net::HttpStatusCode> status_codes; |
| 2361 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); |
| 2362 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); |
| 2363 status_codes.push(net::HTTP_OK); |
| 2364 status_codes.push(net::HTTP_OK); |
| 2365 |
| 2366 embedded_test_server()->RegisterRequestHandler( |
| 2367 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); |
| 2368 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
| 2369 LoadAndLaunchPlatformApp("web_view/download_cookie_isolation", |
| 2370 "created-webviews"); |
| 2371 |
| 2372 content::WebContents* web_contents = GetFirstAppWindowWebContents(); |
| 2373 ASSERT_TRUE(web_contents); |
| 2374 |
| 2375 base::ScopedTempDir temporary_download_dir; |
| 2376 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); |
| 2377 DownloadPrefs::FromBrowserContext(web_contents->GetBrowserContext()) |
| 2378 ->SetDownloadPath(temporary_download_dir.path()); |
| 2379 |
| 2380 content::DownloadManager* download_manager = |
| 2381 content::BrowserContext::GetDownloadManager( |
| 2382 web_contents->GetBrowserContext()); |
| 2383 |
| 2384 std::unique_ptr<content::DownloadTestObserver> interrupted_observer( |
| 2385 new content::DownloadTestObserverInterrupted( |
| 2386 download_manager, 2, |
| 2387 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2388 |
| 2389 EXPECT_TRUE(content::ExecuteScript( |
| 2390 web_contents, |
| 2391 base::StringPrintf( |
| 2392 "startDownload('first', '%s?cookie=first')", |
| 2393 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); |
| 2394 |
| 2395 EXPECT_TRUE(content::ExecuteScript( |
| 2396 web_contents, |
| 2397 base::StringPrintf( |
| 2398 "startDownload('second', '%s?cookie=second')", |
| 2399 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); |
| 2400 |
| 2401 // Both downloads should fail due to the HTTP_INTERNAL_SERVER_ERROR that was |
| 2402 // injected above to the request handler. This maps to |
| 2403 // DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED. |
| 2404 interrupted_observer->WaitForFinished(); |
| 2405 |
| 2406 content::DownloadManager::DownloadVector downloads; |
| 2407 download_manager->GetAllDownloads(&downloads); |
| 2408 ASSERT_EQ(2u, downloads.size()); |
| 2409 |
| 2410 CloseAppWindow(GetFirstAppWindow()); |
| 2411 |
| 2412 std::unique_ptr<content::DownloadTestObserver> completion_observer( |
| 2413 new content::DownloadTestObserverTerminal( |
| 2414 download_manager, 2, |
| 2415 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2416 |
| 2417 for (auto& download : downloads) { |
| 2418 ASSERT_TRUE(download->CanResume()); |
| 2419 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
| 2420 download->GetLastReason()); |
| 2421 download->Resume(); |
| 2422 } |
| 2423 |
| 2424 completion_observer->WaitForFinished(); |
| 2425 |
| 2426 std::set<std::string> cookies; |
| 2427 for (auto& download : downloads) { |
| 2428 ASSERT_EQ(content::DownloadItem::COMPLETE, download->GetState()); |
| 2429 ASSERT_TRUE(base::PathExists(download->GetTargetFilePath())); |
| 2430 std::string content; |
| 2431 ASSERT_TRUE( |
| 2432 base::ReadFileToString(download->GetTargetFilePath(), &content)); |
| 2433 // Note that the contents of the file is the value of the cookie. |
| 2434 EXPECT_EQ(content, download->GetURL().query()); |
| 2435 cookies.insert(content); |
| 2436 } |
| 2437 |
| 2438 ASSERT_EQ(2u, cookies.size()); |
| 2439 ASSERT_TRUE(cookies.find("cookie=first") != cookies.end()); |
| 2440 ASSERT_TRUE(cookies.find("cookie=second") != cookies.end()); |
| 2441 } |
| 2442 |
| 2443 IN_PROC_BROWSER_TEST_P(WebViewTest, PRE_DownloadCookieIsolation_CrossSession) { |
| 2444 // These are the status codes to be returned by |
| 2445 // HandleDownloadRequestWithCookie. The first two requests are going to result |
| 2446 // in interrupted downloads. The next two requests are going to succeed. |
| 2447 std::queue<net::HttpStatusCode> status_codes; |
| 2448 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); |
| 2449 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); |
| 2450 |
| 2451 embedded_test_server()->RegisterRequestHandler( |
| 2452 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); |
| 2453 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
| 2454 LoadAndLaunchPlatformApp("web_view/download_cookie_isolation", |
| 2455 "created-webviews"); |
| 2456 |
| 2457 content::WebContents* web_contents = GetFirstAppWindowWebContents(); |
| 2458 ASSERT_TRUE(web_contents); |
| 2459 |
| 2460 base::ScopedTempDir temporary_download_dir; |
| 2461 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); |
| 2462 DownloadPrefs::FromBrowserContext(web_contents->GetBrowserContext()) |
| 2463 ->SetDownloadPath(temporary_download_dir.path()); |
| 2464 |
| 2465 content::DownloadManager* download_manager = |
| 2466 content::BrowserContext::GetDownloadManager( |
| 2467 web_contents->GetBrowserContext()); |
| 2468 |
| 2469 DownloadHistoryWaiter history_waiter(web_contents->GetBrowserContext()); |
| 2470 |
| 2471 std::unique_ptr<content::DownloadTestObserver> interrupted_observer( |
| 2472 new content::DownloadTestObserverInterrupted( |
| 2473 download_manager, 2, |
| 2474 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2475 |
| 2476 EXPECT_TRUE(content::ExecuteScript( |
| 2477 web_contents, |
| 2478 base::StringPrintf( |
| 2479 "startDownload('first', '%s?cookie=first')", |
| 2480 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); |
| 2481 |
| 2482 // Note that the second webview uses an in-memory partition. |
| 2483 EXPECT_TRUE(content::ExecuteScript( |
| 2484 web_contents, |
| 2485 base::StringPrintf( |
| 2486 "startDownload('second', '%s?cookie=second')", |
| 2487 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); |
| 2488 |
| 2489 // Both downloads should fail due to the HTTP_INTERNAL_SERVER_ERROR that was |
| 2490 // injected above to the request handler. This maps to |
| 2491 // DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED. |
| 2492 interrupted_observer->WaitForFinished(); |
| 2493 |
| 2494 // Wait for both downloads to be stored. |
| 2495 history_waiter.WaitForStored(2); |
| 2496 |
| 2497 // Leak the temporary download directory. We'll retake ownership in the next |
| 2498 // browser session. |
| 2499 temporary_download_dir.Take(); |
| 2500 } |
| 2501 |
| 2502 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadCookieIsolation_CrossSession) { |
| 2503 std::queue<net::HttpStatusCode> status_codes; |
| 2504 status_codes.push(net::HTTP_OK); |
| 2505 status_codes.push(net::HTTP_OK); |
| 2506 |
| 2507 embedded_test_server()->RegisterRequestHandler( |
| 2508 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); |
| 2509 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
| 2510 |
| 2511 content::BrowserContext* browser_context = profile(); |
| 2512 content::DownloadManager* download_manager = |
| 2513 content::BrowserContext::GetDownloadManager(browser_context); |
| 2514 |
| 2515 DownloadHistoryWaiter history_waiter(browser_context); |
| 2516 history_waiter.WaitForHistoryLoad(); |
| 2517 |
| 2518 base::ScopedTempDir temporary_download_dir; |
| 2519 ASSERT_TRUE(temporary_download_dir.Set( |
| 2520 DownloadPrefs::FromBrowserContext(browser_context)->DownloadPath())); |
| 2521 |
| 2522 content::DownloadManager::DownloadVector saved_downloads; |
| 2523 download_manager->GetAllDownloads(&saved_downloads); |
| 2524 ASSERT_EQ(2u, saved_downloads.size()); |
| 2525 |
| 2526 content::DownloadManager::DownloadVector downloads; |
| 2527 // We can't trivially resume the previous downloads because they are going to |
| 2528 // try to talk to the old EmbeddedTestServer instance. We need to update the |
| 2529 // URL to point to the new instance, which should only differ by the port |
| 2530 // number. |
| 2531 for (auto& download : saved_downloads) { |
| 2532 const std::string port_string = |
| 2533 base::UintToString(embedded_test_server()->port()); |
| 2534 url::Replacements<char> replacements; |
| 2535 replacements.SetPort(port_string.c_str(), |
| 2536 url::Component(0, port_string.size())); |
| 2537 std::vector<GURL> url_chain; |
| 2538 url_chain.push_back(download->GetURL().ReplaceComponents(replacements)); |
| 2539 |
| 2540 downloads.push_back(download_manager->CreateDownloadItem( |
| 2541 base::GenerateGUID(), download->GetId() + 2, download->GetFullPath(), |
| 2542 download->GetTargetFilePath(), url_chain, download->GetReferrerUrl(), |
| 2543 download->GetSiteUrl(), download->GetTabUrl(), |
| 2544 download->GetTabReferrerUrl(), download->GetMimeType(), |
| 2545 download->GetOriginalMimeType(), download->GetStartTime(), |
| 2546 download->GetEndTime(), download->GetETag(), |
| 2547 download->GetLastModifiedTime(), download->GetReceivedBytes(), |
| 2548 download->GetTotalBytes(), download->GetHash(), download->GetState(), |
| 2549 download->GetDangerType(), download->GetLastReason(), |
| 2550 download->GetOpened())); |
| 2551 } |
| 2552 |
| 2553 std::unique_ptr<content::DownloadTestObserver> completion_observer( |
| 2554 new content::DownloadTestObserverTerminal( |
| 2555 download_manager, 2, |
| 2556 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
| 2557 |
| 2558 for (auto& download : downloads) { |
| 2559 ASSERT_TRUE(download->CanResume()); |
| 2560 ASSERT_TRUE( |
| 2561 temporary_download_dir.path().IsParent(download->GetTargetFilePath())); |
| 2562 ASSERT_TRUE(download->GetFullPath().empty()); |
| 2563 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
| 2564 download->GetLastReason()); |
| 2565 download->Resume(); |
| 2566 } |
| 2567 |
| 2568 completion_observer->WaitForFinished(); |
| 2569 |
| 2570 // Of the two downloads, ?cookie=first will succeed and ?cookie=second will |
| 2571 // fail. The latter fails because the underlying storage partition was not |
| 2572 // persisted. |
| 2573 |
| 2574 content::DownloadItem* succeeded_download = downloads[0]; |
| 2575 content::DownloadItem* failed_download = downloads[1]; |
| 2576 |
| 2577 if (downloads[0]->GetState() == content::DownloadItem::INTERRUPTED) |
| 2578 std::swap(succeeded_download, failed_download); |
| 2579 |
| 2580 ASSERT_EQ(content::DownloadItem::COMPLETE, succeeded_download->GetState()); |
| 2581 ASSERT_TRUE(base::PathExists(succeeded_download->GetTargetFilePath())); |
| 2582 std::string content; |
| 2583 ASSERT_TRUE(base::ReadFileToString(succeeded_download->GetTargetFilePath(), |
| 2584 &content)); |
| 2585 // This is the cookie that should've been stored in the persisted storage |
| 2586 // partition. |
| 2587 EXPECT_STREQ("cookie=first", content.c_str()); |
| 2588 |
| 2589 ASSERT_EQ(content::DownloadItem::INTERRUPTED, failed_download->GetState()); |
| 2590 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN, |
| 2591 failed_download->GetLastReason()); |
2230 } | 2592 } |
2231 | 2593 |
2232 // This test makes sure loading <webview> does not crash when there is an | 2594 // This test makes sure loading <webview> does not crash when there is an |
2233 // extension which has content script whitelisted/forced. | 2595 // extension which has content script whitelisted/forced. |
2234 IN_PROC_BROWSER_TEST_P(WebViewTest, WhitelistedContentScript) { | 2596 IN_PROC_BROWSER_TEST_P(WebViewTest, WhitelistedContentScript) { |
2235 // Whitelist the extension for running content script we are going to load. | 2597 // Whitelist the extension for running content script we are going to load. |
2236 extensions::ExtensionsClient::ScriptingWhitelist whitelist; | 2598 extensions::ExtensionsClient::ScriptingWhitelist whitelist; |
2237 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb"; | 2599 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb"; |
2238 whitelist.push_back(extension_id); | 2600 whitelist.push_back(extension_id); |
2239 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); | 2601 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); |
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2991 gfx::Point embedder_origin = | 3353 gfx::Point embedder_origin = |
2992 GetEmbedderWebContents()->GetContainerBounds().origin(); | 3354 GetEmbedderWebContents()->GetContainerBounds().origin(); |
2993 guest_rect.Offset(-embedder_origin.x(), -embedder_origin.y()); | 3355 guest_rect.Offset(-embedder_origin.x(), -embedder_origin.y()); |
2994 | 3356 |
2995 // Generate and send synthetic touch event. | 3357 // Generate and send synthetic touch event. |
2996 content::SimulateTouchPressAt(GetEmbedderWebContents(), | 3358 content::SimulateTouchPressAt(GetEmbedderWebContents(), |
2997 guest_rect.CenterPoint()); | 3359 guest_rect.CenterPoint()); |
2998 EXPECT_TRUE(aura_webview->HasFocus()); | 3360 EXPECT_TRUE(aura_webview->HasFocus()); |
2999 } | 3361 } |
3000 #endif | 3362 #endif |
OLD | NEW |