refactor: move JS dialog handling to JS (#40598)

This commit is contained in:
Jeremy Rose 2023-12-05 17:36:23 -08:00 committed by GitHub
parent ee8d97d7fe
commit 85bc005cd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 302 additions and 200 deletions

View file

@ -88,7 +88,6 @@
#include "shell/browser/electron_browser_client.h"
#include "shell/browser/electron_browser_context.h"
#include "shell/browser/electron_browser_main_parts.h"
#include "shell/browser/electron_javascript_dialog_manager.h"
#include "shell/browser/electron_navigation_throttle.h"
#include "shell/browser/file_select_helper.h"
#include "shell/browser/native_window.h"
@ -263,6 +262,21 @@ struct Converter<WindowOpenDisposition> {
}
};
template <>
struct Converter<content::JavaScriptDialogType> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
content::JavaScriptDialogType val) {
switch (val) {
case content::JAVASCRIPT_DIALOG_TYPE_ALERT:
return gin::ConvertToV8(isolate, "alert");
case content::JAVASCRIPT_DIALOG_TYPE_CONFIRM:
return gin::ConvertToV8(isolate, "confirm");
case content::JAVASCRIPT_DIALOG_TYPE_PROMPT:
return gin::ConvertToV8(isolate, "prompt");
}
}
};
template <>
struct Converter<content::SavePageType> {
static bool FromV8(v8::Isolate* isolate,
@ -1587,10 +1601,7 @@ void WebContents::RequestMediaAccessPermission(
content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
content::WebContents* source) {
if (!dialog_manager_)
dialog_manager_ = std::make_unique<ElectronJavaScriptDialogManager>();
return dialog_manager_.get();
return this;
}
void WebContents::OnAudioStateChanged(bool audible) {
@ -3747,6 +3758,45 @@ void WebContents::OnInputEvent(const blink::WebInputEvent& event) {
Emit("input-event", event);
}
void WebContents::RunJavaScriptDialog(content::WebContents* web_contents,
content::RenderFrameHost* rfh,
content::JavaScriptDialogType dialog_type,
const std::u16string& message_text,
const std::u16string& default_prompt_text,
DialogClosedCallback callback,
bool* did_suppress_message) {
CHECK_EQ(web_contents, this->web_contents());
auto* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
auto info = gin::DataObjectBuilder(isolate)
.Set("frame", rfh)
.Set("dialogType", dialog_type)
.Set("messageText", message_text)
.Set("defaultPromptText", default_prompt_text)
.Build();
EmitWithoutEvent("-run-dialog", info, std::move(callback));
}
void WebContents::RunBeforeUnloadDialog(content::WebContents* web_contents,
content::RenderFrameHost* rfh,
bool is_reload,
DialogClosedCallback callback) {
// TODO: asyncify?
bool default_prevented = Emit("will-prevent-unload");
std::move(callback).Run(default_prevented, std::u16string());
}
void WebContents::CancelDialogs(content::WebContents* web_contents,
bool reset_state) {
auto* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
EmitWithoutEvent(
"-cancel-dialogs",
gin::DataObjectBuilder(isolate).Set("resetState", reset_state).Build());
}
v8::Local<v8::Promise> WebContents::GetProcessMemoryInfo(v8::Isolate* isolate) {
gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();