| Index: chrome/browser/apps/guest_view/web_view_browsertest.cc
|
| diff --git a/chrome/browser/apps/guest_view/web_view_browsertest.cc b/chrome/browser/apps/guest_view/web_view_browsertest.cc
|
| index 4b350c318dc0e56939615760a7890f953790edee..db4d061aa8840e950dd6f70035ae798e5e80bdc0 100644
|
| --- a/chrome/browser/apps/guest_view/web_view_browsertest.cc
|
| +++ b/chrome/browser/apps/guest_view/web_view_browsertest.cc
|
| @@ -3,8 +3,11 @@
|
| // found in the LICENSE file.
|
|
|
| #include <queue>
|
| +#include <set>
|
| #include <utility>
|
|
|
| +#include "base/files/file_util.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| #include "base/location.h"
|
| #include "base/macros.h"
|
| #include "base/path_service.h"
|
| @@ -18,6 +21,7 @@
|
| #include "chrome/browser/apps/app_browsertest_util.h"
|
| #include "chrome/browser/chrome_content_browser_client.h"
|
| #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
|
| +#include "chrome/browser/download/download_prefs.h"
|
| #include "chrome/browser/lifetime/application_lifetime.h"
|
| #include "chrome/browser/pdf/pdf_extension_test_util.h"
|
| #include "chrome/browser/prerender/prerender_link_manager.h"
|
| @@ -48,6 +52,7 @@
|
| #include "content/public/common/child_process_host.h"
|
| #include "content/public/common/content_switches.h"
|
| #include "content/public/test/browser_test_utils.h"
|
| +#include "content/public/test/download_test_observer.h"
|
| #include "content/public/test/fake_speech_recognition_manager.h"
|
| #include "content/public/test/test_renderer_host.h"
|
| #include "extensions/browser/api/declarative/rules_registry.h"
|
| @@ -309,7 +314,7 @@ class MockDownloadWebContentsDelegate : public content::WebContentsDelegate {
|
| orig_delegate_->CanDownload(
|
| url, request_method,
|
| base::Bind(&MockDownloadWebContentsDelegate::DownloadDecided,
|
| - base::Unretained(this)));
|
| + base::Unretained(this), callback));
|
| }
|
|
|
| void WaitForCanDownload(bool expect_allow) {
|
| @@ -326,7 +331,7 @@ class MockDownloadWebContentsDelegate : public content::WebContentsDelegate {
|
| message_loop_runner_->Run();
|
| }
|
|
|
| - void DownloadDecided(bool allow) {
|
| + void DownloadDecided(const base::Callback<void(bool)>& callback, bool allow) {
|
| EXPECT_FALSE(decision_made_);
|
| decision_made_ = true;
|
|
|
| @@ -334,9 +339,11 @@ class MockDownloadWebContentsDelegate : public content::WebContentsDelegate {
|
| EXPECT_EQ(expect_allow_, allow);
|
| if (message_loop_runner_.get())
|
| message_loop_runner_->Quit();
|
| + callback.Run(allow);
|
| return;
|
| }
|
| last_download_allowed_ = allow;
|
| + callback.Run(allow);
|
| }
|
|
|
| void Reset() {
|
| @@ -2211,6 +2218,17 @@ IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) {
|
| "web_view/download");
|
| ASSERT_TRUE(guest_web_contents);
|
|
|
| + base::ScopedTempDir temporary_download_dir;
|
| + ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir());
|
| + DownloadPrefs::FromBrowserContext(guest_web_contents->GetBrowserContext())
|
| + ->SetDownloadPath(temporary_download_dir.path());
|
| +
|
| + std::unique_ptr<content::DownloadTestObserver> completion_observer(
|
| + new content::DownloadTestObserverTerminal(
|
| + content::BrowserContext::GetDownloadManager(
|
| + guest_web_contents->GetBrowserContext()),
|
| + 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
|
| +
|
| // Replace WebContentsDelegate with mock version so we can intercept download
|
| // requests.
|
| content::WebContentsDelegate* delegate = guest_web_contents->GetDelegate();
|
| @@ -2235,6 +2253,140 @@ IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadPermission) {
|
| EXPECT_TRUE(content::ExecuteScript(guest_web_contents,
|
| "startDownload('download-link-3')"));
|
| mock_delegate->WaitForCanDownload(false); // Expect to not allow.
|
| + completion_observer->WaitForFinished();
|
| +}
|
| +
|
| +namespace {
|
| +
|
| +const char kDownloadPathPrefix[] = "/download_cookie_isolation_test";
|
| +
|
| +// EmbeddedTestServer request handler for use with DownloadCookieIsolation test.
|
| +// Responds with the next status code in |status_codes| if the 'Cookie' header
|
| +// sent with the request matches the query() part of the URL. Otherwise, fails
|
| +// the request with an HTTP 403. The body of the response is the value of the
|
| +// Cookie header.
|
| +std::unique_ptr<net::test_server::HttpResponse> HandleDownloadRequestWithCookie(
|
| + std::queue<net::HttpStatusCode>* status_codes,
|
| + const net::test_server::HttpRequest& request) {
|
| + if (request.relative_url.find(kDownloadPathPrefix) != 0) {
|
| + return std::unique_ptr<net::test_server::HttpResponse>();
|
| + }
|
| +
|
| + std::string cookie_to_expect = request.GetURL().query();
|
| + const auto cookie_header_it = request.headers.find("cookie");
|
| + std::unique_ptr<net::test_server::BasicHttpResponse> response;
|
| +
|
| + // Return a 403 if there's no cookie or if the cookie doesn't match.
|
| + if (cookie_header_it == request.headers.end() ||
|
| + cookie_header_it->second != cookie_to_expect) {
|
| + response.reset(new net::test_server::BasicHttpResponse);
|
| + response->set_code(net::HTTP_FORBIDDEN);
|
| + response->set_content_type("text/plain");
|
| + response->set_content("Forbidden");
|
| + return std::move(response);
|
| + }
|
| +
|
| + DCHECK(!status_codes->empty());
|
| +
|
| + // We have a cookie. Send some content along with the next status code.
|
| + response.reset(new net::test_server::BasicHttpResponse);
|
| + response->set_code(status_codes->front());
|
| + response->set_content_type("application/octet-stream");
|
| + response->set_content(cookie_to_expect);
|
| + status_codes->pop();
|
| + return std::move(response);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +// Downloads initiated from isolated guest parititons should use their
|
| +// respective cookie stores. In addition, if those downloads are resumed, they
|
| +// should continue to use their respective cookie stores.
|
| +IN_PROC_BROWSER_TEST_P(WebViewTest, DownloadCookieIsolation) {
|
| + // These are the status codes to be returned by
|
| + // HandleDownloadRequestWithCookie. The first two requests are going to result
|
| + // in interrupted downloads. The next two requests are going to succeed.
|
| + std::queue<net::HttpStatusCode> status_codes;
|
| + status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR);
|
| + status_codes.push(net::HTTP_INTERNAL_SERVER_ERROR);
|
| + status_codes.push(net::HTTP_OK);
|
| + status_codes.push(net::HTTP_OK);
|
| +
|
| + embedded_test_server()->RegisterRequestHandler(
|
| + base::Bind(&HandleDownloadRequestWithCookie, &status_codes));
|
| + ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
|
| + LoadAndLaunchPlatformApp("web_view/download_cookie_isolation",
|
| + "created-webviews");
|
| +
|
| + content::WebContents* web_contents = GetFirstAppWindowWebContents();
|
| + ASSERT_TRUE(web_contents);
|
| +
|
| + base::ScopedTempDir temporary_download_dir;
|
| + ASSERT_TRUE(temporary_download_dir.CreateUniqueTempDir());
|
| + DownloadPrefs::FromBrowserContext(web_contents->GetBrowserContext())
|
| + ->SetDownloadPath(temporary_download_dir.path());
|
| +
|
| + content::DownloadManager* download_manager =
|
| + content::BrowserContext::GetDownloadManager(
|
| + web_contents->GetBrowserContext());
|
| +
|
| + std::unique_ptr<content::DownloadTestObserver> interrupted_observer(
|
| + new content::DownloadTestObserverInterrupted(
|
| + download_manager, 2,
|
| + content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
|
| +
|
| + EXPECT_TRUE(content::ExecuteScript(
|
| + web_contents,
|
| + base::StringPrintf(
|
| + "startDownload('first', '%s?cookie=first')",
|
| + embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str())));
|
| +
|
| + EXPECT_TRUE(content::ExecuteScript(
|
| + web_contents,
|
| + base::StringPrintf(
|
| + "startDownload('second', '%s?cookie=second')",
|
| + embedded_test_server()->GetURL(kDownloadPathPrefix).spec().c_str())));
|
| +
|
| + // Both downloads should fail due to the HTTP_INTERNAL_SERVER_ERROR that was
|
| + // injected above to the request handler. This maps to
|
| + // DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED.
|
| + interrupted_observer->WaitForFinished();
|
| +
|
| + content::DownloadManager::DownloadVector downloads;
|
| + download_manager->GetAllDownloads(&downloads);
|
| + ASSERT_EQ(2u, downloads.size());
|
| +
|
| + CloseAppWindow(GetFirstAppWindow());
|
| +
|
| + std::unique_ptr<content::DownloadTestObserver> completion_observer(
|
| + new content::DownloadTestObserverTerminal(
|
| + download_manager, 2,
|
| + content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
|
| +
|
| + for (auto& download : downloads) {
|
| + ASSERT_TRUE(download->CanResume());
|
| + EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED,
|
| + download->GetLastReason());
|
| + download->Resume();
|
| + }
|
| +
|
| + completion_observer->WaitForFinished();
|
| +
|
| + std::set<std::string> cookies;
|
| + for (auto& download : downloads) {
|
| + ASSERT_EQ(content::DownloadItem::COMPLETE, download->GetState());
|
| + ASSERT_TRUE(base::PathExists(download->GetTargetFilePath()));
|
| + std::string content;
|
| + ASSERT_TRUE(
|
| + base::ReadFileToString(download->GetTargetFilePath(), &content));
|
| + // Note that the contents of the file is the value of the cookie.
|
| + EXPECT_EQ(content, download->GetURL().query());
|
| + cookies.insert(content);
|
| + }
|
| +
|
| + ASSERT_EQ(2u, cookies.size());
|
| + ASSERT_TRUE(cookies.find("cookie=first") != cookies.end());
|
| + ASSERT_TRUE(cookies.find("cookie=second") != cookies.end());
|
| }
|
|
|
| // This test makes sure loading <webview> does not crash when there is an
|
|
|