-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebview.c
282 lines (225 loc) · 6.57 KB
/
webview.c
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
/* webview extension for PHP */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "webview.h"
#include "ext/standard/info.h"
#include "php_webview.h"
#include "webview_arginfo.h"
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
zend_result php_json_decode_ex(zval *return_value, const char *str, size_t str_len, zend_long options, zend_long depth);
static zend_object_handlers webview_object_handlers;
typedef struct php_webview_t
{
webview_t native;
zend_object std;
} php_webview_t;
typedef struct bind_arg_t
{
webview_t native;
zend_fcall_info *fci;
zend_fcall_info_cache *fcc;
} bind_arg_t;
#define Z_WEBVIEW_P(zv) \
((php_webview_t *)((char *)(Z_OBJ_P(zv)) - XtOffsetOf(php_webview_t, std)))
zend_object *webview_new(zend_class_entry *ce)
{
php_webview_t *wv = zend_object_alloc(sizeof(php_webview_t), ce);
zend_object_std_init(&wv->std, ce);
wv->std.handlers = &webview_object_handlers;
return &wv->std;
}
PHP_METHOD(Webview, __construct)
{
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
zend_long debug = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(debug)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() > 0)
{
zend_update_property_long(Z_OBJCE_P(ZEND_THIS), Z_OBJ_P(ZEND_THIS),
"debug", sizeof("debug") - 1, debug);
}
container->native = webview_create((int) debug, NULL);
}
PHP_METHOD(Webview, __destruct)
{
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
webview_destroy(container->native);
}
PHP_METHOD(Webview, set_title)
{
zval *title;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(title)
ZEND_PARSE_PARAMETERS_END();
webview_set_title(container->native, Z_STRVAL(*title));
}
PHP_METHOD(Webview, set_html)
{
zval *html;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(html)
ZEND_PARSE_PARAMETERS_END();
webview_set_html(container->native, Z_STRVAL(*html));
}
PHP_METHOD(Webview, set_size)
{
zend_long width, height, hint = 0;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_LONG(width)
Z_PARAM_LONG(height)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(hint)
ZEND_PARSE_PARAMETERS_END();
webview_set_size(container->native, (int) width, (int) height, hint);
}
PHP_METHOD(Webview, navigate)
{
zval *url;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(url)
ZEND_PARSE_PARAMETERS_END();
webview_navigate(container->native, Z_STRVAL(*url));
}
PHP_METHOD(Webview, init)
{
zval *js;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(js)
ZEND_PARSE_PARAMETERS_END();
webview_init(container->native, Z_STRVAL(*js));
}
PHP_METHOD(Webview, eval)
{
zval *js;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(js)
ZEND_PARSE_PARAMETERS_END();
webview_eval(container->native, Z_STRVAL(*js));
}
PHP_METHOD(Webview, run)
{
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_NONE();
webview_run(container->native);
}
PHP_METHOD(Webview, terminate)
{
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_NONE();
webview_terminate(container->native);
}
void bind_callback_handler(const char *seq, const char *req, void *arg)
{
bind_arg_t *context = (bind_arg_t *)arg;
zval args;
zval *result = emalloc(sizeof(zval));
context->fci->retval = result;
context->fci->param_count = 0;
if (strlen(req) > 0) {
php_json_decode_ex(&args, req, strlen(req), 1, 512);
context->fci->param_count = zend_array_count(Z_ARRVAL(args));
zend_fcall_info_args(context->fci, &args);
}
if(zend_call_function(context->fci, context->fcc) == SUCCESS){
if (ZVAL_IS_NULL(result)) {
ZVAL_STRING(result, "[]");
}
webview_return(context->native, seq, 0, Z_STRVAL_P(result));
} else {
webview_return(context->native, seq, 1, "[]");
}
zval_ptr_dtor(result);
zval_ptr_dtor(&args);
}
PHP_METHOD(Webview, bind)
{
zval *name;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
bind_arg_t *context = emalloc(sizeof(bind_arg_t));
context->native = container->native;
context->fci = emalloc(sizeof(zend_fcall_info));
context->fcc = emalloc(sizeof(zend_fcall_info_cache));
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ZVAL(name)
Z_PARAM_FUNC(*context->fci, *context->fcc)
ZEND_PARSE_PARAMETERS_END();
Z_TRY_ADDREF(context->fci->function_name);
if (context->fci->object != NULL){
++context->fci->object->gc.refcount; // @todo: really needed?
}
webview_bind(container->native, Z_STRVAL(*name), bind_callback_handler, context);
}
PHP_METHOD(Webview, unbind)
{
zval *name;
php_webview_t *container = Z_WEBVIEW_P(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(name)
ZEND_PARSE_PARAMETERS_END();
webview_unbind(container->native, Z_STRVAL(*name));
}
PHP_MINIT_FUNCTION(webview)
{
zend_class_entry *ce = register_class_Webview();
ce->create_object = webview_new;
memcpy(&webview_object_handlers, &std_object_handlers,
sizeof(zend_object_handlers));
webview_object_handlers.offset = XtOffsetOf(php_webview_t, std);
REGISTER_LONG_CONSTANT("WEBVIEW_HINT_NONE", 0, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("WEBVIEW_HINT_MIN", 1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("WEBVIEW_HINT_MAX", 2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("WEBVIEW_HINT_FIXED", 3, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(webview)
{
#if defined(ZTS) && defined(COMPILE_DL_WEBVIEW)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(webview)
{
php_info_print_table_start();
php_info_print_table_row(2, "webview support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ webview_module_entry */
zend_module_entry webview_module_entry = {
STANDARD_MODULE_HEADER,
"webview", /* Extension name */
NULL, /* zend_function_entry */
PHP_MINIT(webview), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(webview), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(webview), /* PHP_MINFO - Module info */
PHP_WEBVIEW_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES};
/* }}} */
#ifdef COMPILE_DL_WEBVIEW
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(webview)
#endif