FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cefpython/src/client_handler/dialog_handler_gtk.cpp at cefpython57 · netdebug/cefpython · GitHub
netdebug
cefpython
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cefpython
/
src
/
client_handler
/
dialog_handler_gtk.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
468 lines (397 loc) · 14.3 KB
Breadcrumbs
cefpython
/
src
/
client_handler
/
dialog_handler_gtk.cpp
Copy path
File metadata and controls
468 lines (397 loc) · 14.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
Default dialog handler implementation on Linux.
//
Copied from upstream cefclient with changes:
//
- Rewrote GetWindow() func
//
- Removed "client" namespace
//
- Changed titles of JS alerts, removed URL and "Javascript" word
//
Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
//
reserved. Use of this source code is governed by a BSD-style license that
//
can be found in the LICENSE file.
#
include
<
libgen.h
>
#
include
<
sys/stat.h
>
#
include
<
X11/Xlib.h
>
#
include
<
gtk/gtk.h
>
#
include
<
gdk/gdkx.h
>
#
include
"
include/cef_browser.h
"
#
include
"
include/cef_parser.h
"
#
include
"
include/wrapper/cef_helpers.h
"
#
include
"
include/base/cef_logging.h
"
#
include
"
dialog_handler_gtk.h
"
#
include
"
x11.h
"
namespace
{
const
char
kPromptTextId
[] =
"
cef_prompt_text
"
;
//
If there's a text entry in the dialog, get the text from the first one and
//
return it.
std::string
GetPromptText
(GtkDialog* dialog) {
GtkWidget* widget =
static_cast
<GtkWidget*>(
g_object_get_data
(
G_OBJECT
(dialog),
kPromptTextId
));
if
(widget)
return
gtk_entry_get_text
(
GTK_ENTRY
(widget));
return
std::string
();
}
std::string
GetDescriptionFromMimeType
(
const
std::string& mime_type) {
//
Check for wild card mime types and return an appropriate description.
static
const
struct
{
const
char
* mime_type;
const
char
* label;
}
kWildCardMimeTypes
[] = {
{
"
audio
"
,
"
Audio Files
"
},
{
"
image
"
,
"
Image Files
"
},
{
"
text
"
,
"
Text Files
"
},
{
"
video
"
,
"
Video Files
"
},
};
for
(
size_t
i =
0
;
i <
sizeof
(
kWildCardMimeTypes
) /
sizeof
(
kWildCardMimeTypes
[
0
]); ++i) {
if
(mime_type ==
std::string
(
kWildCardMimeTypes
[i].
mime_type
) +
"
/*
"
)
return
std::string
(
kWildCardMimeTypes
[i].
label
);
}
return
std::string
();
}
void
AddFilters
(GtkFileChooser* chooser,
const
std::vector<CefString>& accept_filters,
bool
include_all_files,
std::vector<GtkFileFilter*>* filters) {
bool
has_filter =
false
;
for
(
size_t
i =
0
; i < accept_filters.
size
(); ++i) {
const
std::string& filter = accept_filters[i];
if
(filter.
empty
())
continue
;
std::vector<std::string> extensions;
std::string description;
size_t
sep_index = filter.
find
(
'
|
'
);
if
(sep_index != std::string::npos) {
//
Treat as a filter of the form "Filter Name|.ext1;.ext2;.ext3".
description = filter.
substr
(
0
, sep_index);
const
std::string& exts = filter.
substr
(sep_index +
1
);
size_t
last =
0
;
size_t
size = exts.
size
();
for
(
size_t
i =
0
; i <= size; ++i) {
if
(i == size || exts[i] ==
'
;
'
) {
std::string
ext
(exts, last, i - last);
if
(!ext.
empty
() && ext[
0
] ==
'
.
'
)
extensions.
push_back
(ext);
last = i +
1
;
}
}
}
else
if
(filter[
0
] ==
'
.
'
) {
//
Treat as an extension beginning with the '.' character.
extensions.
push_back
(filter);
}
else
{
//
Otherwise convert mime type to one or more extensions.
description =
GetDescriptionFromMimeType
(filter);
std::vector<CefString> ext;
CefGetExtensionsForMimeType
(filter, ext);
for
(
size_t
x =
0
; x < ext.
size
(); ++x)
extensions.
push_back
(
"
.
"
+ ext[x].
ToString
());
}
if
(extensions.
empty
())
continue
;
GtkFileFilter* gtk_filter =
gtk_file_filter_new
();
std::string ext_str;
for
(
size_t
x =
0
; x < extensions.
size
(); ++x) {
const
std::string& pattern =
"
*
"
+ extensions[x];
if
(x !=
0
)
ext_str +=
"
;
"
;
ext_str += pattern;
gtk_file_filter_add_pattern
(gtk_filter, pattern.
c_str
());
}
if
(description.
empty
())
description = ext_str;
else
description +=
"
(
"
+ ext_str +
"
)
"
;
gtk_file_filter_set_name
(gtk_filter, description.
c_str
());
gtk_file_chooser_add_filter
(chooser, gtk_filter);
if
(!has_filter)
has_filter =
true
;
filters->
push_back
(gtk_filter);
}
//
Add the *.* filter, but only if we have added other filters (otherwise it
//
is implied).
if
(include_all_files && has_filter) {
GtkFileFilter* filter =
gtk_file_filter_new
();
gtk_file_filter_add_pattern
(filter,
"
*
"
);
gtk_file_filter_set_name
(filter,
"
All Files (*)
"
);
gtk_file_chooser_add_filter
(chooser, filter);
}
}
GtkWindow*
GetWindow
(CefRefPtr<CefBrowser> browser) {
//
-- REWRITTEN FOR CEF PYTHON USE CASE --
//
X11 window handle
::Window xwindow = browser->
GetHost
()->
GetWindowHandle
();
//
X11 display
::Display* xdisplay =
cef_get_xdisplay
();
//
GDK display
GdkDisplay* gdk_display =
NULL
;
if
(xdisplay) {
//
See if we can find GDK display using X11 display
gdk_display =
gdk_x11_lookup_xdisplay
(xdisplay);
}
if
(!gdk_display) {
//
If not then get the default display
gdk_display =
gdk_display_get_default
();
}
if
(!gdk_display) {
//
The tkinter_.py and hello_world.py examples do not use GTK
//
internally, so GTK wasn't yet initialized and must do it
//
now, so that display is available. Also must install X11
//
error handlers to avoid 'BadWindow' errors.
LOG
(
INFO
) <<
"
[Browser process] Initialize GTK
"
;
gtk_init
(
0
,
NULL
);
InstallX11ErrorHandlers
();
//
Now the display is available
gdk_display =
gdk_display_get_default
();
}
//
In kivy_.py example getting error message:
//
> Can't create GtkPlug as child of non-GtkSocket
//
However dialog handler works just fine.
GtkWidget* widget =
gtk_plug_new_for_display
(gdk_display, xwindow);
//
Getting top level widget doesn't seem to be required.
//
OFF: GtkWidget* toplevel = gtk_widget_get_toplevel(widget);
GtkWindow* window =
GTK_WINDOW
(widget);
if
(!window) {
LOG
(
ERROR
) <<
"
No GtkWindow for browser
"
;
}
return
window;
}
}
//
namespace
ClientDialogHandlerGtk::ClientDialogHandlerGtk
()
: gtk_dialog_(
NULL
) {
}
bool
ClientDialogHandlerGtk::OnFileDialog
(
CefRefPtr<CefBrowser> browser,
FileDialogMode mode,
const
CefString& title,
const
CefString& default_file_path,
const
std::vector<CefString>& accept_filters,
int
selected_accept_filter,
CefRefPtr<CefFileDialogCallback> callback) {
std::vector<CefString> files;
GtkFileChooserAction action;
const
gchar* accept_button;
//
Remove any modifier flags.
FileDialogMode mode_type =
static_cast
<FileDialogMode>(mode &
FILE_DIALOG_TYPE_MASK
);
if
(mode_type ==
FILE_DIALOG_OPEN
|| mode_type ==
FILE_DIALOG_OPEN_MULTIPLE
) {
action =
GTK_FILE_CHOOSER_ACTION_OPEN
;
accept_button =
GTK_STOCK_OPEN
;
}
else
if
(mode_type ==
FILE_DIALOG_OPEN_FOLDER
) {
action =
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
accept_button =
GTK_STOCK_OPEN
;
}
else
if
(mode_type ==
FILE_DIALOG_SAVE
) {
action =
GTK_FILE_CHOOSER_ACTION_SAVE
;
accept_button =
GTK_STOCK_SAVE
;
}
else
{
NOTREACHED
();
return
false
;
}
std::string title_str;
if
(!title.
empty
()) {
title_str = title;
}
else
{
switch
(mode_type) {
case
FILE_DIALOG_OPEN
:
title_str =
"
Open File
"
;
break
;
case
FILE_DIALOG_OPEN_MULTIPLE
:
title_str =
"
Open Files
"
;
break
;
case
FILE_DIALOG_OPEN_FOLDER
:
title_str =
"
Open Folder
"
;
break
;
case
FILE_DIALOG_SAVE
:
title_str =
"
Save File
"
;
break
;
default
:
break
;
}
}
GtkWindow* window =
GetWindow
(browser);
if
(!window)
return
false
;
GtkWidget* dialog =
gtk_file_chooser_dialog_new
(
title_str.
c_str
(),
GTK_WINDOW
(window),
action,
GTK_STOCK_CANCEL
,
GTK_RESPONSE_CANCEL
,
accept_button,
GTK_RESPONSE_ACCEPT
,
NULL
);
if
(mode_type ==
FILE_DIALOG_OPEN_MULTIPLE
)
gtk_file_chooser_set_select_multiple
(
GTK_FILE_CHOOSER
(dialog),
TRUE
);
if
(mode_type ==
FILE_DIALOG_SAVE
) {
gtk_file_chooser_set_do_overwrite_confirmation
(
GTK_FILE_CHOOSER
(dialog),
!!(mode &
FILE_DIALOG_OVERWRITEPROMPT_FLAG
));
}
gtk_file_chooser_set_show_hidden
(
GTK_FILE_CHOOSER
(dialog),
!(mode &
FILE_DIALOG_HIDEREADONLY_FLAG
));
if
(!default_file_path.
empty
() && mode_type ==
FILE_DIALOG_SAVE
) {
const
std::string& file_path = default_file_path;
bool
exists =
false
;
struct
stat
sb;
if
(
stat
(file_path.
c_str
(), &sb) ==
0
&&
S_ISREG
(sb.
st_mode
)) {
//
Use the directory and name of the existing file.
gtk_file_chooser_set_filename
(
GTK_FILE_CHOOSER
(dialog),
file_path.
data
());
exists =
true
;
}
if
(!exists) {
//
Set the current file name but let the user choose the directory.
std::string file_name_str = file_path;
const
char
* file_name =
basename
(
const_cast
<
char
*>(file_name_str.
data
()));
gtk_file_chooser_set_current_name
(
GTK_FILE_CHOOSER
(dialog), file_name);
}
}
std::vector<GtkFileFilter*> filters;
AddFilters
(
GTK_FILE_CHOOSER
(dialog), accept_filters,
true
, &filters);
if
(selected_accept_filter <
static_cast
<
int
>(filters.
size
())) {
gtk_file_chooser_set_filter
(
GTK_FILE_CHOOSER
(dialog),
filters[selected_accept_filter]);
}
bool
success =
false
;
if
(
gtk_dialog_run
(
GTK_DIALOG
(dialog)) ==
GTK_RESPONSE_ACCEPT
) {
if
(mode_type ==
FILE_DIALOG_OPEN
|| mode_type ==
FILE_DIALOG_OPEN_FOLDER
||
mode_type ==
FILE_DIALOG_SAVE
) {
char
* filename =
gtk_file_chooser_get_filename
(
GTK_FILE_CHOOSER
(dialog));
files.
push_back
(
std::string
(filename));
success =
true
;
}
else
if
(mode_type ==
FILE_DIALOG_OPEN_MULTIPLE
) {
GSList* filenames =
gtk_file_chooser_get_filenames
(
GTK_FILE_CHOOSER
(dialog));
if
(filenames) {
for
(GSList* iter = filenames; iter !=
NULL
;
iter =
g_slist_next
(iter)) {
std::string
path
(
static_cast
<
char
*>(iter->
data
));
g_free
(iter->
data
);
files.
push_back
(path);
}
g_slist_free
(filenames);
success =
true
;
}
}
}
int
filter_index = selected_accept_filter;
if
(success) {
GtkFileFilter* selected_filter =
gtk_file_chooser_get_filter
(
GTK_FILE_CHOOSER
(dialog));
if
(selected_filter !=
NULL
) {
for
(
size_t
x =
0
; x < filters.
size
(); ++x) {
if
(filters[x] == selected_filter) {
filter_index = x;
break
;
}
}
}
}
gtk_widget_destroy
(dialog);
if
(success)
callback->
Continue
(filter_index, files);
else
callback->
Cancel
();
return
true
;
}
bool
ClientDialogHandlerGtk::OnJSDialog
(
CefRefPtr<CefBrowser> browser,
const
CefString& origin_url,
JSDialogType dialog_type,
const
CefString& message_text,
const
CefString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback,
bool
& suppress_message) {
CEF_REQUIRE_UI_THREAD
();
GtkButtonsType buttons =
GTK_BUTTONS_NONE
;
GtkMessageType gtk_message_type =
GTK_MESSAGE_OTHER
;
std::string title;
switch
(dialog_type) {
case
JSDIALOGTYPE_ALERT
:
buttons =
GTK_BUTTONS_NONE
;
gtk_message_type =
GTK_MESSAGE_WARNING
;
title =
"
Alert
"
;
break
;
case
JSDIALOGTYPE_CONFIRM
:
buttons =
GTK_BUTTONS_CANCEL
;
gtk_message_type =
GTK_MESSAGE_QUESTION
;
title =
"
Confirm
"
;
break
;
case
JSDIALOGTYPE_PROMPT
:
buttons =
GTK_BUTTONS_CANCEL
;
gtk_message_type =
GTK_MESSAGE_QUESTION
;
title =
"
Prompt
"
;
break
;
}
js_dialog_callback_ = callback;
if
(!origin_url.
empty
()) {
//
title += " - ";
//
title += CefFormatUrlForSecurityDisplay(origin_url).ToString();
}
GtkWindow* window =
GetWindow
(browser);
if
(!window)
return
false
;
gtk_dialog_ =
gtk_message_dialog_new
(
GTK_WINDOW
(window),
GTK_DIALOG_MODAL
,
gtk_message_type,
buttons,
"
%s
"
,
message_text.
ToString
().
c_str
());
g_signal_connect
(gtk_dialog_,
"
delete-event
"
,
G_CALLBACK
(gtk_widget_hide_on_delete),
NULL
);
gtk_window_set_title
(
GTK_WINDOW
(gtk_dialog_), title.
c_str
());
GtkWidget* ok_button =
gtk_dialog_add_button
(
GTK_DIALOG
(gtk_dialog_),
GTK_STOCK_OK
,
GTK_RESPONSE_OK
);
if
(dialog_type !=
JSDIALOGTYPE_PROMPT
)
gtk_widget_grab_focus
(ok_button);
if
(dialog_type ==
JSDIALOGTYPE_PROMPT
) {
GtkWidget* content_area =
gtk_dialog_get_content_area
(
GTK_DIALOG
(gtk_dialog_));
GtkWidget* text_box =
gtk_entry_new
();
gtk_entry_set_text
(
GTK_ENTRY
(text_box),
default_prompt_text.
ToString
().
c_str
());
gtk_box_pack_start
(
GTK_BOX
(content_area), text_box,
TRUE
,
TRUE
,
0
);
g_object_set_data
(
G_OBJECT
(gtk_dialog_),
kPromptTextId
, text_box);
gtk_entry_set_activates_default
(
GTK_ENTRY
(text_box),
TRUE
);
}
gtk_dialog_set_default_response
(
GTK_DIALOG
(gtk_dialog_),
GTK_RESPONSE_OK
);
g_signal_connect
(gtk_dialog_,
"
response
"
,
G_CALLBACK
(OnDialogResponse),
this
);
gtk_widget_show_all
(
GTK_WIDGET
(gtk_dialog_));
return
true
;
}
bool
ClientDialogHandlerGtk::OnBeforeUnloadDialog
(
CefRefPtr<CefBrowser> browser,
const
CefString& message_text,
bool
is_reload,
CefRefPtr<CefJSDialogCallback> callback) {
CEF_REQUIRE_UI_THREAD
();
const
std::string& new_message_text =
message_text.
ToString
() +
"
\n\n
Is it OK to leave/reload this page?
"
;
bool
suppress_message =
false
;
return
OnJSDialog
(browser,
CefString
(),
JSDIALOGTYPE_CONFIRM
,
new_message_text,
CefString
(), callback, suppress_message);
}
void
ClientDialogHandlerGtk::OnResetDialogState
(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD
();
if
(!gtk_dialog_)
return
;
gtk_widget_destroy
(gtk_dialog_);
gtk_dialog_ =
NULL
;
js_dialog_callback_ =
NULL
;
}
//
static
void
ClientDialogHandlerGtk::OnDialogResponse
(GtkDialog* dialog,
gint response_id,
ClientDialogHandlerGtk* handler) {
CEF_REQUIRE_UI_THREAD
();
DCHECK_EQ
(dialog,
GTK_DIALOG
(handler->
gtk_dialog_
));
switch
(response_id) {
case
GTK_RESPONSE_OK
:
handler->
js_dialog_callback_
->
Continue
(
true
,
GetPromptText
(dialog));
break
;
case
GTK_RESPONSE_CANCEL
:
case
GTK_RESPONSE_DELETE_EVENT
:
handler->
js_dialog_callback_
->
Continue
(
false
,
CefString
());
break
;
default
:
NOTREACHED
();
}
handler->
OnResetDialogState
(
NULL
);
}
Back
|
FazBrowse Home
|
New Git URL