test: use WebContents event to test beforeunload (#23699)

This commit is contained in:
Cheng Zhao 2020-05-26 22:21:38 +09:00 committed by GitHub
parent 0dabd5e8c7
commit 08f288faf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 97 additions and 134 deletions

View file

@ -266,6 +266,9 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) {
// Already closed by renderer
return;
// Required to make beforeunload handler work.
api_web_contents_->NotifyUserActivation();
if (web_contents()->NeedToFireBeforeUnloadOrUnload())
web_contents()->DispatchBeforeUnload(false /* auto_cancel */);
else

View file

@ -756,6 +756,8 @@ void WebContents::BeforeUnloadFired(content::WebContents* tab,
*proceed_to_fire_unload = proceed;
else
*proceed_to_fire_unload = true;
// Note that Chromium does not emit this for navigations.
Emit("before-unload-fired", proceed);
}
void WebContents::SetContentsBounds(content::WebContents* source,
@ -1546,6 +1548,9 @@ void WebContents::LoadURL(const GURL& url,
// Calling LoadURLWithParams() can trigger JS which destroys |this|.
auto weak_this = GetWeakPtr();
// Required to make beforeunload handler work.
NotifyUserActivation();
params.transition_type = ui::PAGE_TRANSITION_TYPED;
params.should_clear_history_list = true;
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
@ -2675,6 +2680,15 @@ void WebContents::GrantOriginAccess(const GURL& url) {
url::Origin::Create(url));
}
void WebContents::NotifyUserActivation() {
auto* frame = web_contents()->GetMainFrame();
if (!frame)
return;
mojo::AssociatedRemote<mojom::ElectronRenderer> renderer;
frame->GetRemoteAssociatedInterfaces()->GetInterface(&renderer);
renderer->NotifyUserActivation();
}
v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
const base::FilePath& file_path) {
gin_helper::Promise<void> promise(isolate());

View file

@ -367,6 +367,9 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
// the specified URL.
void GrantOriginAccess(const GURL& url);
// Notifies the web page that there is user interaction.
void NotifyUserActivation();
v8::Local<v8::Promise> TakeHeapSnapshot(const base::FilePath& file_path);
// Properties.

View file

@ -22,6 +22,8 @@ interface ElectronRenderer {
string context_id,
int32 object_id);
NotifyUserActivation();
TakeHeapSnapshot(handle file) => (bool success);
};

View file

@ -236,6 +236,12 @@ void ElectronApiServiceImpl::DereferenceRemoteJSCallback(
}
#endif
void ElectronApiServiceImpl::NotifyUserActivation() {
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
if (frame)
frame->NotifyUserActivation();
}
void ElectronApiServiceImpl::TakeHeapSnapshot(
mojo::ScopedHandle file,
TakeHeapSnapshotCallback callback) {

View file

@ -39,6 +39,7 @@ class ElectronApiServiceImpl : public mojom::ElectronRenderer,
void DereferenceRemoteJSCallback(const std::string& context_id,
int32_t object_id) override;
#endif
void NotifyUserActivation() override;
void TakeHeapSnapshot(mojo::ScopedHandle file,
TakeHeapSnapshotCallback callback) override;