Commit ade55632 authored by Dominique Fauteux-Chapleau's avatar Dominique Fauteux-Chapleau Committed by Commit Bot

Reland "Add browser tests for DeepScanningDialogDelegate"

This is a reland of 28640451

I was able to reproduce the issue on my Mac machine.
The reason for the failure was that the implementation of
WebContentsDestroyed was too naive, the intent of calling "delete this"
was to make the dialog go away without calling the "Cancel" callback.
This is now achieved by deleting the delegate first and updating
CancelButtonCallback so it becomes a no-op in this case (check diff
between Patchset 1 and 2 to see this fix).

Original change's description:
> Add browser tests for DeepScanningDialogDelegate
>
> These tests use the least possible amount of overrides to ensure
> DeepScanningDialogDelegate every requests/callbacks eventually resolves
> and returns to the caller.
>
> Also add code to handle the web contents being destroyed and avoid ASAN
> issues.
>
> Bug: 1041890
> Change-Id: Ibfe232354f0591ae9e854bf5a0163ede68d6faf9
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2083598
> Commit-Queue: Dominique Fauteux-Chapleau <domfc@chromium.org>
> Reviewed-by: Daniel Rubery <drubery@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#747713}

Bug: 1041890
Change-Id: Ie3042b0703077c20fc778450a7863ba9f26c8efc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2091217Reviewed-by: default avatarDaniel Rubery <drubery@chromium.org>
Commit-Queue: Dominique Fauteux-Chapleau <domfc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748297}
parent bb50c737
......@@ -164,7 +164,7 @@ class BinaryUploadService {
// Upload the given file contents for deep scanning. The results will be
// returned asynchronously by calling |request|'s |callback|. This must be
// called on the UI thread.
void UploadForDeepScanning(std::unique_ptr<Request> request);
virtual void UploadForDeepScanning(std::unique_ptr<Request> request);
void OnGetInstanceID(Request* request, const std::string& token);
......
......@@ -726,14 +726,17 @@ void DeepScanningDialogDelegate::FillAllResultsWith(bool status) {
std::fill(result_.paths_results.begin(), result_.paths_results.end(), status);
}
BinaryUploadService* DeepScanningDialogDelegate::GetBinaryUploadService() {
return g_browser_process->safe_browsing_service()->GetBinaryUploadService(
Profile::FromBrowserContext(web_contents_->GetBrowserContext()));
}
void DeepScanningDialogDelegate::UploadTextForDeepScanning(
std::unique_ptr<BinaryUploadService::Request> request) {
DCHECK_EQ(
DlpDeepScanningClientRequest::WEB_CONTENT_UPLOAD,
request->deep_scanning_request().dlp_scan_request().content_source());
BinaryUploadService* upload_service =
g_browser_process->safe_browsing_service()->GetBinaryUploadService(
Profile::FromBrowserContext(web_contents_->GetBrowserContext()));
BinaryUploadService* upload_service = GetBinaryUploadService();
if (upload_service)
upload_service->MaybeUploadForDeepScanning(std::move(request));
}
......@@ -745,9 +748,7 @@ void DeepScanningDialogDelegate::UploadFileForDeepScanning(
!data_.do_dlp_scan ||
(DlpDeepScanningClientRequest::FILE_UPLOAD ==
request->deep_scanning_request().dlp_scan_request().content_source()));
BinaryUploadService* upload_service =
g_browser_process->safe_browsing_service()->GetBinaryUploadService(
Profile::FromBrowserContext(web_contents_->GetBrowserContext()));
BinaryUploadService* upload_service = GetBinaryUploadService();
if (upload_service)
upload_service->MaybeUploadForDeepScanning(std::move(request));
}
......
......@@ -29,6 +29,7 @@ class Profile;
namespace safe_browsing {
class BinaryUploadService;
class DeepScanningDialogViews;
// A tab modal dialog delegate that informs the user of a background deep
......@@ -326,6 +327,10 @@ class DeepScanningDialogDelegate {
// DeepScanningFinalResult enum.
void UpdateFinalResult(DeepScanningFinalResult message);
// Returns the BinaryUploadService used to upload content for deep scanning.
// Virtual to override in tests.
virtual BinaryUploadService* GetBinaryUploadService();
// The web contents that is attempting to access the data.
content::WebContents* web_contents_ = nullptr;
......
......@@ -176,7 +176,8 @@ DeepScanningDialogViews::DeepScanningDialogViews(
content::WebContents* web_contents,
DeepScanAccessPoint access_point,
bool is_file_scan)
: delegate_(std::move(delegate)),
: content::WebContentsObserver(web_contents),
delegate_(std::move(delegate)),
web_contents_(web_contents),
access_point_(std::move(access_point)),
is_file_scan_(is_file_scan) {
......@@ -201,7 +202,7 @@ void DeepScanningDialogViews::AcceptButtonCallback() {
}
void DeepScanningDialogViews::CancelButtonCallback() {
DCHECK(delegate_);
if (delegate_)
delegate_->Cancel(is_warning());
}
......@@ -229,6 +230,13 @@ ui::ModalType DeepScanningDialogViews::GetModalType() const {
return ui::MODAL_TYPE_CHILD;
}
void DeepScanningDialogViews::WebContentsDestroyed() {
// If |web_contents_| is destroyed, then the scan results don't matter so the
// delegate can be destroyed as well.
delegate_.reset(nullptr);
CancelDialog();
}
void DeepScanningDialogViews::ShowResult(
DeepScanningDialogDelegate::DeepScanningFinalResult result) {
DCHECK(is_pending());
......
......@@ -11,6 +11,7 @@
#include "base/time/time.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_delegate.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_utils.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/controls/label.h"
#include "ui/views/window/dialog_delegate.h"
......@@ -38,7 +39,8 @@ class DeepScanningMessageView;
// Dialog shown for Deep Scanning to offer the possibility of cancelling the
// upload to the user.
class DeepScanningDialogViews : public views::DialogDelegate {
class DeepScanningDialogViews : public views::DialogDelegate,
public content::WebContentsObserver {
public:
// Enum used to represent what the dialog is currently showing.
enum class DeepScanningDialogStatus {
......@@ -119,6 +121,9 @@ class DeepScanningDialogViews : public views::DialogDelegate {
void DeleteDelegate() override;
ui::ModalType GetModalType() const override;
// content::WebContentsObserver:
void WebContentsDestroyed() override;
// Updates the dialog with the result, and simply delete it from memory if
// nothing should be shown.
void ShowResult(DeepScanningDialogDelegate::DeepScanningFinalResult result);
......
......@@ -1140,6 +1140,7 @@ if (!is_android) {
"../browser/safe_browsing/chrome_cleaner/reporter_runner_browsertest_win.cc",
"../browser/safe_browsing/cloud_content_scanning/deep_scanning_browsertest_base.cc",
"../browser/safe_browsing/cloud_content_scanning/deep_scanning_browsertest_base.h",
"../browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_delegate_browsertest.cc",
"../browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_views_browsertest.cc",
"../browser/safe_browsing/download_protection/download_protection_service_browsertest.cc",
"../browser/safe_browsing/test_safe_browsing_database_helper.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment