@@ -124,69 +124,209 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
124
124
V8_CHECK_CONSTRUCTOR ();
125
125
V8_CHECK_ARGS_LEN_MIN_MAX (1 , 4 );
126
126
127
- V8_ARG_TO_STRING (1 , url);
128
-
129
127
alt::IResource* altres = V8ResourceImpl::GetResource (isolate->GetEnteredOrMicrotaskContext ());
130
128
V8_CHECK (altres, " invalid resource" );
131
129
132
130
alt::IWebView* view = nullptr ;
133
131
134
- if (info.Length () == 4 )
132
+ if (info.Length () == 1 && info[ 0 ]-> IsObject () )
135
133
{
136
- V8_ARG_TO_BOOLEAN (2 , isOverlayBool);
137
- V8_ARG_TO_OBJECT (3 , pos);
138
- V8_ARG_TO_OBJECT (4 , size);
139
-
140
- V8_OBJECT_GET_INT (pos, " x" , posX);
141
- V8_OBJECT_GET_INT (pos, " y" , posY);
134
+ V8_ARG_TO_OBJECT (1 , params);
135
+ using values_t = std::unordered_map<std::string, v8::Local<v8::Value>>;
136
+ std::optional<values_t > values = V8Helpers::CppValue<v8::Local<v8::Value>>(params);
137
+ V8_CHECK (values.has_value (), " Params are empty" );
138
+ V8_CHECK (!values->empty (), " Params are empty" );
139
+
140
+ alt::WebViewHeaders headers;
141
+ std::vector<alt::WebViewCookie> cookies;
142
+ int posX = 0 ;
143
+ int posY = 0 ;
144
+ int sizeX = 0 ;
145
+ int sizeY = 0 ;
146
+ int drawableHash = 0 ;
147
+ std::string targetTextureStr;
148
+ bool isOverlay = false ;
149
+ bool onTextureView = false ;
150
+ V8_TO_STRING (values->at (" url" ), url);
151
+
152
+ if (values->contains (" pos" ))
153
+ {
154
+ V8_TO_OBJECT (values->at (" pos" ), pos);
155
+ V8_OBJECT_GET_INT (pos, " x" , v8posX);
156
+ V8_OBJECT_GET_INT (pos, " y" , v8posY);
157
+ posX = v8posX;
158
+ posY = v8posY;
159
+ }
160
+ if (values->contains (" size" ))
161
+ {
162
+ V8_TO_OBJECT (values->at (" size" ), size);
163
+ V8_OBJECT_GET_INT (size, " x" , v8sizeX);
164
+ V8_OBJECT_GET_INT (size, " y" , v8sizeY);
165
+ sizeX = v8sizeX;
166
+ sizeY = v8sizeY;
167
+ }
168
+ if (values->contains (" isOverlay" ))
169
+ {
170
+ V8_TO_BOOLEAN (values->at (" isOverlay" ), v8isOverlay);
171
+ isOverlay = v8isOverlay;
172
+ }
173
+ if (values->contains (" drawableHash" ))
174
+ {
175
+ V8_CHECK (values->contains (" targetTexture" ), " targetTexture should be specified for drawableHash" );
176
+ V8_TO_INTEGER (values->at (" drawableHash" ), v8drawableHash);
177
+ drawableHash = v8drawableHash;
178
+ onTextureView = true ;
179
+ }
180
+ if (values->contains (" targetTexture" ))
181
+ {
182
+ V8_CHECK (values->contains (" drawableHash" ), " drawableHash should be specified for targetTexture" );
183
+ V8_TO_STRING (values->at (" targetTexture" ), v8targetTexture);
184
+ targetTextureStr = v8targetTexture;
185
+ }
186
+ if (values->contains (" headers" ))
187
+ {
188
+ V8_TO_OBJECT (values->at (" headers" ), v8headers);
189
+ auto headers_opt = V8Helpers::CppValue<std::string>(v8headers);
190
+ V8_CHECK (headers_opt.has_value (), " headers object is empty" );
191
+ headers = std::move (headers_opt).value ();
192
+ }
193
+ if (values->contains (" cookies" ))
194
+ {
195
+ V8_TO_ARRAY (values->at (" cookies" ), v8cookies);
196
+ auto cookies_opt = V8Helpers::CppValue<v8::Local<v8::Value>>(v8cookies);
197
+ V8_CHECK (cookies_opt.has_value (), " cookies array is empty" );
198
+ for (auto & cookie: cookies_opt.value ())
199
+ {
200
+
201
+ V8_TO_OBJECT (cookie, v8cookie);
202
+ auto cookie_opt = V8Helpers::CppValue<v8::Local<v8::Value>>(v8cookie);
203
+ V8_CHECK (cookie_opt.has_value (), " cookie object is empty" );
204
+
205
+ V8_OBJECT_GET_STRING (v8cookie, " url" , cookie_url);
206
+ V8_OBJECT_GET_STRING (v8cookie, " name" , cookie_name);
207
+ V8_OBJECT_GET_STRING (v8cookie, " value" , cookie_value);
208
+
209
+ V8_CHECK (cookie_name.find (" __altv_" ) == 0 , " Cookie name should start with '__altv_'" );
210
+ WebViewCookie new_cookie {};
211
+ new_cookie.url = std::move (cookie_url);
212
+ new_cookie.name = std::move (cookie_name);
213
+ new_cookie.value = std::move (cookie_value);
214
+ if (cookie_opt->contains (" httpOnly" ))
215
+ {
216
+ V8_TO_BOOLEAN (cookie_opt->at (" httpOnly" ), v8http_only);
217
+ new_cookie.httpOnly = v8http_only;
218
+ }
219
+ if (cookie_opt->contains (" secure" ))
220
+ {
221
+ V8_TO_BOOLEAN (cookie_opt->at (" secure" ), v8secure);
222
+ new_cookie.secure = v8secure;
223
+ }
224
+ if (cookie_opt->contains (" domain" ))
225
+ {
226
+ V8_TO_STRING (cookie_opt->at (" domain" ), v8domain);
227
+ new_cookie.domain = std::move (v8domain);
228
+ }
229
+ if (cookie_opt->contains (" path" ))
230
+ {
231
+ V8_TO_STRING (cookie_opt->at (" path" ), v8path);
232
+ new_cookie.path = std::move (v8path);
233
+ }
234
+ if (cookie_opt->contains (" sameSite" ))
235
+ {
236
+ V8_TO_STRING (cookie_opt->at (" sameSite" ), v8same_site);
237
+ V8_CHECK (v8same_site == " NO_RESTRICTION" || v8same_site == " LAX_MODE" || v8same_site == " STRICT_MODE" ,
238
+ " sameSite value should be one of ['NO_RESTRICTION', 'LAX_MODE', 'STRICT_MODE']" );
239
+ new_cookie.sameSite = std::move (v8same_site);
240
+ }
241
+ if (cookie_opt->contains (" priority" ))
242
+ {
243
+ V8_TO_STRING (cookie_opt->at (" priority" ), v8priority);
244
+ V8_CHECK (v8priority == " LOW" || v8priority == " MEDIUM" || v8priority == " HIGH" , " priority value should be one of ['LOW', 'MEDIUM', 'HIGH']" );
245
+ new_cookie.priority = std::move (v8priority);
246
+ }
247
+ if (cookie_opt->contains (" expires" ))
248
+ {
249
+ V8_TO_INTEGER (cookie_opt->at (" expires" ), v8expires);
250
+ new_cookie.expires = v8expires;
251
+ }
252
+
253
+ V8_CHECK (!(new_cookie.httpOnly && new_cookie.secure ), " HTTP only and secure cannot be combined" );
254
+
255
+ cookies.emplace_back (std::move (new_cookie));
256
+ }
257
+ }
142
258
143
- V8_OBJECT_GET_INT (size, " x" , sizeX);
144
- V8_OBJECT_GET_INT (size, " y" , sizeY);
259
+ if (onTextureView)
260
+ {
261
+ auto texture = alt::ICore::Instance ().GetTextureFromDrawable (drawableHash, targetTextureStr);
262
+ V8_CHECK (texture != nullptr , " Texture not found" );
145
263
146
- view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { sizeX, sizeY }, true , isOverlayBool, altres);
147
- }
148
- else if (info.Length () == 3 && info[2 ]->IsObject ())
264
+ view = alt::ICore::Instance ().CreateWebView (url, (uint32_t )drawableHash, targetTextureStr, altres, headers, cookies);
265
+ V8_CHECK (view, " Interactive WebView cannot be created" );
266
+ } else
267
+ {
268
+ view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { sizeX, sizeY }, true , isOverlay, altres, headers, cookies);
269
+ }
270
+ } else
149
271
{
150
- V8_ARG_TO_OBJECT (2 , pos);
151
- V8_ARG_TO_OBJECT (3 , size);
272
+ V8_ARG_TO_STRING (1 , url);
273
+ if (info.Length () == 4 )
274
+ {
275
+ V8_ARG_TO_BOOLEAN (2 , isOverlayBool);
276
+ V8_ARG_TO_OBJECT (3 , pos);
277
+ V8_ARG_TO_OBJECT (4 , size);
152
278
153
- V8_OBJECT_GET_INT (pos, " x" , posX);
154
- V8_OBJECT_GET_INT (pos, " y" , posY);
279
+ V8_OBJECT_GET_INT (pos, " x" , posX);
280
+ V8_OBJECT_GET_INT (pos, " y" , posY);
155
281
156
- V8_OBJECT_GET_INT (size, " x" , sizeX);
157
- V8_OBJECT_GET_INT (size, " y" , sizeY);
282
+ V8_OBJECT_GET_INT (size, " x" , sizeX);
283
+ V8_OBJECT_GET_INT (size, " y" , sizeY);
158
284
159
- view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { sizeX, sizeY }, true , false , altres);
160
- }
161
- else if (info.Length () == 3 )
162
- {
163
- V8_ARG_TO_INT (2 , drawableHash );
164
- V8_ARG_TO_STRING (3 , targetTextureStr );
285
+ view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { sizeX, sizeY }, true , isOverlayBool , altres);
286
+ }
287
+ else if (info.Length () == 3 && info[ 2 ]-> IsObject () )
288
+ {
289
+ V8_ARG_TO_OBJECT (2 , pos );
290
+ V8_ARG_TO_OBJECT (3 , size );
165
291
166
- auto texture = alt::ICore::Instance (). GetTextureFromDrawable (drawableHash, targetTextureStr );
167
- V8_CHECK (texture != nullptr , " Texture not found " );
292
+ V8_OBJECT_GET_INT (pos, " x " , posX );
293
+ V8_OBJECT_GET_INT (pos, " y " , posY );
168
294
169
- view = alt::ICore::Instance ().CreateWebView (url, (uint32_t )drawableHash, targetTextureStr, altres);
170
- V8_CHECK (view, " Interactive WebView cannot be created" );
171
- }
172
- else if (info.Length () == 2 && info[1 ]->IsObject ())
173
- {
174
- V8_ARG_TO_OBJECT (2 , pos);
295
+ V8_OBJECT_GET_INT (size, " x" , sizeX);
296
+ V8_OBJECT_GET_INT (size, " y" , sizeY);
175
297
176
- V8_OBJECT_GET_INT (pos, " x" , posX);
177
- V8_OBJECT_GET_INT (pos, " y" , posY);
298
+ view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { sizeX, sizeY }, true , false , altres);
299
+ }
300
+ else if (info.Length () == 3 )
301
+ {
302
+ V8_ARG_TO_INT (2 , drawableHash);
303
+ V8_ARG_TO_STRING (3 , targetTextureStr);
178
304
179
- view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { 0 , 0 }, true , false , altres);
180
- }
181
- else if (info.Length () == 2 )
182
- {
183
- V8_ARG_TO_BOOLEAN (2 , isOverlayBool);
305
+ auto texture = alt::ICore::Instance ().GetTextureFromDrawable (drawableHash, targetTextureStr);
306
+ V8_CHECK (texture != nullptr , " Texture not found" );
184
307
185
- view = alt::ICore::Instance ().CreateWebView (url, { 0 , 0 }, { 0 , 0 }, true , isOverlayBool, altres);
186
- }
187
- else
188
- {
189
- view = alt::ICore::Instance ().CreateWebView (url, { 0 , 0 }, { 0 , 0 }, true , false , altres);
308
+ view = alt::ICore::Instance ().CreateWebView (url, (uint32_t )drawableHash, targetTextureStr, altres);
309
+ V8_CHECK (view, " Interactive WebView cannot be created" );
310
+ }
311
+ else if (info.Length () == 2 && info[1 ]->IsObject ())
312
+ {
313
+ V8_ARG_TO_OBJECT (2 , pos);
314
+
315
+ V8_OBJECT_GET_INT (pos, " x" , posX);
316
+ V8_OBJECT_GET_INT (pos, " y" , posY);
317
+
318
+ view = alt::ICore::Instance ().CreateWebView (url, { posX, posY }, { 0 , 0 }, true , false , altres);
319
+ }
320
+ else if (info.Length () == 2 )
321
+ {
322
+ V8_ARG_TO_BOOLEAN (2 , isOverlayBool);
323
+
324
+ view = alt::ICore::Instance ().CreateWebView (url, { 0 , 0 }, { 0 , 0 }, true , isOverlayBool, altres);
325
+ }
326
+ else
327
+ {
328
+ view = alt::ICore::Instance ().CreateWebView (url, { 0 , 0 }, { 0 , 0 }, true , false , altres);
329
+ }
190
330
}
191
331
192
332
V8_BIND_BASE_OBJECT (view, " Failed to create WebView" );
@@ -205,6 +345,68 @@ static void SetExtraHeader(const v8::FunctionCallbackInfo<v8::Value>& info)
205
345
view->SetExtraHeader (name, value);
206
346
}
207
347
348
+ static void SetCookie (const v8::FunctionCallbackInfo<v8::Value>& info)
349
+ {
350
+ V8_GET_ISOLATE_CONTEXT ();
351
+
352
+ V8_GET_THIS_BASE_OBJECT (view, alt::IWebView);
353
+
354
+ V8_CHECK_ARGS_LEN (1 );
355
+ V8_ARG_TO_OBJECT (1 , cookie);
356
+
357
+ auto cookie_opt = V8Helpers::CppValue<v8::Local<v8::Value>>(cookie);
358
+ V8_CHECK (cookie_opt.has_value (), " cookie object is empty" );
359
+
360
+ V8_OBJECT_GET_STRING (cookie, " url" , cookie_url);
361
+ V8_OBJECT_GET_STRING (cookie, " name" , cookie_name);
362
+ V8_OBJECT_GET_STRING (cookie, " value" , cookie_value);
363
+
364
+ V8_CHECK (cookie_name.find (" __altv_" ) == 0 , " Cookie name should start with '__altv_'" );
365
+ WebViewCookie new_cookie {};
366
+ new_cookie.url = std::move (cookie_url);
367
+ new_cookie.name = std::move (cookie_name);
368
+ new_cookie.value = std::move (cookie_value);
369
+ if (cookie_opt->contains (" httpOnly" ))
370
+ {
371
+ V8_TO_BOOLEAN (cookie_opt->at (" httpOnly" ), v8http_only);
372
+ new_cookie.httpOnly = v8http_only;
373
+ }
374
+ if (cookie_opt->contains (" secure" ))
375
+ {
376
+ V8_TO_BOOLEAN (cookie_opt->at (" secure" ), v8secure);
377
+ new_cookie.secure = v8secure;
378
+ }
379
+ if (cookie_opt->contains (" domain" ))
380
+ {
381
+ V8_TO_STRING (cookie_opt->at (" domain" ), v8domain);
382
+ new_cookie.domain = std::move (v8domain);
383
+ }
384
+ if (cookie_opt->contains (" path" ))
385
+ {
386
+ V8_TO_STRING (cookie_opt->at (" path" ), v8path);
387
+ new_cookie.path = std::move (v8path);
388
+ }
389
+ if (cookie_opt->contains (" sameSite" ))
390
+ {
391
+ V8_TO_STRING (cookie_opt->at (" sameSite" ), v8same_site);
392
+ new_cookie.sameSite = std::move (v8same_site);
393
+ }
394
+ if (cookie_opt->contains (" priority" ))
395
+ {
396
+ V8_TO_STRING (cookie_opt->at (" priority" ), v8priority);
397
+ new_cookie.priority = std::move (v8priority);
398
+ }
399
+ if (cookie_opt->contains (" expires" ))
400
+ {
401
+ V8_TO_INTEGER (cookie_opt->at (" expires" ), v8expires);
402
+ new_cookie.expires = v8expires;
403
+ }
404
+
405
+ V8_CHECK (!(new_cookie.httpOnly && new_cookie.secure ), " HTTP only and secure cannot be combined" );
406
+
407
+ view->SetCookie (new_cookie);
408
+ }
409
+
208
410
static void SetZoomLevel (const v8::FunctionCallbackInfo<v8::Value>& info)
209
411
{
210
412
V8_GET_ISOLATE_CONTEXT ();
@@ -345,6 +547,7 @@ extern V8Class v8WebView("WebView",
345
547
V8Helpers::SetMethod<IWebView, &IWebView::Unfocus>(isolate, tpl, " unfocus" );
346
548
347
549
V8Helpers::SetMethod (isolate, tpl, " setExtraHeader" , &SetExtraHeader);
550
+ V8Helpers::SetMethod (isolate, tpl, " setCookie" , &SetCookie);
348
551
V8Helpers::SetMethod (isolate, tpl, " setZoomLevel" , &SetZoomLevel);
349
552
V8Helpers::SetMethod (isolate, tpl, " reload" , &Reload);
350
553
0 commit comments