File: WebKitWebHitTestResult.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (434 lines) | stat: -rw-r--r-- 15,835 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2014 Igalia S.L.
 *
 * 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.1 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 "WebKitWebHitTestResult.h"

#include "InjectedBundleHitTestResult.h"
#include "InjectedBundleScriptWorld.h"
#include "WebKitScriptWorldPrivate.h"
#include "WebKitWebHitTestResultPrivate.h"
#include <JavaScriptCore/APICast.h>
#include <JavaScriptCore/JSGlobalObjectInlines.h>
#include <JavaScriptCore/JSLock.h>
#include <WebCore/FrameDestructionObserverInlines.h>
#include <WebCore/JSNode.h>
#include <WebCore/LocalFrame.h>
#include <WebCore/Node.h>
#include <WebCore/ScriptController.h>
#include <glib/gi18n-lib.h>
#include <jsc/JSCContextPrivate.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>

#if !ENABLE(2022_GLIB_API)
#include "WebKitDOMNodePrivate.h"
#endif

using namespace WebKit;
using namespace WebCore;

/**
 * WebKitWebHitTestResult:
 * @See_also: #WebKitHitTestResult, #WebKitWebPage
 *
 * Result of a Hit Test (Web Process Extensions).
 *
 * WebKitWebHitTestResult extends #WebKitHitTestResult to provide information
 * about the #WebKitDOMNode in the coordinates of the Hit Test.
 *
 * Since: 2.8
 */

struct _WebKitWebHitTestResultPrivate {
    WeakPtr<Node, WeakPtrImplWithEventTargetData> node;

#if ENABLE(2022_GLIB_API)
    GRefPtr<WebKitHitTestResult> hitTestResult;
#endif // ENABLE(2022_GLIB_API)
};

#if ENABLE(2022_GLIB_API)
WEBKIT_DEFINE_FINAL_TYPE(WebKitWebHitTestResult, webkit_web_hit_test_result, G_TYPE_OBJECT, GObject)
#else
WEBKIT_DEFINE_TYPE(WebKitWebHitTestResult, webkit_web_hit_test_result, WEBKIT_TYPE_HIT_TEST_RESULT)

enum {
    PROP_0,

    PROP_NODE
};

static void webkitWebHitTestResultGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
    WebKitWebHitTestResult* webHitTestResult = WEBKIT_WEB_HIT_TEST_RESULT(object);

    switch (propId) {
    case PROP_NODE:
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
        g_value_set_object(value, webkit_web_hit_test_result_get_node(webHitTestResult));
        G_GNUC_END_IGNORE_DEPRECATIONS;
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static void webkitWebHitTestResultSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
{
    WebKitWebHitTestResult* webHitTestResult = WEBKIT_WEB_HIT_TEST_RESULT(object);

    switch (propId) {
    case PROP_NODE:
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
        if (gpointer node = g_value_get_object(value))
            webHitTestResult->priv->node = core(WEBKIT_DOM_NODE(node));
        G_GNUC_END_IGNORE_DEPRECATIONS;
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}
#endif

static void webkit_web_hit_test_result_class_init(WebKitWebHitTestResultClass* klass)
{
#if !ENABLE(2022_GLIB_API)
    GObjectClass* gObjectClass = G_OBJECT_CLASS(klass);

    gObjectClass->get_property = webkitWebHitTestResultGetProperty;
    gObjectClass->set_property = webkitWebHitTestResultSetProperty;

    G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
    /**
     * WebKitWebHitTestResult:node:
     *
     * The #WebKitDOMNode
     *
     * Deprecated: 2.40
     */
    g_object_class_install_property(
        gObjectClass,
        PROP_NODE,
        g_param_spec_object(
            "node",
            nullptr, nullptr,
            WEBKIT_DOM_TYPE_NODE,
            static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
    G_GNUC_END_IGNORE_DEPRECATIONS;
#endif
}

WebKitWebHitTestResult* webkitWebHitTestResultCreate(const HitTestResult& hitTestResult)
{
    unsigned context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
    String absoluteLinkURL = hitTestResult.absoluteLinkURL().string();
    if (!absoluteLinkURL.isEmpty())
        context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
    String absoluteImageURL = hitTestResult.absoluteImageURL().string();
    if (!absoluteImageURL.isEmpty())
        context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
    String absoluteMediaURL = hitTestResult.absoluteMediaURL().string();
    if (!absoluteMediaURL.isEmpty())
        context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
    if (hitTestResult.isContentEditable())
        context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
    if (hitTestResult.scrollbar())
        context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR;
    if (hitTestResult.isSelected())
        context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;

    String linkTitle = hitTestResult.titleDisplayString();
    String linkLabel = hitTestResult.textContent();

#if ENABLE(2022_GLIB_API)
    GRefPtr<WebKitHitTestResult> webkitHitTestResult = adoptGRef(WEBKIT_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
#else
    auto* result = WEBKIT_WEB_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_WEB_HIT_TEST_RESULT,
#endif
        "context", context,
        "link-uri", context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK ? absoluteLinkURL.utf8().data() : nullptr,
        "image-uri", context & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE ? absoluteImageURL.utf8().data() : nullptr,
        "media-uri", context & WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA ? absoluteMediaURL.utf8().data() : nullptr,
        "link-title", !linkTitle.isEmpty() ? linkTitle.utf8().data() : nullptr,
        "link-label", !linkLabel.isEmpty() ? linkLabel.utf8().data() : nullptr,
#if ENABLE(2022_GLIB_API)
        nullptr)));
#else
        "node", kit(hitTestResult.innerNonSharedNode()),
        nullptr));
#endif

#if ENABLE(2022_GLIB_API)
    auto* result = WEBKIT_WEB_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_WEB_HIT_TEST_RESULT, nullptr));
    result->priv->hitTestResult = WTFMove(webkitHitTestResult);
    result->priv->node = hitTestResult.innerNonSharedNode();
#endif
    return result;
}

#if ENABLE(2022_GLIB_API)

/**
 * webkit_web_hit_test_result_get_context:
 * @web_hit_test_result: a #WebKitWebHitTestResult
 *
 * Gets the the context flags for the hit test result.
 *
 * Returns: a bitmask of #WebKitHitTestResultContext flags
 */
guint webkit_web_hit_test_result_get_context(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), 0);
    return webkit_hit_test_result_get_context(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_context_is_link:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Check whether there is a link element at the hit test position.
 *
 * Checks whether %WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK flag is present in
 * the context flags.
 *
 * Returns: %TRUE if the hit test covers a link element or %FALSE otherwise.
 */
gboolean webkit_web_hit_test_result_context_is_link(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), FALSE);
    return webkit_hit_test_result_context_is_link(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_context_is_image:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Check whether there is an image element at the hit test position.
 *
 * Checks whether %WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE flag is present in
 * the context flags.
 *
 * Returns: %TRUE if the hit test covers an image element or %FALSE otherwise.
 */
gboolean webkit_web_hit_test_result_context_is_image(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), FALSE);
    return webkit_hit_test_result_context_is_image(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_context_is_media:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Check whether there is a media element at the hit test position.
 *
 * Checks whether %WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA flag is present in
 * the context flags.
 *
 * Returns: %TRUE if the hit test covers a media element or %FALSE otherwise.
 */
gboolean webkit_web_hit_test_result_context_is_media(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), FALSE);
    return webkit_hit_test_result_context_is_media(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_context_is_editable:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Check whether there is an editable element at the hit test position.
 *
 * Checks whether %WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE flag is present in
 * the context flags.
 *
 * Returns: %TRUE if the hit test covers an editable element or %FALSE otherwise.
 */
gboolean webkit_web_hit_test_result_context_is_editable(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), FALSE);
    return webkit_hit_test_result_context_is_editable(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_context_is_selection:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Check whether there is a selected element at the hit test position.
 *
 * Checks whether %WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION flag is present in
 * the context flags.
 *
 * Returns: %TRUE if the hit test covers a selected element or %FALSE otherwise.
 */
gboolean webkit_web_hit_test_result_context_is_selection(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), FALSE);
    return webkit_hit_test_result_context_is_selection(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_get_link_uri:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Obtains the URI associated with the link element at the hit test position.
 *
 * Returns: the URI of the link element, or %NULL if the hit test does not cover a link element.
 */
const gchar* webkit_web_hit_test_result_get_link_uri(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
    return webkit_hit_test_result_get_link_uri(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_get_link_title:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Obtains the title associated with the link element at the hit test position.
 *
 * Returns: the title of the link element, or %NULL if the hit test does not cover a link element
 *    or the link element does not have a title.
 */
const gchar* webkit_web_hit_test_result_get_link_title(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
    return webkit_hit_test_result_get_link_title(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_get_link_label:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Obtains the label associated with the link element at the hit test position.
 *
 * Returns: the label of the link element, or %NULL if the hit test does not cover a link element
 *    or the link element does not have a label.
 */
const gchar* webkit_web_hit_test_result_get_link_label(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
    return webkit_hit_test_result_get_link_label(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_get_image_uri:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Obtains the URI associated with the image element at the hit test position.
 *
 * Returns: the URI of the image element, or %NULL if the hit test does not cover an image element.
 */
const gchar* webkit_web_hit_test_result_get_image_uri(WebKitWebHitTestResult *webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
    return webkit_hit_test_result_get_image_uri(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_get_media_uri:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Obtains the URI associated with the media element at the hit test position.
 *
 * Returns: the URI of the media element, or %NULL if the hit test does not cover a media element.
 */
const gchar* webkit_web_hit_test_result_get_media_uri(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
    return webkit_hit_test_result_get_media_uri(webHitTestResult->priv->hitTestResult.get());
}

/**
 * webkit_web_hit_test_result_context_is_scrollbar:
 * @web_hit_test_result: a #WebWebKitHitTestResult
 *
 * Check whether there is a scrollbar at the hit test position.
 *
 * Checks whether %WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR flag is present in
 * the context flags.
 *
 * Returns: %TRUE if the hit test covers a scrollbar or %FALSE otherwise.
 */
gboolean webkit_web_hit_test_result_context_is_scrollbar(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), FALSE);
    return webkit_hit_test_result_context_is_scrollbar(webHitTestResult->priv->hitTestResult.get());
}

#else
/**
 * webkit_web_hit_test_result_get_node:
 * @hit_test_result: a #WebKitWebHitTestResult
 *
 * Get the #WebKitDOMNode in the coordinates of the Hit Test.
 *
 * Returns: (transfer none): a #WebKitDOMNode
 *
 * Since: 2.8
 *
 * Deprecated: 2.40: Use webkit_web_hit_test_result_get_js_node() instead
 */
WebKitDOMNode* webkit_web_hit_test_result_get_node(WebKitWebHitTestResult* webHitTestResult)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);

    return kit(webHitTestResult->priv->node.get());
}
#endif // ENABLE(2022_GLIB_API)

/**
 * webkit_web_hit_test_result_get_js_node:
 * @web_hit_test_result: a #WebKitWebHitTestResult
 * @world: (nullable): a #WebKitScriptWorld, or %NULL to use the default
 *
 * Get the #JSCValue for the DOM node in @world at the coordinates of the Hit Test.
 *
 * Returns: (transfer full) (nullable): a #JSCValue for the DOM node, or %NULL
 *
 * Since: 2.40
 */
JSCValue* webkit_web_hit_test_result_get_js_node(WebKitWebHitTestResult* webHitTestResult, WebKitScriptWorld* world)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
    g_return_val_if_fail(!world || WEBKIT_IS_SCRIPT_WORLD(world), nullptr);

    if (!webHitTestResult->priv->node)
        return nullptr;

    RefPtr frame = webHitTestResult->priv->node->document().frame();
    if (!frame)
        return nullptr;

    if (!world)
        world = webkit_script_world_get_default();

    RefPtr wkWorld = webkitScriptWorldGetInjectedBundleScriptWorld(world);
    auto* globalObject = frame->script().globalObject(wkWorld->coreWorld());
    auto jsContext = jscContextGetOrCreate(toGlobalRef(globalObject));
    JSValueRef jsValue = nullptr;
    {
        JSC::JSLockHolder lock(globalObject);
        jsValue = toRef(globalObject, toJS(globalObject, globalObject, webHitTestResult->priv->node.get()));
    }

    return jsValue ? jscContextGetOrCreateValue(jsContext.get(), jsValue).leakRef() : nullptr;
}