Added JavascriptDialogHandler (Issue 118). · lineCode/cefpython@2504ef3 · GitHub
Skip to content

Commit 2504ef3

Browse files
committed
Added JavascriptDialogHandler (Issue 118).
Redesigned the wxpython.py example on Linux. Added table of contents and source code highlighting.
1 parent 7ff8973 commit 2504ef3

19 files changed

Lines changed: 1434 additions & 640 deletions

cefpython/browser.pyx

Lines changed: 6 additions & 0 deletions

cefpython/cef3/client_handler/client_handler.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,76 @@ void ClientHandler::OnScrollOffsetChanged(CefRefPtr<CefBrowser> browser) {
703703
REQUIRE_UI_THREAD();
704704
RenderHandler_OnScrollOffsetChanged(browser);
705705
}
706+
707+
708+
// ----------------------------------------------------------------------------
709+
// CefJSDialogHandler
710+
// ----------------------------------------------------------------------------
711+
///
712+
// Called to run a JavaScript dialog. The |default_prompt_text| value will be
713+
// specified for prompt dialogs only. Set |suppress_message| to true and
714+
// return false to suppress the message (suppressing messages is preferable
715+
// to immediately executing the callback as this is used to detect presumably
716+
// malicious behavior like spamming alert messages in onbeforeunload). Set
717+
// |suppress_message| to false and return false to use the default
718+
// implementation (the default implementation will show one modal dialog at a
719+
// time and suppress any additional dialog requests until the displayed dialog
720+
// is dismissed). Return true if the application will use a custom dialog or
721+
// if the callback has been executed immediately. Custom dialogs may be either
722+
// modal or modeless. If a custom dialog is used the application must execute
723+
// |callback| once the custom dialog is dismissed.
724+
///
725+
/*--cef(optional_param=accept_lang,optional_param=message_text,
726+
optional_param=default_prompt_text)--*/
727+
bool ClientHandler::OnJSDialog(CefRefPtr<CefBrowser> browser,
728+
const CefString& origin_url,
729+
const CefString& accept_lang,
730+
JSDialogType dialog_type,
731+
const CefString& message_text,
732+
const CefString& default_prompt_text,
733+
CefRefPtr<CefJSDialogCallback> callback,
734+
bool& suppress_message) {
735+
REQUIRE_UI_THREAD();
736+
return JavascriptDialogHandler_OnJavascriptDialog(browser, origin_url,
737+
accept_lang, dialog_type, message_text, default_prompt_text,
738+
callback, suppress_message);
739+
}
740+
741+
///
742+
// Called to run a dialog asking the user if they want to leave a page. Return
743+
// false to use the default dialog implementation. Return true if the
744+
// application will use a custom dialog or if the callback has been executed
745+
// immediately. Custom dialogs may be either modal or modeless. If a custom
746+
// dialog is used the application must execute |callback| once the custom
747+
// dialog is dismissed.
748+
///
749+
/*--cef(optional_param=message_text)--*/
750+
bool ClientHandler::OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser,
751+
const CefString& message_text,
752+
bool is_reload,
753+
CefRefPtr<CefJSDialogCallback> callback) {
754+
REQUIRE_UI_THREAD();
755+
return JavascriptDialogHandler_OnBeforeUnloadJavascriptDialog(browser,
756+
message_text, is_reload, callback);
757+
}
758+
759+
///
760+
// Called to cancel any pending dialogs and reset any saved dialog state. Will
761+
// be called due to events like page navigation irregardless of whether any
762+
// dialogs are currently pending.
763+
///
764+
/*--cef()--*/
765+
void ClientHandler::OnResetDialogState(CefRefPtr<CefBrowser> browser) {
766+
REQUIRE_UI_THREAD();
767+
return JavascriptDialogHandler_OnResetJavascriptDialogState(browser);
768+
}
769+
770+
///
771+
// Called when the default implementation dialog is closed.
772+
///
773+
/*--cef()--*/
774+
void ClientHandler::OnDialogClosed(CefRefPtr<CefBrowser> browser) {
775+
REQUIRE_UI_THREAD();
776+
return JavascriptDialogHandler_OnJavascriptDialogClosed(browser);
777+
}
778+

cefpython/cef3/client_handler/client_handler.h

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class ClientHandler :
1717
public CefKeyboardHandler,
1818
public CefRequestHandler,
1919
public CefLoadHandler,
20-
public CefRenderHandler
20+
public CefRenderHandler,
21+
public CefJSDialogHandler
2122
{
2223
public:
2324
ClientHandler(){}
@@ -89,7 +90,7 @@ class ClientHandler :
8990
///
9091
/*--cef()--*/
9192
virtual CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() OVERRIDE {
92-
return NULL;
93+
return this;
9394
}
9495

9596
///
@@ -640,6 +641,66 @@ class ClientHandler :
640641
/*--cef()--*/
641642
virtual void OnScrollOffsetChanged(CefRefPtr<CefBrowser> browser) OVERRIDE;
642643

644+
// --------------------------------------------------------------------------
645+
// CefJSDialogHandler
646+
// --------------------------------------------------------------------------
647+
typedef cef_jsdialog_type_t JSDialogType;
648+
649+
///
650+
// Called to run a JavaScript dialog. The |default_prompt_text| value will be
651+
// specified for prompt dialogs only. Set |suppress_message| to true and
652+
// return false to suppress the message (suppressing messages is preferable
653+
// to immediately executing the callback as this is used to detect presumably
654+
// malicious behavior like spamming alert messages in onbeforeunload). Set
655+
// |suppress_message| to false and return false to use the default
656+
// implementation (the default implementation will show one modal dialog at a
657+
// time and suppress any additional dialog requests until the displayed dialog
658+
// is dismissed). Return true if the application will use a custom dialog or
659+
// if the callback has been executed immediately. Custom dialogs may be either
660+
// modal or modeless. If a custom dialog is used the application must execute
661+
// |callback| once the custom dialog is dismissed.
662+
///
663+
/*--cef(optional_param=accept_lang,optional_param=message_text,
664+
optional_param=default_prompt_text)--*/
665+
virtual bool OnJSDialog(CefRefPtr<CefBrowser> browser,
666+
const CefString& origin_url,
667+
const CefString& accept_lang,
668+
JSDialogType dialog_type,
669+
const CefString& message_text,
670+
const CefString& default_prompt_text,
671+
CefRefPtr<CefJSDialogCallback> callback,
672+
bool& suppress_message) OVERRIDE;
673+
674+
///
675+
// Called to run a dialog asking the user if they want to leave a page. Return
676+
// false to use the default dialog implementation. Return true if the
677+
// application will use a custom dialog or if the callback has been executed
678+
// immediately. Custom dialogs may be either modal or modeless. If a custom
679+
// dialog is used the application must execute |callback| once the custom
680+
// dialog is dismissed.
681+
///
682+
/*--cef(optional_param=message_text)--*/
683+
virtual bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser,
684+
const CefString& message_text,
685+
bool is_reload,
686+
CefRefPtr<CefJSDialogCallback> callback)
687+
OVERRIDE;
688+
689+
///
690+
// Called to cancel any pending dialogs and reset any saved dialog state. Will
691+
// be called due to events like page navigation irregardless of whether any
692+
// dialogs are currently pending.
693+
///
694+
/*--cef()--*/
695+
virtual void OnResetDialogState(CefRefPtr<CefBrowser> browser) OVERRIDE;
696+
697+
///
698+
// Called when the default implementation dialog is closed.
699+
///
700+
/*--cef()--*/
701+
virtual void OnDialogClosed(CefRefPtr<CefBrowser> browser) OVERRIDE;
702+
703+
643704
private:
644705

645706
// Include the default reference counting implementation.

cefpython/cef3/linux/binaries_32bit/kivy_.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,19 @@ def GetViewRect(self, browser, rect):
656656
return True
657657

658658

659+
def OnJavascriptDialog(self, browser, originUrl, acceptLang, dialogType,
660+
messageText, defaultPromptText, callback,
661+
suppressMessage):
662+
suppressMessage[0] = True
663+
return False
664+
665+
666+
def OnBeforeUnloadJavascriptDialog(self, browser, messageText, isReload,
667+
callback):
668+
callback.Continue(allow=True, userInput="")
669+
return True
670+
671+
659672
if __name__ == '__main__':
660673
class CefBrowserApp(App):
661674
def build(self):
Lines changed: 130 additions & 0 deletions

cefpython/cef3/linux/binaries_32bit/prism.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)