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

File tree

19 files changed

+1434
-640
lines changed

19 files changed

+1434
-640
lines changed

cefpython/browser.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ cdef class PyBrowser:
236236
"GetViewRect", "GetScreenPoint", "GetScreenInfo",
237237
"OnPopupShow", "OnPopupSize", "OnPaint", "OnCursorChange",
238238
"OnScrollOffsetChanged"]
239+
# JavascriptDialogHandler
240+
self.allowedClientCallbacks += ["OnJavascriptDialog",
241+
"OnBeforeUnloadJavascriptDialog",
242+
"OnResetJavascriptDialogState",
243+
"OnJavascriptDialogClosed"]
244+
239245
if name not in self.allowedClientCallbacks:
240246
raise Exception("Browser.SetClientCallback() failed: unknown "
241247
"callback: %s" % name)

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/* http://prismjs.com/download.html?themes=prism&languages=clike+javascript+python */
2+
/**
3+
* prism.js default theme for JavaScript, CSS and HTML
4+
* Based on dabblet (http://dabblet.com)
5+
* @author Lea Verou
6+
*/
7+
8+
code[class*="language-"],
9+
pre[class*="language-"] {
10+
color: black;
11+
text-shadow: 0 1px white;
12+
font-family: Consolas, Monaco, 'Andale Mono', monospace;
13+
direction: ltr;
14+
text-align: left;
15+
white-space: pre;
16+
word-spacing: normal;
17+
word-break: normal;
18+
19+
20+
-moz-tab-size: 4;
21+
-o-tab-size: 4;
22+
tab-size: 4;
23+
24+
-webkit-hyphens: none;
25+
-moz-hyphens: none;
26+
-ms-hyphens: none;
27+
hyphens: none;
28+
}
29+
30+
pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
31+
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
32+
text-shadow: none;
33+
background: #b3d4fc;
34+
}
35+
36+
pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
37+
code[class*="language-"]::selection, code[class*="language-"] ::selection {
38+
text-shadow: none;
39+
background: #b3d4fc;
40+
}
41+
42+
@media print {
43+
code[class*="language-"],
44+
pre[class*="language-"] {
45+
text-shadow: none;
46+
}
47+
}
48+
49+
/* Code blocks */
50+
pre[class*="language-"] {
51+
padding: 1em;
52+
margin: .5em 0;
53+
overflow: auto;
54+
}
55+
56+
:not(pre) > code[class*="language-"],
57+
pre[class*="language-"] {
58+
background: #f5f2f0;
59+
}
60+
61+
/* Inline code */
62+
:not(pre) > code[class*="language-"] {
63+
padding: .1em;
64+
border-radius: .3em;
65+
}
66+
67+
.token.comment,
68+
.token.prolog,
69+
.token.doctype,
70+
.token.cdata {
71+
color: slategray;
72+
}
73+
74+
.token.punctuation {
75+
color: #999;
76+
}
77+
78+
.namespace {
79+
opacity: .7;
80+
}
81+
82+
.token.property,
83+
.token.tag,
84+
.token.boolean,
85+
.token.number,
86+
.token.constant,
87+
.token.symbol {
88+
color: #905;
89+
}
90+
91+
.token.selector,
92+
.token.attr-name,
93+
.token.string,
94+
.token.builtin {
95+
color: #690;
96+
}
97+
98+
.token.operator,
99+
.token.entity,
100+
.token.url,
101+
.language-css .token.string,
102+
.style .token.string,
103+
.token.variable {
104+
color: #a67f59;
105+
background: hsla(0,0%,100%,.5);
106+
}
107+
108+
.token.atrule,
109+
.token.attr-value,
110+
.token.keyword {
111+
color: #07a;
112+
}
113+
114+
.token.function {
115+
color: #DD4A68;
116+
}
117+
118+
.token.regex,
119+
.token.important {
120+
color: #e90;
121+
}
122+
123+
.token.important {
124+
font-weight: bold;
125+
}
126+
127+
.token.entity {
128+
cursor: help;
129+
}
130+

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)