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

Side by Side Diff: chrome/browser/apps/guest_view/web_view_browsertest.cc

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

Powered by Google App Engine
This is Rietveld 408576698