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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/*
* Copyright (C) 2007, 2008 Holger Hans Peter Freyther
* Copyright (C) 2007, 2008 Christian Dywan <christian@imendio.com>
* Copyright (C) 2008 Nuanti Ltd.
* Copyright (C) 2008 Alp Toker <alp@atoker.com>
* Copyright (C) 2008 Gustavo Noronha Silva <gns@gnome.org>
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "ChromeClientGtk.h"
#include "Console.h"
#include "FileSystem.h"
#include "FileChooser.h"
#include "FloatRect.h"
#include "FrameLoadRequest.h"
#include "GtkVersioning.h"
#include "IntRect.h"
#include "HitTestResult.h"
#include "Icon.h"
#include "KURL.h"
#include "PlatformString.h"
#include "PopupMenuClient.h"
#include "PopupMenuGtk.h"
#include "SearchPopupMenuGtk.h"
#include "SecurityOrigin.h"
#include "webkitgeolocationpolicydecision.h"
#include "webkitwebview.h"
#include "webkitnetworkrequest.h"
#include "webkitprivate.h"
#include "NotImplemented.h"
#include "WindowFeatures.h"
#if ENABLE(DATABASE)
#include "DatabaseTracker.h"
#endif
#include <wtf/text/CString.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
using namespace WebCore;
namespace WebKit {
ChromeClient::ChromeClient(WebKitWebView* webView)
: m_webView(webView)
{
ASSERT(m_webView);
}
void ChromeClient::chromeDestroyed()
{
delete this;
}
FloatRect ChromeClient::windowRect()
{
GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(m_webView));
if (gtk_widget_is_toplevel(window)) {
gint left, top, width, height;
gtk_window_get_position(GTK_WINDOW(window), &left, &top);
gtk_window_get_size(GTK_WINDOW(window), &width, &height);
return IntRect(left, top, width, height);
}
return FloatRect();
}
void ChromeClient::setWindowRect(const FloatRect& rect)
{
IntRect intrect = IntRect(rect);
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
g_object_set(webWindowFeatures,
"x", intrect.x(),
"y", intrect.y(),
"width", intrect.width(),
"height", intrect.height(),
NULL);
gboolean autoResizeWindow;
WebKitWebSettings* settings = webkit_web_view_get_settings(m_webView);
g_object_get(settings, "auto-resize-window", &autoResizeWindow, NULL);
if (!autoResizeWindow)
return;
GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(m_webView));
if (gtk_widget_is_toplevel(window)) {
gtk_window_move(GTK_WINDOW(window), intrect.x(), intrect.y());
gtk_window_resize(GTK_WINDOW(window), intrect.width(), intrect.height());
}
}
FloatRect ChromeClient::pageRect()
{
GtkAllocation allocation;
#if GTK_CHECK_VERSION(2, 18, 0)
gtk_widget_get_allocation(GTK_WIDGET(m_webView), &allocation);
#else
allocation = GTK_WIDGET(m_webView)->allocation;
#endif
return IntRect(allocation.x, allocation.y, allocation.width, allocation.height);
}
float ChromeClient::scaleFactor()
{
// Not implementable
return 1.0;
}
void ChromeClient::focus()
{
gtk_widget_grab_focus(GTK_WIDGET(m_webView));
}
void ChromeClient::unfocus()
{
GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(m_webView));
if (gtk_widget_is_toplevel(window))
gtk_window_set_focus(GTK_WINDOW(window), NULL);
}
Page* ChromeClient::createWindow(Frame* frame, const FrameLoadRequest& frameLoadRequest, const WindowFeatures& coreFeatures)
{
WebKitWebView* webView = 0;
g_signal_emit_by_name(m_webView, "create-web-view", kit(frame), &webView);
if (!webView)
return 0;
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_window_features_new_from_core_features(coreFeatures);
g_object_set(webView, "window-features", webWindowFeatures, NULL);
g_object_unref(webWindowFeatures);
if (!frameLoadRequest.isEmpty())
webkit_web_view_open(webView, frameLoadRequest.resourceRequest().url().string().utf8().data());
return core(webView);
}
void ChromeClient::show()
{
webkit_web_view_notify_ready(m_webView);
}
bool ChromeClient::canRunModal()
{
notImplemented();
return false;
}
void ChromeClient::runModal()
{
notImplemented();
}
void ChromeClient::setToolbarsVisible(bool visible)
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
g_object_set(webWindowFeatures, "toolbar-visible", visible, NULL);
}
bool ChromeClient::toolbarsVisible()
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
gboolean visible;
g_object_get(webWindowFeatures, "toolbar-visible", &visible, NULL);
return visible;
}
void ChromeClient::setStatusbarVisible(bool visible)
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
g_object_set(webWindowFeatures, "statusbar-visible", visible, NULL);
}
bool ChromeClient::statusbarVisible()
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
gboolean visible;
g_object_get(webWindowFeatures, "statusbar-visible", &visible, NULL);
return visible;
}
void ChromeClient::setScrollbarsVisible(bool visible)
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
g_object_set(webWindowFeatures, "scrollbar-visible", visible, NULL);
}
bool ChromeClient::scrollbarsVisible() {
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
gboolean visible;
g_object_get(webWindowFeatures, "scrollbar-visible", &visible, NULL);
return visible;
}
void ChromeClient::setMenubarVisible(bool visible)
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
g_object_set(webWindowFeatures, "menubar-visible", visible, NULL);
}
bool ChromeClient::menubarVisible()
{
WebKitWebWindowFeatures* webWindowFeatures = webkit_web_view_get_window_features(m_webView);
gboolean visible;
g_object_get(webWindowFeatures, "menubar-visible", &visible, NULL);
return visible;
}
void ChromeClient::setResizable(bool)
{
// Ignored for now
}
void ChromeClient::closeWindowSoon()
{
// We may not have a WebView as create-web-view can return NULL.
if (!m_webView)
return;
webkit_web_view_stop_loading(m_webView);
gboolean isHandled = false;
g_signal_emit_by_name(m_webView, "close-web-view", &isHandled);
if (isHandled)
return;
// FIXME: should we clear the frame group name here explicitly? Mac does it.
// But this gets cleared in Page's destructor anyway.
// webkit_web_view_set_group_name(m_webView, "");
}
bool ChromeClient::canTakeFocus(FocusDirection)
{
return gtk_widget_get_can_focus(GTK_WIDGET(m_webView));
}
void ChromeClient::takeFocus(FocusDirection)
{
unfocus();
}
void ChromeClient::focusedNodeChanged(Node*)
{
}
bool ChromeClient::canRunBeforeUnloadConfirmPanel()
{
return true;
}
bool ChromeClient::runBeforeUnloadConfirmPanel(const WTF::String& message, WebCore::Frame* frame)
{
return runJavaScriptConfirm(frame, message);
}
void ChromeClient::addMessageToConsole(WebCore::MessageSource source, WebCore::MessageType type, WebCore::MessageLevel level, const WTF::String& message, unsigned int lineNumber, const WTF::String& sourceId)
{
gboolean retval;
g_signal_emit_by_name(m_webView, "console-message", message.utf8().data(), lineNumber, sourceId.utf8().data(), &retval);
}
void ChromeClient::runJavaScriptAlert(Frame* frame, const String& message)
{
gboolean retval;
g_signal_emit_by_name(m_webView, "script-alert", kit(frame), message.utf8().data(), &retval);
}
bool ChromeClient::runJavaScriptConfirm(Frame* frame, const String& message)
{
gboolean retval;
gboolean didConfirm;
g_signal_emit_by_name(m_webView, "script-confirm", kit(frame), message.utf8().data(), &didConfirm, &retval);
return didConfirm == TRUE;
}
bool ChromeClient::runJavaScriptPrompt(Frame* frame, const String& message, const String& defaultValue, String& result)
{
gboolean retval;
gchar* value = 0;
g_signal_emit_by_name(m_webView, "script-prompt", kit(frame), message.utf8().data(), defaultValue.utf8().data(), &value, &retval);
if (value) {
result = String::fromUTF8(value);
g_free(value);
return true;
}
return false;
}
void ChromeClient::setStatusbarText(const String& string)
{
CString stringMessage = string.utf8();
g_signal_emit_by_name(m_webView, "status-bar-text-changed", stringMessage.data());
}
bool ChromeClient::shouldInterruptJavaScript()
{
notImplemented();
return false;
}
bool ChromeClient::tabsToLinks() const
{
return true;
}
IntRect ChromeClient::windowResizerRect() const
{
notImplemented();
return IntRect();
}
void ChromeClient::invalidateWindow(const IntRect&, bool)
{
notImplemented();
}
void ChromeClient::invalidateContentsAndWindow(const IntRect& updateRect, bool immediate)
{
GdkRectangle rect = updateRect;
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(m_webView));
if (window) {
gdk_window_invalidate_rect(window, &rect, FALSE);
// We don't currently do immediate updates since they delay other UI elements.
//if (immediate)
// gdk_window_process_updates(window, FALSE);
}
}
void ChromeClient::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
{
invalidateContentsAndWindow(updateRect, immediate);
}
void ChromeClient::scroll(const IntSize& delta, const IntRect& rectToScroll, const IntRect& clipRect)
{
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(m_webView));
if (!window)
return;
// We cannot use gdk_window_scroll here because it is only able to
// scroll the whole window at once, and we often need to scroll
// portions of the window only (think frames).
GdkRectangle area = clipRect;
GdkRectangle moveRect;
GdkRectangle sourceRect = area;
sourceRect.x -= delta.width();
sourceRect.y -= delta.height();
#ifdef GTK_API_VERSION_2
GdkRegion* invalidRegion = gdk_region_rectangle(&area);
if (gdk_rectangle_intersect(&area, &sourceRect, &moveRect)) {
GdkRegion* moveRegion = gdk_region_rectangle(&moveRect);
gdk_window_move_region(window, moveRegion, delta.width(), delta.height());
gdk_region_offset(moveRegion, delta.width(), delta.height());
gdk_region_subtract(invalidRegion, moveRegion);
gdk_region_destroy(moveRegion);
}
gdk_window_invalidate_region(window, invalidRegion, FALSE);
gdk_region_destroy(invalidRegion);
#else
cairo_region_t* invalidRegion = cairo_region_create_rectangle(&area);
if (gdk_rectangle_intersect(&area, &sourceRect, &moveRect)) {
cairo_region_t* moveRegion = cairo_region_create_rectangle(&moveRect);
gdk_window_move_region(window, moveRegion, delta.width(), delta.height());
cairo_region_translate(moveRegion, delta.width(), delta.height());
cairo_region_subtract(invalidRegion, moveRegion);
cairo_region_destroy(moveRegion);
}
gdk_window_invalidate_region(window, invalidRegion, FALSE);
cairo_region_destroy(invalidRegion);
#endif
}
// FIXME: this does not take into account the WM decorations
static IntPoint widgetScreenPosition(GtkWidget* widget)
{
GtkWidget* window = gtk_widget_get_toplevel(widget);
int widgetX = 0, widgetY = 0;
gtk_widget_translate_coordinates(widget, window, 0, 0, &widgetX, &widgetY);
IntPoint result(widgetX, widgetY);
int originX, originY;
gdk_window_get_origin(gtk_widget_get_window(window), &originX, &originY);
result.move(originX, originY);
return result;
}
IntRect ChromeClient::windowToScreen(const IntRect& rect) const
{
IntRect result(rect);
IntPoint screenPosition = widgetScreenPosition(GTK_WIDGET(m_webView));
result.move(screenPosition.x(), screenPosition.y());
return result;
}
IntPoint ChromeClient::screenToWindow(const IntPoint& point) const
{
IntPoint result(point);
IntPoint screenPosition = widgetScreenPosition(GTK_WIDGET(m_webView));
result.move(-screenPosition.x(), -screenPosition.y());
return result;
}
PlatformPageClient ChromeClient::platformPageClient() const
{
return GTK_WIDGET(m_webView);
}
void ChromeClient::contentsSizeChanged(Frame* frame, const IntSize& size) const
{
// We need to queue a resize request only if the size changed,
// otherwise we get into an infinite loop!
GtkWidget* widget = GTK_WIDGET(m_webView);
GtkRequisition requisition;
#if GTK_CHECK_VERSION(2, 20, 0)
gtk_widget_get_requisition(widget, &requisition);
#else
requisition = widget->requisition;
#endif
if (gtk_widget_get_realized(widget)
&& (requisition.height != size.height())
|| (requisition.width != size.width()))
gtk_widget_queue_resize_no_redraw(widget);
}
void ChromeClient::scrollbarsModeDidChange() const
{
WebKitWebFrame* webFrame = webkit_web_view_get_main_frame(m_webView);
g_object_notify(G_OBJECT(webFrame), "horizontal-scrollbar-policy");
g_object_notify(G_OBJECT(webFrame), "vertical-scrollbar-policy");
gboolean isHandled;
g_signal_emit_by_name(webFrame, "scrollbars-policy-changed", &isHandled);
if (isHandled)
return;
GtkWidget* parent = gtk_widget_get_parent(GTK_WIDGET(m_webView));
if (!parent || !GTK_IS_SCROLLED_WINDOW(parent))
return;
GtkPolicyType horizontalPolicy = webkit_web_frame_get_horizontal_scrollbar_policy(webFrame);
GtkPolicyType verticalPolicy = webkit_web_frame_get_vertical_scrollbar_policy(webFrame);
// ScrolledWindow doesn't like to display only part of a widget if
// the scrollbars are completely disabled; We have a disparity
// here on what the policy requested by the web app is and what we
// can represent; the idea is not to show scrollbars, only.
if (horizontalPolicy == GTK_POLICY_NEVER)
horizontalPolicy = GTK_POLICY_AUTOMATIC;
if (verticalPolicy == GTK_POLICY_NEVER)
verticalPolicy = GTK_POLICY_AUTOMATIC;
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(parent),
horizontalPolicy, verticalPolicy);
}
void ChromeClient::mouseDidMoveOverElement(const HitTestResult& hit, unsigned modifierFlags)
{
// check if the element is a link...
bool isLink = hit.isLiveLink();
if (isLink) {
KURL url = hit.absoluteLinkURL();
if (!url.isEmpty() && url != m_hoveredLinkURL) {
TextDirection dir;
CString titleString = hit.title(dir).utf8();
CString urlString = url.prettyURL().utf8();
g_signal_emit_by_name(m_webView, "hovering-over-link", titleString.data(), urlString.data());
m_hoveredLinkURL = url;
}
} else if (!isLink && !m_hoveredLinkURL.isEmpty()) {
g_signal_emit_by_name(m_webView, "hovering-over-link", 0, 0);
m_hoveredLinkURL = KURL();
}
}
void ChromeClient::setToolTip(const String& toolTip, TextDirection)
{
webkit_web_view_set_tooltip_text(m_webView, toolTip.utf8().data());
}
void ChromeClient::print(Frame* frame)
{
WebKitWebFrame* webFrame = kit(frame);
gboolean isHandled = false;
g_signal_emit_by_name(m_webView, "print-requested", webFrame, &isHandled);
if (isHandled)
return;
webkit_web_frame_print(webFrame);
}
#if ENABLE(DATABASE)
void ChromeClient::exceededDatabaseQuota(Frame* frame, const String& databaseName)
{
guint64 defaultQuota = webkit_get_default_web_database_quota();
DatabaseTracker::tracker().setQuota(frame->document()->securityOrigin(), defaultQuota);
WebKitWebFrame* webFrame = kit(frame);
WebKitWebView* webView = getViewFromFrame(webFrame);
WebKitSecurityOrigin* origin = webkit_web_frame_get_security_origin(webFrame);
WebKitWebDatabase* webDatabase = webkit_security_origin_get_web_database(origin, databaseName.utf8().data());
g_signal_emit_by_name(webView, "database-quota-exceeded", webFrame, webDatabase);
}
#endif
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
void ChromeClient::reachedMaxAppCacheSize(int64_t spaceNeeded)
{
// FIXME: Free some space.
notImplemented();
}
void ChromeClient::reachedApplicationCacheOriginQuota(SecurityOrigin*)
{
notImplemented();
}
#endif
void ChromeClient::runOpenPanel(Frame*, PassRefPtr<FileChooser> prpFileChooser)
{
RefPtr<FileChooser> chooser = prpFileChooser;
GtkWidget* dialog = gtk_file_chooser_dialog_new(_("Upload File"),
GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(platformPageClient()))),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), chooser->allowsMultipleFiles());
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(dialog))) {
GSList* filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
Vector<String> names;
for (GSList* item = filenames ; item ; item = item->next) {
if (!item->data)
continue;
names.append(filenameToString(static_cast<char*>(item->data)));
g_free(item->data);
}
g_slist_free(filenames);
chooser->chooseFiles(names);
} else {
gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
if (filename)
chooser->chooseFile(filenameToString(filename));
g_free(filename);
}
}
gtk_widget_destroy(dialog);
}
void ChromeClient::chooseIconForFiles(const Vector<WTF::String>& filenames, WebCore::FileChooser* chooser)
{
chooser->iconLoaded(Icon::createIconForFiles(filenames));
}
void ChromeClient::setCursor(const Cursor&)
{
notImplemented();
}
void ChromeClient::requestGeolocationPermissionForFrame(Frame* frame, Geolocation* geolocation)
{
WebKitWebFrame* webFrame = kit(frame);
WebKitWebView* webView = getViewFromFrame(webFrame);
GRefPtr<WebKitGeolocationPolicyDecision> policyDecision(adoptGRef(webkit_geolocation_policy_decision_new(webFrame, geolocation)));
gboolean isHandled = FALSE;
g_signal_emit_by_name(webView, "geolocation-policy-decision-requested", webFrame, policyDecision.get(), &isHandled);
if (!isHandled)
webkit_geolocation_policy_deny(policyDecision.get());
}
void ChromeClient::cancelGeolocationPermissionRequestForFrame(WebCore::Frame* frame, WebCore::Geolocation*)
{
WebKitWebFrame* webFrame = kit(frame);
WebKitWebView* webView = getViewFromFrame(webFrame);
g_signal_emit_by_name(webView, "geolocation-policy-decision-cancelled", webFrame);
}
bool ChromeClient::selectItemWritingDirectionIsNatural()
{
return true;
}
PassRefPtr<WebCore::PopupMenu> ChromeClient::createPopupMenu(WebCore::PopupMenuClient* client) const
{
return adoptRef(new PopupMenuGtk(client));
}
PassRefPtr<WebCore::SearchPopupMenu> ChromeClient::createSearchPopupMenu(WebCore::PopupMenuClient* client) const
{
return adoptRef(new SearchPopupMenuGtk(client));
}
}
|