File: WebProcessTest.cpp

package info (click to toggle)
webkitgtk 2.4.11-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,128 kB
  • ctags: 192,099
  • sloc: cpp: 1,034,786; ansic: 19,255; perl: 10,747; ruby: 8,592; asm: 4,386; python: 4,132; sh: 3,988; yacc: 2,072; lex: 350; makefile: 181; xml: 63
file content (92 lines) | stat: -rw-r--r-- 3,876 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
/*
 * Copyright (C) 2013 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 "WebProcessTest.h"

#include <JavaScriptCore/JSRetainPtr.h>
#include <gio/gio.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/gobject/GUniquePtr.h>

typedef HashMap<String, std::function<std::unique_ptr<WebProcessTest> ()>> TestsMap;
static TestsMap& testsMap()
{
    static NeverDestroyed<TestsMap> s_testsMap;
    return s_testsMap;
}

void WebProcessTest::add(const String& testName, std::function<std::unique_ptr<WebProcessTest> ()> closure)
{
    testsMap().add(testName, std::move(closure));
}

std::unique_ptr<WebProcessTest> WebProcessTest::create(const String& testName)
{
    g_assert(testsMap().contains(testName));
    return testsMap().get(testName)();
}

static JSValueRef runTest(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    JSRetainPtr<JSStringRef> stringValue(Adopt, JSValueToStringCopy(context, arguments[0], nullptr));
    g_assert(stringValue);
    size_t testPathLength = JSStringGetMaximumUTF8CStringSize(stringValue.get());
    GUniquePtr<char> testPath(static_cast<char*>(g_malloc(testPathLength)));
    JSStringGetUTF8CString(stringValue.get(), testPath.get(), testPathLength);

    WebKitWebPage* webPage = WEBKIT_WEB_PAGE(JSObjectGetPrivate(thisObject));
    g_assert(WEBKIT_IS_WEB_PAGE(webPage));

    std::unique_ptr<WebProcessTest> test = WebProcessTest::create(String::fromUTF8(testPath.get()));
    return JSValueMakeBoolean(context, test->runTest(g_strrstr(testPath.get(), "/") + 1, webPage));
}

static const JSStaticFunction webProcessTestRunnerStaticFunctions[] =
{
    { "runTest", runTest, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { nullptr, nullptr, 0 }
};

static void webProcessTestRunnerFinalize(JSObjectRef object)
{
    g_object_unref(JSObjectGetPrivate(object));
}

static void windowObjectClearedCallback(WebKitScriptWorld* world, WebKitWebPage* webPage, WebKitFrame* frame, WebKitWebExtension* extension)
{
    JSGlobalContextRef context = webkit_frame_get_javascript_context_for_script_world(frame, world);
    JSObjectRef globalObject = JSContextGetGlobalObject(context);

    JSClassDefinition classDefinition = kJSClassDefinitionEmpty;
    classDefinition.className = "WebProcessTestRunner";
    classDefinition.staticFunctions = webProcessTestRunnerStaticFunctions;
    classDefinition.finalize = webProcessTestRunnerFinalize;

    JSClassRef jsClass = JSClassCreate(&classDefinition);
    JSObjectRef classObject = JSObjectMake(context, jsClass, g_object_ref(webPage));
    JSRetainPtr<JSStringRef> propertyString(Adopt, JSStringCreateWithUTF8CString("WebProcessTestRunner"));
    JSObjectSetProperty(context, globalObject, propertyString.get(), classObject, kJSPropertyAttributeNone, nullptr);
    JSClassRelease(jsClass);
}

extern "C" void webkit_web_extension_initialize(WebKitWebExtension* extension)
{
    g_signal_connect(webkit_script_world_get_default(), "window-object-cleared", G_CALLBACK(windowObjectClearedCallback), extension);
}