-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (163 loc) · 7.49 KB
/
main.cpp
File metadata and controls
190 lines (163 loc) · 7.49 KB
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
/*
* Copyright (C) 2015, 2016 Igalia S.L.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <WPE/WebKit.h>
#include <WPE/WebKit/WKCookieManagerSoup.h>
#include <cstdio>
#include <glib.h>
#include <initializer_list>
#include <stdlib.h>
WKPageNavigationClientV0 s_navigationClient = {
{ 0, nullptr },
// decidePolicyForNavigationAction
[](WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef listener, WKTypeRef, const void*) {
WKFramePolicyListenerUse(listener);
},
// decidePolicyForNavigationResponse
[](WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef listener, WKTypeRef, const void*) {
WKFramePolicyListenerUse(listener);
},
nullptr, // decidePolicyForPluginLoad
nullptr, // didStartProvisionalNavigation
nullptr, // didReceiveServerRedirectForProvisionalNavigation
nullptr, // didFailProvisionalNavigation
nullptr, // didCommitNavigation
nullptr, // didFinishNavigation
nullptr, // didFailNavigation
nullptr, // didFailProvisionalLoadInSubframe
// didFinishDocumentLoad
[](WKPageRef page, WKNavigationRef, WKTypeRef, const void*) {
WKStringRef messageName = WKStringCreateWithUTF8CString("Hello");
WKMutableArrayRef messageBody = WKMutableArrayCreate();
for (auto& item : { "Test1", "Test2", "Test3" }) {
WKStringRef itemString = WKStringCreateWithUTF8CString(item);
WKArrayAppendItem(messageBody, itemString);
WKRelease(itemString);
}
fprintf(stderr, "[WPELauncher] Hello InjectedBundle ...\n");
WKPagePostMessageToInjectedBundle(page, messageName, messageBody);
WKRelease(messageBody);
WKRelease(messageName);
},
nullptr, // didSameDocumentNavigation
nullptr, // renderingProgressDidChange
nullptr, // canAuthenticateAgainstProtectionSpace
nullptr, // didReceiveAuthenticationChallenge
// webProcessDidCrash
[](WKPageRef page, const void*) {
fprintf(stderr, "ERROR: WebProcess crashed: exiting ...\n");
fflush(stderr);
exit(1);
},
nullptr, // copyWebCryptoMasterKey
nullptr, // didBeginNavigationGesture
nullptr, // willEndNavigationGesture
nullptr, // didEndNavigationGesture
nullptr, // didRemoveNavigationGestureSnapshot
};
WKViewClientV0 s_viewClient = {
{ 0, nullptr },
// frameDisplayed
[](WKViewRef, const void*) {
static unsigned s_frameCount = 0;
static gint64 lastDumpTime = g_get_monotonic_time();
if (!g_getenv("WPE_DISPLAY_FPS"))
return;
++s_frameCount;
gint64 time = g_get_monotonic_time();
if (time - lastDumpTime >= 5 * G_USEC_PER_SEC) {
fprintf(stderr, "[WPELauncher] %.2f FPS\n",
s_frameCount * G_USEC_PER_SEC * 1.0 / (time - lastDumpTime));
s_frameCount = 0;
lastDumpTime = time;
}
},
};
int main(int argc, char* argv[])
{
GMainLoop* loop = g_main_loop_new(nullptr, FALSE);
auto contextConfiguration = WKContextConfigurationCreate();
auto injectedBundlePath = WKStringCreateWithUTF8CString("/usr/lib/libWPEInjectedBundle.so");
WKContextConfigurationSetInjectedBundlePath(contextConfiguration, injectedBundlePath);
gchar *wpeStoragePath = g_build_filename(g_get_user_cache_dir(), "wpe", "local-storage", nullptr);
g_mkdir_with_parents(wpeStoragePath, 0700);
auto storageDirectory = WKStringCreateWithUTF8CString(wpeStoragePath);
g_free(wpeStoragePath);
WKContextConfigurationSetLocalStorageDirectory(contextConfiguration, storageDirectory);
gchar *wpeDiskCachePath = g_build_filename(g_get_user_cache_dir(), "wpe", "disk-cache", nullptr);
g_mkdir_with_parents(wpeDiskCachePath, 0700);
auto diskCacheDirectory = WKStringCreateWithUTF8CString(wpeDiskCachePath);
g_free(wpeDiskCachePath);
WKContextConfigurationSetDiskCacheDirectory(contextConfiguration, diskCacheDirectory);
WKRelease(injectedBundlePath);
WKContextRef context = WKContextCreateWithConfiguration(contextConfiguration);
WKRelease(contextConfiguration);
auto pageGroupIdentifier = WKStringCreateWithUTF8CString("WPEPageGroup");
auto pageGroup = WKPageGroupCreateWithIdentifier(pageGroupIdentifier);
WKRelease(pageGroupIdentifier);
auto preferences = WKPreferencesCreate();
// Allow mixed content.
WKPreferencesSetAllowRunningOfInsecureContent(preferences, true);
WKPreferencesSetAllowDisplayOfInsecureContent(preferences, true);
WKPreferencesSetWebSecurityEnabled(preferences, false);
// By default allow console log messages to system console reporting.
if (!g_getenv("WPE_SHELL_DISABLE_CONSOLE_LOG"))
WKPreferencesSetLogsPageMessagesToSystemConsoleEnabled(preferences, true);
if (const char* value = g_getenv("WEBKIT_INSPECTOR_SERVER")) {
// Very naïve check for <ip>:<port>
if (strlen(value) > 2 && strchr(value, ':'))
WKPreferencesSetDeveloperExtrasEnabled(preferences, true);
}
WKPageGroupSetPreferences(pageGroup, preferences);
auto pageConfiguration = WKPageConfigurationCreate();
WKPageConfigurationSetContext(pageConfiguration, context);
WKPageConfigurationSetPageGroup(pageConfiguration, pageGroup);
WKPreferencesSetFullScreenEnabled(preferences, true);
if (!!g_getenv("WPE_SHELL_COOKIE_STORAGE")) {
gchar *cookieDatabasePath = g_build_filename(g_get_user_cache_dir(), "cookies.db", nullptr);
auto path = WKStringCreateWithUTF8CString(cookieDatabasePath);
g_free(cookieDatabasePath);
auto cookieManager = WKContextGetCookieManager(context);
WKCookieManagerSetCookiePersistentStorage(cookieManager, path, kWKCookieStorageTypeSQLite);
}
auto view = WKViewCreate(pageConfiguration);
WKViewSetViewClient(view, &s_viewClient.base);
auto page = WKViewGetPage(view);
WKPageSetPageNavigationClient(page, &s_navigationClient.base);
const char* url = "http://youtube.com/tv";
if (argc > 1)
url = argv[1];
auto shellURL = WKURLCreateWithUTF8CString(url);
WKPageLoadURL(page, shellURL);
WKRelease(shellURL);
g_main_loop_run(loop);
WKRelease(view);
WKRelease(pageConfiguration);
WKRelease(pageGroup);
WKRelease(context);
WKRelease(preferences);
g_main_loop_unref(loop);
return 0;
}