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/task_management_browsertest_util.h" | 38 #include "chrome/browser/task_management/task_management_browsertest_util.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 1854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2204 << message_; | 2217 << message_; |
2205 } | 2218 } |
2206 | 2219 |
2207 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) { | 2220 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) { |
2208 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. | 2221 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. |
2209 content::WebContents* guest_web_contents = | 2222 content::WebContents* guest_web_contents = |
2210 LoadGuest("/extensions/platform_apps/web_view/download/guest.html", | 2223 LoadGuest("/extensions/platform_apps/web_view/download/guest.html", |
2211 "web_view/download"); | 2224 "web_view/download"); |
2212 ASSERT_TRUE(guest_web_contents); | 2225 ASSERT_TRUE(guest_web_contents); |
2213 | 2226 |
2227 base::ScopedTempDir temporary_download_dir; | |
2228 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); | |
2229 DownloadPrefs::FromBrowserContext(guest_web_contents->GetBrowserContext()) | |
2230 ->SetDownloadPath(temporary_download_dir.path()); | |
2231 | |
2232 std::unique_ptr<content::DownloadTestObserver> completion_observer( | |
2233 new content::DownloadTestObserverTerminal( | |
2234 content::BrowserContext::GetDownloadManager( | |
2235 guest_web_contents->GetBrowserContext()), | |
2236 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | |
2237 | |
2214 // Replace WebContentsDelegate with mock version so we can intercept download | 2238 // Replace WebContentsDelegate with mock version so we can intercept download |
2215 // requests. | 2239 // requests. |
2216 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate(); | 2240 content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate(); |
2217 std::unique_ptr<MockDownloadWebContentsDelegate> mock_delegate( | 2241 std::unique_ptr<MockDownloadWebContentsDelegate> mock_delegate( |
2218 new MockDownloadWebContentsDelegate(delegate)); | 2242 new MockDownloadWebContentsDelegate(delegate)); |
2219 guest_web_contents->SetDelegate(mock_delegate.get()); | 2243 guest_web_contents->SetDelegate(mock_delegate.get()); |
2220 | 2244 |
2221 // Start test. | 2245 // Start test. |
2222 // 1. Guest requests a download that its embedder denies. | 2246 // 1. Guest requests a download that its embedder denies. |
2223 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2247 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2224 "startDownload('download-link-1')")); | 2248 "startDownload('download-link-1')")); |
2225 mock_delegate->WaitForCanDownload(false); // Expect to not allow. | 2249 mock_delegate->WaitForCanDownload(false); // Expect to not allow. |
2226 mock_delegate->Reset(); | 2250 mock_delegate->Reset(); |
2227 | 2251 |
2228 // 2. Guest requests a download that its embedder allows. | 2252 // 2. Guest requests a download that its embedder allows. |
2229 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2253 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2230 "startDownload('download-link-2')")); | 2254 "startDownload('download-link-2')")); |
2231 mock_delegate->WaitForCanDownload(true); // Expect to allow. | 2255 mock_delegate->WaitForCanDownload(true); // Expect to allow. |
2232 mock_delegate->Reset(); | 2256 mock_delegate->Reset(); |
2233 | 2257 |
2234 // 3. Guest requests a download that its embedder ignores, this implies deny. | 2258 // 3. Guest requests a download that its embedder ignores, this implies deny. |
2235 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, | 2259 EXPECT_TRUE(content::ExecuteScript(guest_web_contents, |
2236 "startDownload('download-link-3')")); | 2260 "startDownload('download-link-3')")); |
2237 mock_delegate->WaitForCanDownload(false); // Expect to not allow. | 2261 mock_delegate->WaitForCanDownload(false); // Expect to not allow. |
2262 completion_observer->WaitForFinished(); | |
2263 } | |
2264 | |
2265 namespace { | |
2266 | |
2267 const char kDownloadPathPrefix[] = "/download_cookie_isolation_test"; | |
2268 | |
2269 // EmbeddedTestServer request handler for use with DownloadCookieIsolation test. | |
2270 // Responds with the next status code in |status_codes| if the 'Cookie' header | |
2271 // sent with the request matches the query() part of the URL. Otherwise, fails | |
2272 // the request with an HTTP 403. The body of the response is the value of the | |
2273 // Cookie header. | |
2274 std::unique_ptr<net::test_server::HttpResponse> HandleDownloadRequestWithCookie( | |
2275 std::queue<net::HttpStatusCode>* status_codes, | |
2276 const net::test_server::HttpRequest& request) { | |
2277 if (request.relative_url.find(kDownloadPathPrefix) != 0) { | |
2278 return std::unique_ptr<net::test_server::HttpResponse>(); | |
2279 } | |
2280 | |
2281 std::string cookie_to_expect = request.GetURL().query(); | |
2282 const auto cookie_header_it = request.headers.find("cookie"); | |
2283 std::unique_ptr<net::test_server::BasicHttpResponse> response; | |
2284 | |
2285 // Return a 403 if there's no cookie or if the cookie doesn't match. | |
2286 if (cookie_header_it == request.headers.end() || | |
2287 cookie_header_it->second != cookie_to_expect) { | |
2288 response.reset(new net::test_server::BasicHttpResponse); | |
2289 response->set_code(net::HTTP_FORBIDDEN); | |
2290 response->set_content_type("text/plain"); | |
2291 response->set_content("Forbidden"); | |
2292 return std::move(response); | |
2293 } | |
2294 | |
2295 DCHECK(!status_codes->empty()); | |
2296 | |
2297 // We have a cookie. Send some content along with the next status code. | |
2298 response.reset(new net::test_server::BasicHttpResponse); | |
2299 response->set_code(status_codes->front()); | |
2300 response->set_content_type("application/octet-stream"); | |
2301 response->set_content(cookie_to_expect); | |
2302 status_codes->pop(); | |
2303 return std::move(response); | |
2304 } | |
2305 | |
2306 class DownloadHistoryWaiter : public DownloadHistory::Observer { | |
2307 public: | |
2308 explicit DownloadHistoryWaiter(content::BrowserContext* browser_context) { | |
2309 DownloadService* service = | |
2310 DownloadServiceFactory::GetForBrowserContext(browser_context); | |
2311 download_history_ = service->GetDownloadHistory(); | |
2312 download_history_->AddObserver(this); | |
2313 } | |
2314 | |
2315 ~DownloadHistoryWaiter() override { download_history_->RemoveObserver(this); } | |
2316 | |
2317 void WaitForStored(size_t download_count) { | |
2318 if (stored_downloads_.size() >= download_count) | |
2319 return; | |
2320 stored_download_target_ = download_count; | |
2321 Wait(); | |
2322 } | |
2323 | |
2324 void WaitForHistoryLoad() { | |
2325 if (history_query_complete_) | |
2326 return; | |
2327 Wait(); | |
2328 } | |
2329 | |
2330 private: | |
2331 void Wait() { | |
2332 base::RunLoop run_loop; | |
2333 quit_closure_ = run_loop.QuitClosure(); | |
2334 run_loop.Run(); | |
2335 } | |
2336 | |
2337 void OnDownloadStored(content::DownloadItem* item, | |
2338 const history::DownloadRow& info) override { | |
2339 stored_downloads_.insert(item); | |
2340 if (!quit_closure_.is_null() && | |
2341 stored_downloads_.size() >= stored_download_target_) { | |
2342 base::ResetAndReturn(&quit_closure_).Run(); | |
2343 } | |
2344 } | |
2345 | |
2346 void OnHistoryQueryComplete() override { | |
2347 history_query_complete_ = true; | |
2348 if (!quit_closure_.is_null()) | |
2349 base::ResetAndReturn(&quit_closure_).Run(); | |
2350 } | |
2351 | |
2352 std::unordered_set<content::DownloadItem*> stored_downloads_; | |
2353 size_t stored_download_target_ = 0; | |
2354 bool history_query_complete_ = false; | |
2355 base::Closure quit_closure_; | |
2356 DownloadHistory* download_history_ = nullptr; | |
2357 }; | |
2358 | |
2359 } // namespace | |
2360 | |
2361 // Downloads initiated from isolated guest parititons should use their | |
2362 // respective cookie stores. In addition, if those downloads are resumed, they | |
2363 // should continue to use their respective cookie stores. | |
2364 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadCookieIsolation) { | |
2365 // These are the status codes to be returned by | |
2366 // HandleDownloadRequestWithCookie. The first two requests are going to result | |
2367 // in interrupted downloads. The next two requests are going to succeed. | |
2368 std::queue<net::HttpStatusCode> status_codes; | |
2369 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); | |
2370 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); | |
2371 status_codes.push(net::HTTP_OK); | |
2372 status_codes.push(net::HTTP_OK); | |
2373 | |
2374 embedded_test_server()->RegisterRequestHandler( | |
2375 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); | |
2376 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. | |
2377 LoadAndLaunchPlatformApp("web_view/download_cookie_isolation", | |
2378 "created-webviews"); | |
2379 | |
2380 content::WebContents* web_contents = GetFirstAppWindowWebContents(); | |
2381 ASSERT_TRUE(web_contents); | |
2382 | |
2383 base::ScopedTempDir temporary_download_dir; | |
2384 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); | |
2385 DownloadPrefs::FromBrowserContext(web_contents->GetBrowserContext()) | |
2386 ->SetDownloadPath(temporary_download_dir.path()); | |
2387 | |
2388 content::DownloadManager* download_manager = | |
2389 content::BrowserContext::GetDownloadManager( | |
2390 web_contents->GetBrowserContext()); | |
2391 | |
2392 std::unique_ptr<content::DownloadTestObserver> interrupted_observer( | |
2393 new content::DownloadTestObserverInterrupted( | |
2394 download_manager, 2, | |
2395 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | |
2396 | |
2397 EXPECT_TRUE(content::ExecuteScript( | |
2398 web_contents, | |
2399 base::StringPrintf( | |
2400 "startDownload('first', '%s?cookie=first')", | |
2401 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); | |
2402 | |
2403 EXPECT_TRUE(content::ExecuteScript( | |
2404 web_contents, | |
2405 base::StringPrintf( | |
2406 "startDownload('second', '%s?cookie=second')", | |
2407 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); | |
2408 | |
2409 // Both downloads should fail due to the HTTP_INTERNAL_SERVER_ERROR that was | |
2410 // injected above to the request handler. This maps to | |
2411 // DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED. | |
2412 interrupted_observer->WaitForFinished(); | |
2413 | |
2414 content::DownloadManager::DownloadVector downloads; | |
2415 download_manager->GetAllDownloads(&downloads); | |
2416 ASSERT_EQ(2u, downloads.size()); | |
2417 | |
2418 CloseAppWindow(GetFirstAppWindow()); | |
2419 | |
2420 std::unique_ptr<content::DownloadTestObserver> completion_observer( | |
2421 new content::DownloadTestObserverTerminal( | |
2422 download_manager, 2, | |
2423 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | |
2424 | |
2425 for (auto& download : downloads) { | |
2426 ASSERT_TRUE(download->CanResume()); | |
2427 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, | |
2428 download->GetLastReason()); | |
2429 download->Resume(); | |
2430 } | |
2431 | |
2432 completion_observer->WaitForFinished(); | |
2433 | |
2434 std::set<std::string> cookies; | |
2435 for (auto& download : downloads) { | |
2436 ASSERT_EQ(content::DownloadItem::COMPLETE, download->GetState()); | |
2437 ASSERT_TRUE(base::PathExists(download->GetTargetFilePath())); | |
2438 std::string content; | |
2439 ASSERT_TRUE( | |
2440 base::ReadFileToString(download->GetTargetFilePath(), &content)); | |
2441 // Note that the contents of the file is the value of the cookie. | |
2442 EXPECT_EQ(content, download->GetURL().query()); | |
2443 cookies.insert(content); | |
2444 } | |
2445 | |
2446 ASSERT_EQ(2u, cookies.size()); | |
2447 ASSERT_TRUE(cookies.find("cookie=first") != cookies.end()); | |
2448 ASSERT_TRUE(cookies.find("cookie=second") != cookies.end()); | |
2449 } | |
2450 | |
2451 IN_PROC_BROWSER_TEST_P(WebViewTest, PRE_DownloadCookieIsolation_CrossSession) { | |
2452 // These are the status codes to be returned by | |
2453 // HandleDownloadRequestWithCookie. The first two requests are going to result | |
2454 // in interrupted downloads. The next two requests are going to succeed. | |
2455 std::queue<net::HttpStatusCode> status_codes; | |
2456 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); | |
2457 status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR); | |
2458 | |
2459 embedded_test_server()->RegisterRequestHandler( | |
2460 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); | |
2461 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. | |
2462 LoadAndLaunchPlatformApp("web_view/download_cookie_isolation", | |
2463 "created-webviews"); | |
2464 | |
2465 content::WebContents* web_contents = GetFirstAppWindowWebContents(); | |
2466 ASSERT_TRUE(web_contents); | |
2467 | |
2468 base::ScopedTempDir temporary_download_dir; | |
2469 ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir()); | |
2470 DownloadPrefs::FromBrowserContext(web_contents->GetBrowserContext()) | |
2471 ->SetDownloadPath(temporary_download_dir.path()); | |
2472 | |
2473 content::DownloadManager* download_manager = | |
2474 content::BrowserContext::GetDownloadManager( | |
2475 web_contents->GetBrowserContext()); | |
2476 | |
2477 DownloadHistoryWaiter history_waiter(web_contents->GetBrowserContext()); | |
2478 | |
2479 std::unique_ptr<content::DownloadTestObserver> interrupted_observer( | |
2480 new content::DownloadTestObserverInterrupted( | |
2481 download_manager, 2, | |
2482 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | |
2483 | |
2484 EXPECT_TRUE(content::ExecuteScript( | |
2485 web_contents, | |
2486 base::StringPrintf( | |
2487 "startDownload('first', '%s?cookie=first')", | |
2488 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); | |
2489 | |
2490 // Note that the second webview uses an in-memory partition. | |
2491 EXPECT_TRUE(content::ExecuteScript( | |
2492 web_contents, | |
2493 base::StringPrintf( | |
2494 "startDownload('second', '%s?cookie=second')", | |
2495 embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str()))); | |
2496 | |
2497 // Both downloads should fail due to the HTTP_INTERNAL_SERVER_ERROR that was | |
2498 // injected above to the request handler. This maps to | |
2499 // DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED. | |
2500 interrupted_observer->WaitForFinished(); | |
2501 | |
2502 // Wait for both downloads to be stored. | |
2503 history_waiter.WaitForStored(2); | |
2504 | |
2505 // Leak the temporary download directory. We'll retake ownership in the next | |
2506 // browser session. | |
2507 temporary_download_dir.Take(); | |
2508 } | |
2509 | |
2510 IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadCookieIsolation_CrossSession) { | |
2511 std::queue<net::HttpStatusCode> status_codes; | |
2512 status_codes.push(net::HTTP_OK); | |
2513 status_codes.push(net::HTTP_OK); | |
2514 | |
2515 embedded_test_server()->RegisterRequestHandler( | |
2516 base::Bind(&HandleDownloadRequestWithCookie, &status_codes)); | |
2517 ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages. | |
2518 | |
2519 content::BrowserContext* browser_context = profile(); | |
2520 content::DownloadManager* download_manager = | |
2521 content::BrowserContext::GetDownloadManager(browser_context); | |
2522 | |
2523 DownloadHistoryWaiter history_waiter(browser_context); | |
2524 history_waiter.WaitForHistoryLoad(); | |
2525 | |
2526 base::ScopedTempDir temporary_download_dir; | |
2527 ASSERT_TRUE(temporary_download_dir.Set( | |
2528 DownloadPrefs::FromBrowserContext(browser_context)->DownloadPath())); | |
2529 | |
2530 content::DownloadManager::DownloadVector saved_downloads; | |
2531 download_manager->GetAllDownloads(&saved_downloads); | |
2532 ASSERT_EQ(2u, saved_downloads.size()); | |
2533 | |
2534 content::DownloadManager::DownloadVector downloads; | |
2535 // We can't trivially resume the previous downloads because they are going to | |
2536 // try to talk to the old EmbeddedTestServer instance. We need to update the | |
2537 // URL to point to the new instance, which should only differ by the port | |
2538 // number. | |
2539 for (auto& download : saved_downloads) { | |
2540 const std::string port_string = | |
2541 base::UintToString(embedded_test_server()->port()); | |
2542 url::Replacements<char> replacements; | |
2543 replacements.SetPort(port_string.c_str(), | |
2544 url::Component(0, port_string.size())); | |
2545 std::vector<GURL> url_chain; | |
2546 url_chain.push_back(download->GetURL().ReplaceComponents(replacements)); | |
2547 | |
2548 downloads.push_back(download_manager->CreateDownloadItem( | |
2549 base::GenerateGUID(), download->GetId() + 2, download->GetFullPath(), | |
2550 download->GetTargetFilePath(), url_chain, download->GetReferrerUrl(), | |
2551 download->GetSiteUrl(), download->GetTabUrl(), | |
2552 download->GetTabReferrerUrl(), download->GetMimeType(), | |
2553 download->GetOriginalMimeType(), download->GetStartTime(), | |
2554 download->GetEndTime(), download->GetETag(), | |
2555 download->GetLastModifiedTime(), download->GetReceivedBytes(), | |
2556 download->GetTotalBytes(), download->GetHash(), download->GetState(), | |
2557 download->GetDangerType(), download->GetLastReason(), | |
2558 download->GetOpened())); | |
2559 } | |
2560 | |
2561 std::unique_ptr<content::DownloadTestObserver> completion_observer( | |
2562 new content::DownloadTestObserverTerminal( | |
2563 download_manager, 2, | |
2564 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | |
2565 | |
2566 for (auto& download : downloads) { | |
2567 ASSERT_TRUE(download->CanResume()); | |
2568 ASSERT_TRUE( | |
2569 temporary_download_dir.path().IsParent(download->GetTargetFilePath())); | |
2570 ASSERT_TRUE(download->GetFullPath().empty()); | |
2571 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, | |
2572 download->GetLastReason()); | |
2573 download->Resume(); | |
2574 } | |
2575 | |
2576 completion_observer->WaitForFinished(); | |
2577 | |
2578 // Of the two downloads, ?cookie=first will succeed and ?cookie=second with | |
Charlie Reis
2016/05/10 22:50:02
nit: s/with/will/
(Thanks for testing this case!)
asanka
2016/05/11 03:14:52
Done. THanks!
| |
2579 // fail. The latter fails because the underlying storage partition was not | |
2580 // persisted. | |
2581 | |
2582 content::DownloadItem* succeeded_download = downloads[0]; | |
2583 content::DownloadItem* failed_download = downloads[1]; | |
2584 | |
2585 if (downloads[0]->GetState() == content::DownloadItem::INTERRUPTED) | |
2586 std::swap(succeeded_download, failed_download); | |
2587 | |
2588 ASSERT_EQ(content::DownloadItem::COMPLETE, succeeded_download->GetState()); | |
2589 ASSERT_TRUE(base::PathExists(succeeded_download->GetTargetFilePath())); | |
2590 std::string content; | |
2591 ASSERT_TRUE(base::ReadFileToString(succeeded_download->GetTargetFilePath(), | |
2592 &content)); | |
2593 // This is the cookie that should've been stored in the persisted storage | |
2594 // partition. | |
2595 EXPECT_STREQ("cookie=first", content.c_str()); | |
2596 | |
2597 ASSERT_EQ(content::DownloadItem::INTERRUPTED, failed_download->GetState()); | |
2598 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN, | |
2599 failed_download->GetLastReason()); | |
2238 } | 2600 } |
2239 | 2601 |
2240 // This test makes sure loading <webview> does not crash when there is an | 2602 // This test makes sure loading <webview> does not crash when there is an |
2241 // extension which has content script whitelisted/forced. | 2603 // extension which has content script whitelisted/forced. |
2242 IN_PROC_BROWSER_TEST_P(WebViewTest, WhitelistedContentScript) { | 2604 IN_PROC_BROWSER_TEST_P(WebViewTest, WhitelistedContentScript) { |
2243 // Whitelist the extension for running content script we are going to load. | 2605 // Whitelist the extension for running content script we are going to load. |
2244 extensions::ExtensionsClient::ScriptingWhitelist whitelist; | 2606 extensions::ExtensionsClient::ScriptingWhitelist whitelist; |
2245 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb"; | 2607 const std::string extension_id = "imeongpbjoodlnmlakaldhlcmijmhpbb"; |
2246 whitelist.push_back(extension_id); | 2608 whitelist.push_back(extension_id); |
2247 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); | 2609 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. | 3459 // 4 tasks expected. The order is arbitrary. |
3098 // Tab: about:blank, | 3460 // Tab: about:blank, |
3099 // Background Page: <webview> task manager test, | 3461 // Background Page: <webview> task manager test, |
3100 // App: <webview> task manager test, | 3462 // App: <webview> task manager test, |
3101 // Webview: WebViewed test content. | 3463 // Webview: WebViewed test content. |
3102 EXPECT_EQ(4U, task_manager.tasks().size()); | 3464 EXPECT_EQ(4U, task_manager.tasks().size()); |
3103 EXPECT_TRUE(HasExpectedGuestTask(task_manager, guest_contents)); | 3465 EXPECT_TRUE(HasExpectedGuestTask(task_manager, guest_contents)); |
3104 } | 3466 } |
3105 | 3467 |
3106 #endif // defined(ENABLE_TASK_MANAGER) | 3468 #endif // defined(ENABLE_TASK_MANAGER) |
OLD | NEW |