blob: 8f578664f7267e92702ec711f9c8e92cb298e5b8 [file] [log] [blame]
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "content/browser/download/download_manager_impl.h"
#include "content/browser/download/save_package.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_download_manager_delegate.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
namespace content {
const char kTestFile[] = "/simple_page.html";
class SavePackageBrowserTest : public ContentBrowserTest {
protected:
void SetUp() override {
ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
ContentBrowserTest::SetUp();
}
// Returns full paths of destination file and directory.
void GetDestinationPaths(const std::string& prefix,
base::FilePath* full_file_name,
base::FilePath* dir) {
*full_file_name = save_dir_.GetPath().AppendASCII(prefix + ".htm");
*dir = save_dir_.GetPath().AppendASCII(prefix + "_files");
}
// Temporary directory we will save pages to.
base::ScopedTempDir save_dir_;
};
// Create a SavePackage and delete it without calling Init.
// SavePackage dtor has various asserts/checks that should not fire.
IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, ImplicitCancel) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url = embedded_test_server()->GetURL(kTestFile);
NavigateToURL(shell(), url);
base::FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
scoped_refptr<SavePackage> save_package(new SavePackage(
shell()->web_contents(), SAVE_PAGE_TYPE_AS_ONLY_HTML, full_file_name,
dir));
}
// Create a SavePackage, call Cancel, then delete it.
// SavePackage dtor has various asserts/checks that should not fire.
IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, ExplicitCancel) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url = embedded_test_server()->GetURL(kTestFile);
NavigateToURL(shell(), url);
base::FilePath full_file_name, dir;
GetDestinationPaths("a", &full_file_name, &dir);
scoped_refptr<SavePackage> save_package(new SavePackage(
shell()->web_contents(), SAVE_PAGE_TYPE_AS_ONLY_HTML, full_file_name,
dir));
save_package->Cancel(true);
}
IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, DownloadItemDestroyed) {
class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
public:
void ChooseSavePath(
WebContents* web_contents,
const base::FilePath& suggested_path,
const base::FilePath::StringType& default_extension,
bool can_save_as_complete,
const SavePackagePathPickedCallback& callback) override {
callback.Run(suggested_path, SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
SavePackageDownloadCreatedCallback());
}
void GetSaveDir(BrowserContext* context,
base::FilePath* website_save_dir,
base::FilePath* download_save_dir,
bool* skip_dir_check) override {
*website_save_dir = download_dir_;
*download_save_dir = download_dir_;
*skip_dir_check = false;
}
bool ShouldCompleteDownload(DownloadItem* download,
const base::Closure& closure) override {
return true;
}
base::FilePath download_dir_;
};
class DownloadicidalObserver : public DownloadManager::Observer {
public:
void OnDownloadCreated(DownloadManager* manager,
DownloadItem* item) override {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(
[](const base::Closure& closure, DownloadItem* item) {
item->Remove();
closure.Run();
},
quit_closure_, item));
}
base::Closure quit_closure_;
};
ASSERT_TRUE(embedded_test_server()->Start());
GURL url = embedded_test_server()->GetURL("/page_with_iframe.html");
NavigateToURL(shell(), url);
auto* download_manager =
static_cast<DownloadManagerImpl*>(BrowserContext::GetDownloadManager(
shell()->web_contents()->GetBrowserContext()));
auto delegate = base::MakeUnique<TestShellDownloadManagerDelegate>();
delegate->download_dir_ = save_dir_.GetPath();
auto* old_delegate = download_manager->GetDelegate();
download_manager->SetDelegate(delegate.get());
{
base::RunLoop run_loop;
DownloadicidalObserver download_item_killer;
download_manager->AddObserver(&download_item_killer);
download_item_killer.quit_closure_ = run_loop.QuitClosure();
scoped_refptr<SavePackage> save_package(
new SavePackage(shell()->web_contents()));
save_package->GetSaveInfo();
run_loop.Run();
download_manager->RemoveObserver(&download_item_killer);
EXPECT_TRUE(save_package->canceled());
}
class SavePackageCompletionWaiter : public DownloadManager::Observer {
public:
void OnSavePackageSuccessfullyFinished(DownloadManager* m,
DownloadItem* d) override {
quit_closure_.Run();
}
base::Closure quit_closure_;
};
// Run a second download to completion so that any pending tasks will get
// flushed out. If the previous SavePackage operation didn't cleanup after
// itself, then there could be stray tasks that invoke the now defunct
// download item.
{
base::RunLoop run_loop;
SavePackageCompletionWaiter completion_waiter;
completion_waiter.quit_closure_ = run_loop.QuitClosure();
download_manager->AddObserver(&completion_waiter);
shell()->web_contents()->OnSavePage();
run_loop.Run();
download_manager->RemoveObserver(&completion_waiter);
}
download_manager->SetDelegate(old_delegate);
}
} // namespace content