• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Web
18  * @{
19  *
20  * @brief Provides APIs to intercept the request from ArkWeb.
21  * @since 12
22  */
23 /**
24  * @file arkweb_scheme_handler.h
25  *
26  * @brief Declares the APIs to intercept the request from ArkWeb.
27  * @library libohweb.so
28  * @syscap SystemCapability.Web.Webview.Core
29  * @since 12
30  */
31 #ifndef ARKWEB_SCHEME_HANDLER_H
32 #define ARKWEB_SCHEME_HANDLER_H
33 
34 #include <cstdint>
35 
36 #include "arkweb_error_code.h"
37 #include "arkweb_net_error_list.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * @brief Configuration information for custom schemes.
45  *
46  * @syscap SystemCapability.Web.Webview.Core
47  * @since 12
48  */
49 typedef enum ArkWeb_CustomSchemeOption {
50     OH_ARKWEB_SCHEME_OPTION_NONE = 0,
51 
52     /*
53      * @brief If ARKWEB_SCHEME_OPTION_STANDARD is set the scheme will be handled as a standard scheme. The standard
54      *        schemes needs to comply with the URL normalization and parsing rules defined in Section 3.1 of RFC 1738,
55      *        which can be found in the http://www.ietf.org/rfc/rfc1738.txt.
56      *
57      * @syscap SystemCapability.Web.Webview.Core
58      * @since 12
59      */
60     ARKWEB_SCHEME_OPTION_STANDARD = 1 << 0,
61 
62     /*
63      * @brief If ARKWEB_SCHEME_OPTION_LOCAL is set, the same security rules as those applied to the "file" URL will be
64      *        used to handle the scheme.
65      *
66      * @syscap SystemCapability.Web.Webview.Core
67      * @since 12
68      */
69     ARKWEB_SCHEME_OPTION_LOCAL = 1 << 1,
70 
71     /*
72      * @brief If ARKWEB_SCHEME_OPTION_DISPLAY_ISOLATED is set, then the scheme can only be displayed from other content
73      *        hosted using the same scheme.
74      *
75      * @syscap SystemCapability.Web.Webview.Core
76      * @since 12
77      */
78     ARKWEB_SCHEME_OPTION_DISPLAY_ISOLATED = 1 << 2,
79 
80     /*
81      * @brief If ARKWEB_SCHEME_OPTION_SECURE is set, the same security rules as those applied to the "https" URL will be
82      *        used to handle the scheme.
83      *
84      * @syscap SystemCapability.Web.Webview.Core
85      * @since 12
86      */
87     ARKWEB_SCHEME_OPTION_SECURE = 1 << 3,
88 
89     /*
90      * @brief If ARKWEB_SCHEME_OPTION_CORS_ENABLED is set, then the scheme can be sent CORS requests. In most case this
91      *        value should be set when ARKWEB_SCHEME_OPTION_STANDARD is set.
92      *
93      * @syscap SystemCapability.Web.Webview.Core
94      * @since 12
95      */
96     ARKWEB_SCHEME_OPTION_CORS_ENABLED = 1 << 4,
97 
98     /*
99      * @brief If ARKWEB_SCHEME_OPTION_CSP_BYPASSING is set, then this scheme can bypass Content Security Policy (CSP)
100      *        checks. In most cases, this value should not be set when ARKWEB_SCHEME_OPTION_STANDARD is set.
101      *
102      * @syscap SystemCapability.Web.Webview.Core
103      * @since 12
104      */
105     ARKWEB_SCHEME_OPTION_CSP_BYPASSING = 1 << 5,
106 
107     /*
108      * @brief If ARKWEB_SCHEME_OPTION_FETCH_ENABLED is set, then this scheme can perform FETCH API requests.
109      *
110      * @syscap SystemCapability.Web.Webview.Core
111      * @since 12
112      */
113     ARKWEB_SCHEME_OPTION_FETCH_ENABLED = 1 << 6,
114 
115     /*
116      * @brief If ARKWEB_SCHEME_OPTION_CODE_CACHE_ENABLED is set, then the js of this scheme can generate code cache.
117      *
118      * @syscap SystemCapability.Web.Webview.Core
119      * @since 12
120      */
121     ARKWEB_SCHEME_OPTION_CODE_CACHE_ENABLED = 1 << 7,
122 } ArkWeb_CustomSchemeOption;
123 
124 /*
125  * @brief  This class is used to intercept requests for a specified scheme.
126  *
127  * @syscap SystemCapability.Web.Webview.Core
128  * @since 12
129  */
130 typedef struct ArkWeb_SchemeHandler_ ArkWeb_SchemeHandler;
131 
132 /*
133  * @brief Used to intercept url requests. Response headers and body can be sent through ArkWeb_ResourceHandler.
134  *
135  * @syscap SystemCapability.Web.Webview.Core
136  * @since 12
137  */
138 typedef struct ArkWeb_ResourceHandler_ ArkWeb_ResourceHandler;
139 
140 /*
141  * @brief The response of the intercepted request.
142  *
143  * @syscap SystemCapability.Web.Webview.Core
144  * @since 12
145  */
146 typedef struct ArkWeb_Response_ ArkWeb_Response;
147 
148 /*
149  * @brief The info of the request. You can obtain the requested URL, method, post data, and other information through
150  *        OH_ArkWeb_ResourceRequest.
151  *
152  * @syscap SystemCapability.Web.Webview.Core
153  * @since 12
154  */
155 typedef struct ArkWeb_ResourceRequest_ ArkWeb_ResourceRequest;
156 
157 /*
158  * @brief The request headers of the request.
159  *
160  * @syscap SystemCapability.Web.Webview.Core
161  * @since 12
162  */
163 typedef struct ArkWeb_RequestHeaderList_ ArkWeb_RequestHeaderList;
164 
165 /*
166  * @brief The http body of the request. Use OH_ArkWebHttpBodyStream_* interface to read the body.
167  *
168  * @syscap SystemCapability.Web.Webview.Core
169  * @since 12
170  */
171 typedef struct ArkWeb_HttpBodyStream_ ArkWeb_HttpBodyStream;
172 
173 
174 /*
175  * @brief Callback for handling the request. This will called on the IO thread. should not use resourceHandler in the
176  *        function.
177  * @param schemeHandler The ArkWeb_SchemeHandler.
178  * @param resourceRequest Obtain request's information through this.
179  * @param resourceHandler The ArkWeb_ResourceHandler for the request. It should not be used if intercept is set to
180  *                        false.
181  * @param intercept If true will intercept the request, if false otherwise.
182  *
183  * @syscap SystemCapability.Web.Webview.Core
184  * @since 12
185  */
186 typedef void (*ArkWeb_OnRequestStart)(const ArkWeb_SchemeHandler* schemeHandler,
187                                       ArkWeb_ResourceRequest* resourceRequest,
188                                       const ArkWeb_ResourceHandler* resourceHandler,
189                                       bool* intercept);
190 
191 /*
192  * @brief Callback when the request is completed. This will called on the IO thread.
193  *        Should destory the resourceRequest by ArkWeb_ResourceRequest_Destroy and use ArkWeb_ResourceHandler_Destroy
194  *        destroy the ArkWeb_ResourceHandler received in ArkWeb_OnRequestStart.
195  * @param schemeHandler The ArkWeb_SchemeHandler.
196  * @param resourceRequest The ArkWeb_ResourceRequest.
197  *
198  * @syscap SystemCapability.Web.Webview.Core
199  * @since 12
200  */
201 typedef void (*ArkWeb_OnRequestStop)(const ArkWeb_SchemeHandler* schemeHandler,
202                                      const ArkWeb_ResourceRequest* resourceRequest);
203 
204 /*
205  * @brief Callback when the read operation done.
206  * @param httpBodyStream The ArkWeb_HttpBodyStream.
207  * @param buffer The buffer to receive data.
208  * @param bytesRead Callback after OH_ArkWebHttpBodyStream_Read. bytesRead greater than 0 means that the buffer is
209  *                  filled with data of bytesRead size. Caller can read from the buffer, and if
210  *                  OH_ArkWebHttpBodyStream_IsEOF is false, caller can continue to read the remaining data.
211  *
212  * @syscap SystemCapability.Web.Webview.Core
213  * @since 12
214  */
215 typedef void (*ArkWeb_HttpBodyStreamReadCallback)(const ArkWeb_HttpBodyStream* httpBodyStream,
216                                                   uint8_t* buffer,
217                                                   int bytesRead);
218 
219 /**
220  * @brief Callback when the read operation done.
221  * @param httpBodyStream The ArkWeb_HttpBodyStream.
222  * @param buffer The buffer to receive data.
223  * @param bytesRead Callback after OH_ArkWebHttpBodyStream_AsyncRead. bytesRead greater than 0 means that
224  *                  the buffer is filled with data of bytesRead size. Caller can read from the buffer, and if
225  *                  OH_ArkWebHttpBodyStream_IsEOF is false, caller can continue to read the remaining data.
226  *
227  * @syscap SystemCapability.Web.Webview.Core
228  * @since 20
229  */
230 typedef void (*ArkWeb_HttpBodyStreamAsyncReadCallback)(const ArkWeb_HttpBodyStream *httpBodyStream,
231                                                        uint8_t *buffer,
232                                                        int bytesRead);
233 
234 /*
235  * @brief  Callback when the init operation done.
236  * @param httpBodyStream The ArkWeb_HttpBodyStream.
237  * @param result ARKWEB_NET_OK on success otherwise refer to ARKWEB_NET_ERROR.
238  *
239  * @syscap SystemCapability.Web.Webview.Core
240  * @since 12
241  */
242 typedef void (*ArkWeb_HttpBodyStreamInitCallback)(const ArkWeb_HttpBodyStream* httpBodyStream, ArkWeb_NetError result);
243 
244 /*
245  * @brief Destroy the ArkWeb_RequestHeaderList.
246  * @param requestHeaderList The ArkWeb_RequestHeaderList to be destroyed.
247  *
248  * @syscap SystemCapability.Web.Webview.Core
249  * @since 12
250  */
251 void OH_ArkWebRequestHeaderList_Destroy(ArkWeb_RequestHeaderList* requestHeaderList);
252 
253 /*
254  * @brief Get the request headers size.
255  * @param requestHeaderList The list of request header.
256  * @return The size of request headers. -1 if requestHeaderList is invalid.
257  *
258  * @syscap SystemCapability.Web.Webview.Core
259  * @since 12
260  */
261 int32_t OH_ArkWebRequestHeaderList_GetSize(const ArkWeb_RequestHeaderList* requestHeaderList);
262 
263 /*
264  * @brief Get the specified request header.
265  * @param requestHeaderList The list of request header.
266  * @param index The index of request header.
267  * @param key The header key. Caller must release the string by OH_ArkWeb_ReleaseString.
268  * @param value The header value. Caller must release the string by OH_ArkWeb_ReleaseString.
269  *
270  * @syscap SystemCapability.Web.Webview.Core
271  * @since 12
272  */
273 void OH_ArkWebRequestHeaderList_GetHeader(const ArkWeb_RequestHeaderList* requestHeaderList,
274                                           int32_t index,
275                                           char** key,
276                                           char** value);
277 
278 /*
279  * @brief Set a user data to ArkWeb_ResourceRequest.
280  * @param resourceRequest The ArkWeb_ResourceRequest.
281  * @param userData The user data to set.
282  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
283  *
284  * @syscap SystemCapability.Web.Webview.Core
285  * @since 12
286  */
287 int32_t OH_ArkWebResourceRequest_SetUserData(ArkWeb_ResourceRequest* resourceRequest, void* userData);
288 
289 /*
290  * @brief Get the user data from ArkWeb_ResourceRequest.
291  * @param resourceRequest The ArkWeb_ResourceRequest.
292  * @return The set user data.
293  *
294  * @syscap SystemCapability.Web.Webview.Core
295  * @since 12
296  */
297 void* OH_ArkWebResourceRequest_GetUserData(const ArkWeb_ResourceRequest* resourceRequest);
298 
299 /*
300  * @brief Get the method of request.
301  * @param resourceRequest The ArkWeb_ResourceRequest.
302  * @param method The request's http method. This function will allocate memory for the method string and caller must
303  *               release the string by OH_ArkWeb_ReleaseString.
304  *
305  * @syscap SystemCapability.Web.Webview.Core
306  * @since 12
307  */
308 void OH_ArkWebResourceRequest_GetMethod(const ArkWeb_ResourceRequest* resourceRequest, char** method);
309 
310 /*
311  * @brief Get the url of request.
312  * @param resourceRequest The ArkWeb_ResourceRequest.
313  * @param url The request's url. This function will allocate memory for the url string and caller must release the
314  *            string by OH_ArkWeb_ReleaseString.
315  *
316  * @syscap SystemCapability.Web.Webview.Core
317  * @since 12
318  */
319 void OH_ArkWebResourceRequest_GetUrl(const ArkWeb_ResourceRequest* resourceRequest, char** url);
320 
321 /*
322  * @brief Create a ArkWeb_HttpBodyStream which used to read the http body.
323  * @param resourceRequest The ArkWeb_ResourceRequest.
324  * @param httpBodyStream The request's http body. This function will allocate memory for the http body stream and
325  *                       caller must release the httpBodyStream by OH_ArkWebResourceRequest_DestroyHttpBodyStream.
326  *
327  * @syscap SystemCapability.Web.Webview.Core
328  * @since 12
329  */
330 void OH_ArkWebResourceRequest_GetHttpBodyStream(const ArkWeb_ResourceRequest* resourceRequest,
331                                                 ArkWeb_HttpBodyStream** httpBodyStream);
332 
333 /*
334  * @brief Destroy the http body stream.
335  * @param httpBodyStream The httpBodyStream to be destroyed.
336  *
337  * @syscap SystemCapability.Web.Webview.Core
338  * @since 12
339  */
340 void OH_ArkWebResourceRequest_DestroyHttpBodyStream(ArkWeb_HttpBodyStream* httpBodyStream);
341 
342 /*
343  * @brief Get the resource type of request.
344  * @param resourceRequest The ArkWeb_ResourceRequest.
345  * @return The resource type of request. -1 if resourceRequest is invalid.
346  *
347  * @syscap SystemCapability.Web.Webview.Core
348  * @since 12
349  */
350 int32_t OH_ArkWebResourceRequest_GetResourceType(const ArkWeb_ResourceRequest* resourceRequest);
351 
352 /*
353  * @brief Get the url of frame which trigger this request.
354  * @param resourceRequest The ArkWeb_ResourceRequest.
355  * @param frameUrl The url of frame which trigger this request. This function will allocate memory for the url string
356  *            and caller must release the string by OH_ArkWeb_ReleaseString.
357  *
358  * @syscap SystemCapability.Web.Webview.Core
359  * @since 12
360  */
361 void OH_ArkWebResourceRequest_GetFrameUrl(const ArkWeb_ResourceRequest* resourceRequest, char** frameUrl);
362 
363 /*
364  * @brief Set a user data to ArkWeb_HttpBodyStream.
365  * @param httpBodyStream The ArkWeb_HttpBodyStream.
366  * @param userData The user data to set.
367  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
368  *
369  * @syscap SystemCapability.Web.Webview.Core
370  * @since 12
371  */
372 int32_t OH_ArkWebHttpBodyStream_SetUserData(ArkWeb_HttpBodyStream* httpBodyStream, void* userData);
373 
374 /*
375  * @brief Get the user data from ArkWeb_HttpBodyStream.
376  * @param httpBodyStream The ArkWeb_HttpBodyStream.
377  * @return The set user data.
378  *
379  * @syscap SystemCapability.Web.Webview.Core
380  * @since 12
381  */
382 void* OH_ArkWebHttpBodyStream_GetUserData(const ArkWeb_HttpBodyStream* httpBodyStream);
383 
384 /*
385  * @brief Set the callback for OH_ArkWebHttpBodyStream_Read, the result of OH_ArkWebHttpBodyStream_Read will be
386  *        notified to caller through the readCallback. The callback will runs in the same thread as
387  *        OH_ArkWebHttpBodyStream_Read.
388  * @param httpBodyStream The ArkWeb_HttpBodyStream.
389  * @param readCallback The callback of read function.
390  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
391  *
392  * @syscap SystemCapability.Web.Webview.Core
393  * @since 12
394  */
395 int32_t OH_ArkWebHttpBodyStream_SetReadCallback(ArkWeb_HttpBodyStream* httpBodyStream,
396                                                 ArkWeb_HttpBodyStreamReadCallback readCallback);
397 
398 
399 /**
400  * @brief Set the callback for OH_ArkWebHttpBodyStream_AsyncRead.
401  *
402  * The result of OH_ArkWebHttpBodyStream_AsyncRead will be notified to caller through the\n
403  * readCallback. The callback will runs in the ArkWeb worker thread.\n
404  *
405  * @param httpBodyStream The ArkWeb_HttpBodyStream.
406  * @param readCallback The callback of read function.
407  * @return {@link ARKWEB_NET_OK} 0 - Success.
408  * {@link ARKWEB_INVALID_PARAM} 17100101 - Invalid param.
409  *
410  * @syscap SystemCapability.Web.Webview.Core
411  * @since 20
412  */
413 int32_t OH_ArkWebHttpBodyStream_SetAsyncReadCallback(ArkWeb_HttpBodyStream *httpBodyStream,
414                                                      ArkWeb_HttpBodyStreamAsyncReadCallback readCallback);
415 
416 /*
417  * @brief Init the http body stream. This function must be called before calling any other functions.
418  * @param httpBodyStream The ArkWeb_HttpBodyStream.
419  * @param initCallback The callback of init.
420  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
421  *
422  * @syscap SystemCapability.Web.Webview.Core
423  * @since 12
424  */
425 int32_t OH_ArkWebHttpBodyStream_Init(ArkWeb_HttpBodyStream* httpBodyStream,
426                                      ArkWeb_HttpBodyStreamInitCallback initCallback);
427 
428 /*
429  * @brief Read the http body to the buffer. The buffer must be larger than the bufLen. We will be reading data from a
430  *        worker thread to the buffer, so should not use the buffer in other threads before the callback to avoid
431  *        concurrency issues.
432  * @param httpBodyStream The ArkWeb_HttpBodyStream.
433  * @param buffer The buffer to receive data.
434  * @param bufLen The size of bytes to read.
435  *
436  * @syscap SystemCapability.Web.Webview.Core
437  * @since 12
438  */
439 void OH_ArkWebHttpBodyStream_Read(const ArkWeb_HttpBodyStream* httpBodyStream, uint8_t* buffer, int bufLen);
440 
441 
442 /**
443  * @brief Read the http body to the buffer.
444  *
445  * The buffer must be larger than the bufLen. We will read data from a worker thread to the buffer,\n
446  * so should not use the buffer in other threads before the callback to avoid concurrency issues.\n
447  *
448  * @param httpBodyStream The ArkWeb_HttpBodyStream.
449  * @param buffer The buffer to receive data.
450  * @param bufLen The size of bytes to read.
451  *
452  * @syscap SystemCapability.Web.Webview.Core
453  * @since 20
454  */
455 void OH_ArkWebHttpBodyStream_AsyncRead(const ArkWeb_HttpBodyStream *httpBodyStream, uint8_t *buffer, int bufLen);
456 
457 /*
458  * @brief Get the total size of the data stream.
459  *        When data is chunked or httpBodyStream is invalid, always return zero.
460  * @param httpBodyStream The ArkWeb_HttpBodyStream.
461  * @return The size of data stream.
462  *
463  * @syscap SystemCapability.Web.Webview.Core
464  * @since 12
465  */
466 uint64_t OH_ArkWebHttpBodyStream_GetSize(const ArkWeb_HttpBodyStream* httpBodyStream);
467 
468 /*
469  * @brief Get the current position of the data stream.
470  * @param httpBodyStream The ArkWeb_HttpBodyStream.
471  * @return The current position of data stream. 0 if httpBodyStream is invalid.
472  *
473  * @syscap SystemCapability.Web.Webview.Core
474  * @since 12
475  */
476 uint64_t OH_ArkWebHttpBodyStream_GetPosition(const ArkWeb_HttpBodyStream* httpBodyStream);
477 
478 /*
479  * @brief Get if the data stream is chunked.
480  * @param httpBodyStream The ArkWeb_HttpBodyStream.
481  * @return True if is chunked; false otherwise.
482  *
483  * @syscap SystemCapability.Web.Webview.Core
484  * @since 12
485  */
486 bool OH_ArkWebHttpBodyStream_IsChunked(const ArkWeb_HttpBodyStream* httpBodyStream);
487 
488 
489 /*
490  * @brief Returns true if all data has been consumed from this upload data stream. For chunked uploads, returns false
491  *        until the first read attempt.
492  * @param httpBodyStream The ArkWeb_HttpBodyStream.
493  * @return True if all data has been consumed; false otherwise.
494  *
495  * @syscap SystemCapability.Web.Webview.Core
496  * @since 12
497  */
498 bool OH_ArkWebHttpBodyStream_IsEof(const ArkWeb_HttpBodyStream* httpBodyStream);
499 
500 /*
501  * @brief Returns true if the upload data in the stream is entirely in memory, and all read requests will succeed
502  *        synchronously. Expected to return false for chunked requests.
503  * @param httpBodyStream The ArkWeb_HttpBodyStream.
504  * @return True if the upload data is in memory; false otherwise.
505  *
506  * @syscap SystemCapability.Web.Webview.Core
507  * @since 12
508  */
509 bool OH_ArkWebHttpBodyStream_IsInMemory(const ArkWeb_HttpBodyStream* httpBodyStream);
510 
511 /*
512  * @brief Destroy the ArkWeb_ResourceRequest.
513  * @param resourceRequest The ArkWeb_ResourceRequest.
514  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
515  *
516  * @syscap SystemCapability.Web.Webview.Core
517  * @since 12
518  */
519 int32_t OH_ArkWebResourceRequest_Destroy(const ArkWeb_ResourceRequest* resourceRequest);
520 
521 /*
522  * @brief Get the referrer of request.
523  * @param resourceRequest The ArkWeb_ResourceRequest.
524  * @param referrer The request's referrer. This function will allocate memory for the referrer string and caller
525  *                 must release the string by OH_ArkWeb_ReleaseString.
526  *
527  * @syscap SystemCapability.Web.Webview.Core
528  * @since 12
529  */
530 void OH_ArkWebResourceRequest_GetReferrer(const ArkWeb_ResourceRequest* resourceRequest, char** referrer);
531 
532 /*
533  * @brief Get the OH_ArkWeb_RequestHeaderList of the request.
534  * @param resourceRequest The ArkWeb_ResourceRequest.
535  * @param requestHeaderList The RequestHeaderList of request.
536  *
537  * @syscap SystemCapability.Web.Webview.Core
538  * @since 12
539  */
540 void OH_ArkWebResourceRequest_GetRequestHeaders(const ArkWeb_ResourceRequest* resourceRequest,
541                                                 ArkWeb_RequestHeaderList** requestHeaderList);
542 
543 /*
544  * @brief Get if this is a redirect request.
545  * @param resourceRequest The ArkWeb_ResourceRequest.
546  * @return True if this is a redirect; false otherwise.
547  *
548  * @syscap SystemCapability.Web.Webview.Core
549  * @since 12
550  */
551 bool OH_ArkWebResourceRequest_IsRedirect(const ArkWeb_ResourceRequest* resourceRequest);
552 
553 /*
554  * @brief Get if this is a request from main frame.
555  * @param resourceRequest The ArkWeb_ResourceRequest.
556  * @return True if this is from main frame; false otherwise.
557  *
558  * @syscap SystemCapability.Web.Webview.Core
559  * @since 12
560  */
561 bool OH_ArkWebResourceRequest_IsMainFrame(const ArkWeb_ResourceRequest* resourceRequest);
562 
563 /*
564  * @brief Get if this is a request is triggered by user gesutre.
565  * @param resourceRequest The ArkWeb_ResourceRequest.
566  * @return True if this is triggered by user gesture; false otherwise.
567  *
568  * @syscap SystemCapability.Web.Webview.Core
569  * @since 12
570  */
571 bool OH_ArkWebResourceRequest_HasGesture(const ArkWeb_ResourceRequest* resourceRequest);
572 
573 /*
574  * @brief Register custom scheme to the ArkWeb. Should not be called for built-in HTTP, HTTPS, FILE, FTP, ABOUT and
575  *        DATA schemes. This function should be called on main thread.
576  * @param scheme The scheme to regist.
577  * @param option The configuration of the scheme.
578  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
579  *
580  * @syscap SystemCapability.Web.Webview.Core
581  * @since 12
582  */
583 int32_t OH_ArkWeb_RegisterCustomSchemes(const char* scheme, int32_t option);
584 
585 /*
586  * @brief Set a ArkWeb_SchemeHandler for a specific scheme to intercept requests of that scheme type.
587  *        SchemeHandler should be set after the BrowserContext created.
588  *        Use WebviewController.initializeWebEngine to initialize the BrowserContext without create a ArkWeb.
589  *
590  * @param scheme Scheme that need to be intercepted.
591  * @param schemeHandler The SchemeHandler for the scheme. Only requests triggered by ServiceWorker will be notified
592  *                      through this handler.
593  * @return Return true if set SchemeHandler for specific scheme successful, return false otherwise.
594  *
595  * @syscap SystemCapability.Web.Webview.Core
596  * @since 12
597  */
598 bool OH_ArkWebServiceWorker_SetSchemeHandler(const char* scheme, ArkWeb_SchemeHandler* schemeHandler);
599 
600 /*
601  * @brief Set a ArkWeb_SchemeHandler for a specific scheme to intercept requests of that scheme type.
602  *        SchemeHandler should be set after the BrowserContext created.
603  *        Use WebviewController.initializeWebEngine to initialize the BrowserContext without create a ArkWeb.
604  *
605  * @param scheme Scheme that need to be intercepted.
606  * @param webTag The name of the web component.
607  * @param schemeHandler The SchemeHandler for the scheme. Only requests triggered from the specified web will be
608  *                      notified through this handler.
609  * @return Return true if set SchemeHandler for specific scheme successful, return false otherwise.
610  *
611  * @syscap SystemCapability.Web.Webview.Core
612  * @since 12
613  */
614 bool OH_ArkWeb_SetSchemeHandler(const char* scheme, const char* webTag, ArkWeb_SchemeHandler* schemeHandler);
615 
616 /*
617  * @brief Clear the handler registered on the specified web for service worker.
618  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
619  *
620  * @syscap SystemCapability.Web.Webview.Core
621  * @since 12
622  */
623 int32_t OH_ArkWebServiceWorker_ClearSchemeHandlers();
624 
625 /*
626  * @brief Clear the handler registered on the specified web.
627  * @param webTag The name of the web component.
628  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
629  *
630  * @syscap SystemCapability.Web.Webview.Core
631  * @since 12
632  */
633 int32_t OH_ArkWeb_ClearSchemeHandlers(const char* webTag);
634 
635 /*
636  * @brief Create a SchemeHandler.
637  * @param schemeHandler Return the created SchemeHandler. Use OH_ArkWeb_DestroySchemeHandler destroy it when donn't
638  *                      need it.
639  *
640  * @syscap SystemCapability.Web.Webview.Core
641  * @since 12
642  */
643 void OH_ArkWeb_CreateSchemeHandler(ArkWeb_SchemeHandler** schemeHandler);
644 
645 /*
646  * @brief Destroy a SchemeHandler.
647  * @param The ArkWeb_SchemeHandler to be destroy.
648  *
649  * @syscap SystemCapability.Web.Webview.Core
650  * @since 12
651  */
652 void OH_ArkWeb_DestroySchemeHandler(ArkWeb_SchemeHandler* schemeHandler);
653 
654 /*
655  * @brief Set a user data to ArkWeb_SchemeHandler.
656  * @param schemeHandler The ArkWeb_SchemeHandler.
657  * @param userData The user data to set.
658  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
659  *
660  * @syscap SystemCapability.Web.Webview.Core
661  * @since 12
662  */
663 int32_t OH_ArkWebSchemeHandler_SetUserData(ArkWeb_SchemeHandler* schemeHandler, void* userData);
664 
665 /*
666  * @brief Get the user data from ArkWeb_SchemeHandler.
667  * @param schemeHandler The ArkWeb_SchemeHandler.
668  * @return The set user data.
669  *
670  * @syscap SystemCapability.Web.Webview.Core
671  * @since 12
672  */
673 void* OH_ArkWebSchemeHandler_GetUserData(const ArkWeb_SchemeHandler* schemeHandler);
674 
675 /*
676  * @brief Set the OnRequestStart callback for SchemeHandler.
677  * @param schemeHandler The SchemeHandler for the scheme.
678  * @param onRequestStart The OnRequestStart callback.
679  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
680  *
681  * @syscap SystemCapability.Web.Webview.Core
682  * @since 12
683  */
684 int32_t OH_ArkWebSchemeHandler_SetOnRequestStart(ArkWeb_SchemeHandler* schemeHandler,
685                                                  ArkWeb_OnRequestStart onRequestStart);
686 
687 /*
688  * @brief Set the OnRequestStop callback for SchemeHandler.
689  * @param schemeHandler The SchemeHandler for the scheme.
690  * @param onRequestStop The OnRequestStop callback.
691  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
692  *
693  * @syscap SystemCapability.Web.Webview.Core
694  * @since 12
695  */
696 int32_t OH_ArkWebSchemeHandler_SetOnRequestStop(ArkWeb_SchemeHandler* schemeHandler,
697                                                 ArkWeb_OnRequestStop onRequestStop);
698 
699 /*
700  * @brief Create a Response for a request.
701  * @param Return the created Response. Use OH_ArkWeb_DestroyResponse to destroy when donn't need it.
702  *
703  * @syscap SystemCapability.Web.Webview.Core
704  * @since 12
705  */
706 void OH_ArkWeb_CreateResponse(ArkWeb_Response** response);
707 
708 /*
709  * @brief Destroy the Reponse.
710  * @param response The Response needs destroy.
711  *
712  * @syscap SystemCapability.Web.Webview.Core
713  * @since 12
714  */
715 void OH_ArkWeb_DestroyResponse(ArkWeb_Response* response);
716 
717 /*
718  * @brief Set the resolved URL after redirects or changed as a result of HSTS.
719  * @param response The ArkWeb_Response.
720  * @param url The resolved URL.
721  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
722  *
723  * @syscap SystemCapability.Web.Webview.Core
724  * @since 12
725  */
726 int32_t OH_ArkWebResponse_SetUrl(ArkWeb_Response* response, const char* url);
727 
728 /*
729  * @brief Get the resolved URL after redirects or changed as a result of HSTS.
730  * @param response The ArkWeb_Response.
731  * @param url The resolved URL.
732  *
733  * @syscap SystemCapability.Web.Webview.Core
734  * @since 12
735  */
736 void OH_ArkWebResponse_GetUrl(const ArkWeb_Response* response, char** url);
737 
738 /*
739  * @brief Set a error code to ArkWeb_Response.
740  * @param response The ArkWeb_Response.
741  * @param errorCode The error code for the failed request.
742  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
743  *
744  * @syscap SystemCapability.Web.Webview.Core
745  * @since 12
746  */
747 int32_t OH_ArkWebResponse_SetError(ArkWeb_Response* response, ArkWeb_NetError errorCode);
748 
749 /*
750  * @brief Get the response's error code.
751  * @param response The ArkWeb_Response.
752  * @return The response's error code.
753  *
754  * @syscap SystemCapability.Web.Webview.Core
755  * @since 12
756  */
757 ArkWeb_NetError OH_ArkWebResponse_GetError(const ArkWeb_Response* response);
758 
759 /*
760  * @brief Set a status code to ArkWebResponse.
761  * @param response The ArkWeb_Response.
762  * @param status The http status code for the request.
763  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
764  *
765  * @syscap SystemCapability.Web.Webview.Core
766  * @since 12
767  */
768 int32_t OH_ArkWebResponse_SetStatus(ArkWeb_Response* response, int status);
769 
770 /*
771  * @brief Get the response's status code.
772  * @param response The ArkWeb_Response.
773  * @return The response's http status code. -1 if response is invalid.
774  *
775  * @syscap SystemCapability.Web.Webview.Core
776  * @since 12
777  */
778 int OH_ArkWebResponse_GetStatus(const ArkWeb_Response* response);
779 
780 /*
781  * @brief Set a status text to ArkWebResponse.
782  * @param response The ArkWeb_Response.
783  * @param statusText The status text for the request.
784  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
785  *
786  * @syscap SystemCapability.Web.Webview.Core
787  * @since 12
788  */
789 int32_t OH_ArkWebResponse_SetStatusText(ArkWeb_Response* response, const char* statusText);
790 
791 /*
792  * @brief Get the response's status text.
793  * @param response The ArkWeb_Response.
794  * @param statusText Return the response's statusText. This function will allocate memory for the statusText string and
795  *                   caller must release the string by OH_ArkWeb_ReleaseString.
796  *
797  * @syscap SystemCapability.Web.Webview.Core
798  * @since 12
799  */
800 void OH_ArkWebResponse_GetStatusText(const ArkWeb_Response* response, char** statusText);
801 
802 /*
803  * @brief Set mime type to ArkWebResponse.
804  * @param response The ArkWeb_Response.
805  * @param mimeType The mime type for the request.
806  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
807  *
808  * @syscap SystemCapability.Web.Webview.Core
809  * @since 12
810  */
811 int32_t OH_ArkWebResponse_SetMimeType(ArkWeb_Response* response, const char* mimeType);
812 
813 /*
814  * @brief Get the response's mime type.
815  * @param response The ArkWeb_Response.
816  * @param mimeType Return the response's mime type. This function will allocate memory for the mime type string and
817  *                 caller must release the string by OH_ArkWeb_ReleaseString.
818  *
819  * @syscap SystemCapability.Web.Webview.Core
820  * @since 12
821  */
822 void OH_ArkWebResponse_GetMimeType(const ArkWeb_Response* response, char** mimeType);
823 
824 /*
825  * @brief Set charset to ArkWeb_Response.
826  * @param response The ArkWeb_Response.
827  * @param charset The charset for the request.
828  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
829  *
830  * @syscap SystemCapability.Web.Webview.Core
831  * @since 12
832  */
833 int32_t OH_ArkWebResponse_SetCharset(ArkWeb_Response* response, const char* charset);
834 
835 /*
836  * @brief Get the response's charset.
837  * @param response The ArkWeb_Response.
838  * @param charset Return the response's charset. This function will allocate memory for the charset string and caller
839  *                must release the string by OH_ArkWeb_ReleaseString.
840  *
841  * @syscap SystemCapability.Web.Webview.Core
842  * @since 12
843  */
844 void OH_ArkWebResponse_GetCharset(const ArkWeb_Response* response, char** charset);
845 
846 /*
847  * @brief Set a header to ArkWeb_Response.
848  * @param response The ArkWeb_Response.
849  * @param name The name of the header.
850  * @param value The value of the header.
851  * @bool overwirte If true will overwrite the exsits header, if false otherwise.
852  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
853  *
854  * @syscap SystemCapability.Web.Webview.Core
855  * @since 12
856  */
857 int32_t OH_ArkWebResponse_SetHeaderByName(ArkWeb_Response* response,
858                                           const char* name,
859                                           const char* value,
860                                           bool overwrite);
861 
862 /*
863  * @brief Get the header from the response.
864  * @param response The ArkWeb_Response.
865  * @param name The name of the header.
866  * @param value Return the header's value. This function will allocate memory for the value string and caller must
867  *              release the string by OH_ArkWeb_ReleaseString.
868  *
869  * @syscap SystemCapability.Web.Webview.Core
870  * @since 12
871  */
872 void OH_ArkWebResponse_GetHeaderByName(const ArkWeb_Response* response, const char* name, char** value);
873 
874 /*
875  * @brief Destroy the ArkWeb_ResourceHandler.
876  * @param resourceHandler The ArkWeb_ResourceHandler.
877  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
878  *
879  * @syscap SystemCapability.Web.Webview.Core
880  * @since 12
881  */
882 int32_t OH_ArkWebResourceHandler_Destroy(const ArkWeb_ResourceHandler* resourceHandler);
883 
884 /*
885  * @brief Pass response headers to intercepted requests.
886  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
887  * @param response The ArkWeb_Response for the intercepting requests.
888  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
889  *
890  * @syscap SystemCapability.Web.Webview.Core
891  * @since 12
892  */
893 int32_t OH_ArkWebResourceHandler_DidReceiveResponse(const ArkWeb_ResourceHandler* resourceHandler,
894                                                     const ArkWeb_Response* response);
895 
896 /*
897  * @brief Pass response body data to intercepted requests.
898  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
899  * @param buffer Buffer data to send.
900  * @param bufLen The size of buffer.
901  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
902  *
903  * @syscap SystemCapability.Web.Webview.Core
904  * @since 12
905  */
906 int32_t OH_ArkWebResourceHandler_DidReceiveData(const ArkWeb_ResourceHandler* resourceHandler,
907                                                 const uint8_t* buffer,
908                                                 int64_t bufLen);
909 
910 /*
911  * @brief Notify the ArkWeb that this request should be finished and there is no more data available.
912  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
913  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
914  *
915  * @syscap SystemCapability.Web.Webview.Core
916  * @since 12
917  */
918 int32_t OH_ArkWebResourceHandler_DidFinish(const ArkWeb_ResourceHandler* resourceHandler);
919 
920 /*
921  * @brief Notify the ArkWeb that this request should be failed.
922  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
923  * @param errorCode The error code for this request. refer to arkweb_net_error_list.h
924  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
925  *
926  * @syscap SystemCapability.Web.Webview.Core
927  * @since 12
928  */
929 int32_t OH_ArkWebResourceHandler_DidFailWithError(const ArkWeb_ResourceHandler* resourceHandler,
930                                                   ArkWeb_NetError errorCode);
931 
932 /*
933  * @brief Notify the ArkWeb that this request should be failed.
934  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
935  * @param errorCode The error code for this request. Refer to arkweb_net_error_list.h.
936  * @param completeIfNoResponse If true, will construct a response when haven't received a response.
937  * @return {@link ARKWEB_NET_OK} 0 - Success.
938  *         {@link ARKWEB_INVALID_PARAM} 17100101 - Invalid param.
939  *
940  * @syscap SystemCapability.Web.Webview.Core
941  * @since 20
942  */
943 int32_t OH_ArkWebResourceHandler_DidFailWithErrorV2(const ArkWeb_ResourceHandler* resourceHandler,
944                                                     ArkWeb_NetError errorCode,
945                                                     bool completeIfNoResponse);
946 
947 /*
948  * @brief Release the string acquired by native function.
949  * @param string The string to be released.
950  *
951  * @syscap SystemCapability.Web.Webview.Core
952  * @since 12
953  */
954 void OH_ArkWeb_ReleaseString(char* string);
955 
956 /*
957  * @brief Release the byte array acquired by native function.
958  * @param byteArray The byte array to be released.
959  *
960  * @syscap SystemCapability.Web.Webview.Core
961  * @since 12
962  */
963 void OH_ArkWeb_ReleaseByteArray(uint8_t* byteArray);
964 
965 int32_t OH_ArkWebSchemeHandler_SetFromEts(ArkWeb_SchemeHandler* schemeHandler,
966                                           bool fromEts);
967 
968 void DestroySchemeHandlerApiForTest();
969 void SetSchemeHandlerApiForTest(void* schemeHandlerApi);
970 
971 #ifdef __cplusplus
972 };
973 #endif
974 #endif // ARKWEB_SCHEME_HANDLER_H
975