If anyone is interested, I ended up not using curl because in certain cases we need to do more than just post to an URL.
The way I managed to reliably call a JS function upon closing is by doing the following modifications:
browser/root_window_win.cc
- Code: Select all
// in RootWindowWin::CreateRootWindow
CHECK(hwnd_);
browser_window_->GetClientHandler()->SetRootWindowHWND(hwnd_); // sets the custom member root_window_hwnd_ in ClientHandler
...
// in RootWindowWin::RootWndProc
case WM_CLOSE:
browser = self->GetBrowser();
if (browser == NULL) {
return 0;
}
if (browser->GetIdentifier() == 1) { // Root window (blank and hidden) : do as usual
if (self->OnClose()) {
return 0; // Cancel the close.
}
}
else { // id > 1 so it's an App window
if (wParam == 0) { // Origin = user action
frame = self->GetBrowser()->GetMainFrame();
frame->ExecuteJavaScript("closeApp();", frame->GetURL(), 0);
return 0; // Cancel the close
}
else if (self->OnClose()) { // Origin = AllowClose
return 0; // Cancel the close.
}
}
break;
browser/client_handler.cc
- Code: Select all
// in ClientHandler::OnProcessMessageReceived
else if (message_name == "AllowClose") {
SendMessage(root_window_hwnd_, WM_CLOSE, 1, NULL);
return true;
}
I define the custom JS function "AllowClose"':
shared/renderer/client_app_renderer.cc
- Code: Select all
// V8Handler
bool MyV8Handler::Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
if (name == "AllowClose") {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("AllowClose");
CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
context->GetFrame()->SendProcessMessage(PID_BROWSER, message);
retval = CefV8Value::CreateBool(true);
return true;
}
// Function does not exist.
return false;
}
...
// In ClientAppRenderer::OnContextCreated
object->SetValue("AllowClose",
CefV8Value::CreateFunction("AllowClose", handler),
V8_PROPERTY_ATTRIBUTE_NONE);
Finally, in the JS app:
- Code: Select all
let closing = false;
function closeApp() {
if (!closing) {
closing = true;
// do stuff
post(...).then(AllowClose).catch(AllowClose);
}
}