File: WebKitWebViewWPE.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (328 lines) | stat: -rw-r--r-- 10,883 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2017 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitWebView.h"

#include "PageClientImpl.h"
#include "WebInspectorUIProxy.h"
#include "WebKitColorPrivate.h"
#include "WebKitScriptDialogPrivate.h"
#include "WebKitWebViewPrivate.h"

#if ENABLE(WPE_PLATFORM)
#include <wpe/wpe-platform.h>
#endif

gboolean webkitWebViewAuthenticate(WebKitWebView*, WebKitAuthenticationRequest*)
{
    return FALSE;
}

gboolean webkitWebViewScriptDialog(WebKitWebView* webview, WebKitScriptDialog* dialog)
{
    if (webkit_web_view_is_controlled_by_automation(webview)) {
        webkit_script_dialog_ref(dialog);
        dialog->isUserHandled = false;
    }

    return FALSE;
}

gboolean webkitWebViewRunFileChooser(WebKitWebView*, WebKitFileChooserRequest*)
{
    return FALSE;
}

void webkitWebViewMaximizeWindow(WebKitWebView*, CompletionHandler<void()>&& completionHandler)
{
    completionHandler();
}

void webkitWebViewMinimizeWindow(WebKitWebView*, CompletionHandler<void()>&& completionHandler)
{
    completionHandler();
}

void webkitWebViewRestoreWindow(WebKitWebView*, CompletionHandler<void()>&& completionHandler)
{
    completionHandler();
}

/**
 * webkit_web_view_new:
 * @backend: (transfer full) (nullable): wrapped WPE view backend which
 *    will determine the behaviour of the new [class@WebView], or %NULL to use the WPE platform API.
 *
 * Creates a new web view with a default configuration.
 *
 * The new view will use the default [class@WebContext] and will not
 * have an associated [class@UserContentManager].
 *
 * See also [id@webkit_web_view_new_with_context],
 * [id@webkit_web_view_new_with_user_content_manager]), and
 * [id@webkit_web_view_new_with_settings].
 *
 * Returns: The newly created web view.
 */
WebKitWebView* webkit_web_view_new(WebKitWebViewBackend* backend)
{
#if ENABLE(WPE_PLATFORM)
    g_return_val_if_fail(!backend || !g_type_class_peek(WPE_TYPE_DISPLAY), nullptr);
#else
    g_return_val_if_fail(backend, nullptr);
#endif

    return WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
        "backend", backend,
        "web-context", webkit_web_context_get_default(),
        nullptr));
}

#if !ENABLE(2022_GLIB_API)
/**
 * webkit_web_view_new_with_context:
 * @backend: (transfer full) (not nullable): wrapped WPE view backend which
 *    will determine the behaviour of the new [class@WebView].
 * @context: the web context the new [class@WebView] will use.
 *
 * Creates a new web view with a given context.
 *
 * The new web view will use the given [class@WebContext] and will not have
 * an associated [class@UserContentManager].
 *
 * See also [id@webkit_web_view_new_with_user_content_manager] and
 * [id@webkit_web_view_new_with_settings].
 *
 * Returns: The newly created web view.
 */
WebKitWebView* webkit_web_view_new_with_context(WebKitWebViewBackend* backend, WebKitWebContext* context)
{
    g_return_val_if_fail(backend, nullptr);
    g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);

    return WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
        "backend", backend,
#if !ENABLE(2022_GLIB_API)
        "is-ephemeral", webkit_web_context_is_ephemeral(context),
#endif
        "web-context", context,
        nullptr));
}
#endif

#if !ENABLE(2022_GLIB_API)
/**
 * webkit_web_view_new_with_related_view: (constructor)
 * @backend: (transfer full) (not nullable): wrapped WPE view backend which
 *    will determine the behaviour of the new [class@WebView].
 * @web_view: the related web view.
 *
 * Creates a new web view sharing the same configuration and web process as another.
 *
 * A related view should always be set when creating a [class@WebView] in a handler
 * for the [signal@WebView::create] signal.
 *
 * The new view will also have the same [class@UserContentManager] and
 * [class@Settings] as the related @web_view.
 *
 * Returns: (transfer full): The newly created web view.
 *
 * Since: 2.4
 */
WebKitWebView* webkit_web_view_new_with_related_view(WebKitWebViewBackend* backend, WebKitWebView* webView)
{
    g_return_val_if_fail(backend, nullptr);
    g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), nullptr);

    return WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
        "backend", backend,
        "user-content-manager", webkit_web_view_get_user_content_manager(webView),
        "settings", webkit_web_view_get_settings(webView),
        "related-view", webView,
        nullptr));
}

/**
 * webkit_web_view_new_with_settings:
 * @backend: (transfer full) (not nullable): wrapped WPE view backend which
 *    will determine the behaviour of the new [class@WebView].
 * @settings: settings for the new view.
 *
 * Creates a new web view with the given settings.
 *
 * See also [id@webkit_web_view_new_with_context], and
 * [id@webkit_web_view_new_with_user_content_manager].
 *
 * Returns: (transfer full): The newly created web view.
 *
 * Since: 2.6
 */
WebKitWebView* webkit_web_view_new_with_settings(WebKitWebViewBackend* backend, WebKitSettings* settings)
{
    g_return_val_if_fail(backend, nullptr);
    g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), nullptr);

    return WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
        "backend", backend,
        "settings", settings,
        nullptr));
}

/**
 * webkit_web_view_new_with_user_content_manager:
 * @backend: (transfer full) (not nullable): wrapped WPE view backend which
 *    will determine the behaviour of the new [class@WebView].
 * @user_content_manager: the user content manager for the new view.
 *
 * Creates a new web view with the given user content manager.
 *
 * The content loaded in the new [class@WebView] may be affected by the
 * configuration of the given [class@UserContentManager].
 *
 * Returns: (transfer full): The newly created web view.
 *
 * Since: 2.6
 */
WebKitWebView* webkit_web_view_new_with_user_content_manager(WebKitWebViewBackend* backend, WebKitUserContentManager* userContentManager)
{
    g_return_val_if_fail(backend, nullptr);
    g_return_val_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(userContentManager), nullptr);

    return WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
        "backend", backend,
        "user-content-manager", userContentManager,
        nullptr));
}
#endif

/**
 * webkit_web_view_set_background_color:
 * @web_view: a #WebKitWebView
 * @color: a #WebKitColor
 *
 * Sets the color that will be used to draw the @web_view background before
 * the actual contents are rendered. Note that if the web page loaded in @web_view
 * specifies a background color, it will take precedence over the background color.
 * By default the @web_view background color is opaque white.
 *
 * Since: 2.24
 */
void webkit_web_view_set_background_color(WebKitWebView* webView, WebKitColor* backgroundColor)
{
    g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
    g_return_if_fail(backgroundColor);

    auto& page = webkitWebViewGetPage(webView);
    auto color = webkitColorToWebCoreColor(backgroundColor);
    page.setBackgroundColor(color);
#if ENABLE(WPE_PLATFORM)
    if (auto* view = static_cast<WebKit::PageClientImpl&>(*page.pageClient()).wpeView()) {
        if (color.isOpaque()) {
            WPERectangle rect { 0, 0, wpe_view_get_width(view), wpe_view_get_height(view) };
            wpe_view_set_opaque_rectangles(view, &rect, 1);
        } else
            wpe_view_set_opaque_rectangles(view, nullptr, 0);
    }
#endif
}

/**
 * webkit_web_view_get_background_color:
 * @web_view: a #WebKitWebView
 * @color: (out): a #WebKitColor to fill in with the background color
 *
 * Gets the color that is used to draw the @web_view background before the
 * actual contents are rendered. For more information see also
 * webkit_web_view_set_background_color().
 *
 * Since: 2.24
 */
void webkit_web_view_get_background_color(WebKitWebView* webView, WebKitColor* color)
{
    g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
    auto& page = webkitWebViewGetPage(webView);

    auto& webCoreColor = page.backgroundColor();
    webkitColorFillFromWebCoreColor(webCoreColor.value_or(WebCore::Color::white), color);
}

guint createShowOptionMenuSignal(WebKitWebViewClass* webViewClass)
{
    /**
     * WebKitWebView::show-option-menu:
     * @web_view: the #WebKitWebView on which the signal is emitted
     * @menu: the #WebKitOptionMenu
     * @rectangle: the option element area
     *
     * This signal is emitted when a select element in @web_view needs to display a
     * dropdown menu. This signal can be used to show a custom menu, using @menu to get
     * the details of all items that should be displayed. The area of the element in the
     * #WebKitWebView is given as @rectangle parameter, it can be used to position the
     * menu.
     * To handle this signal asynchronously you should keep a ref of the @menu.
     *
     * Returns: %TRUE to stop other handlers from being invoked for the event.
     *   %FALSE to propagate the event further.
     *
     * Since: 2.28
     */
    return g_signal_new(
        "show-option-menu",
        G_TYPE_FROM_CLASS(webViewClass),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET(WebKitWebViewClass, show_option_menu),
        g_signal_accumulator_true_handled, nullptr,
        g_cclosure_marshal_generic,
        G_TYPE_BOOLEAN, 2,
        WEBKIT_TYPE_OPTION_MENU,
        WEBKIT_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
}

#if ENABLE(WPE_PLATFORM)
/**
 * webkit_web_view_toggle_inspector:
 * @web_view: a #WebKitWebView
 *
 * Show or hide the web inspector of @web_view
 * Note that local inspector is only supported by
 * WPEWebKit when using WPE Platform API.
 *
 * Since: 2.46
 */
void webkit_web_view_toggle_inspector(WebKitWebView* webView)
{
    g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));

    auto& page = webkitWebViewGetPage(webView);
    if (!page.wpeView()) {
        g_warning("Local inspector is only supported by WPEWebKit when using WPE Platform API");
        return;
    }

    auto* inspector = page.inspector();
    if (!inspector)
        return;

    if (inspector->isVisible())
        inspector->close();
    else
        inspector->show();
}
#endif