1 /*
2  * Copyright (c) 2024 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 init operation done.
221  * @param httpBodyStream The ArkWeb_HttpBodyStream.
222  * @param result ARKWEB_NET_OK on success otherwise refer to ARKWEB_NET_ERROR.
223  *
224  * @syscap SystemCapability.Web.Webview.Core
225  * @since 12
226  */
227 typedef void (*ArkWeb_HttpBodyStreamInitCallback)(const ArkWeb_HttpBodyStream* httpBodyStream, ArkWeb_NetError result);
228 
229 /*
230  * @brief Destroy the ArkWeb_RequestHeaderList.
231  * @param requestHeaderList The ArkWeb_RequestHeaderList to be destroyed.
232  *
233  * @syscap SystemCapability.Web.Webview.Core
234  * @since 12
235  */
236 void OH_ArkWebRequestHeaderList_Destroy(ArkWeb_RequestHeaderList* requestHeaderList);
237 
238 /*
239  * @brief Get the request headers size.
240  * @param requestHeaderList The list of request header.
241  * @return The size of request headers. -1 if requestHeaderList is invalid.
242  *
243  * @syscap SystemCapability.Web.Webview.Core
244  * @since 12
245  */
246 int32_t OH_ArkWebRequestHeaderList_GetSize(const ArkWeb_RequestHeaderList* requestHeaderList);
247 
248 /*
249  * @brief Get the specified request header.
250  * @param requestHeaderList The list of request header.
251  * @param index The index of request header.
252  * @param key The header key. Caller must release the string by OH_ArkWeb_ReleaseString.
253  * @param value The header value. Caller must release the string by OH_ArkWeb_ReleaseString.
254  *
255  * @syscap SystemCapability.Web.Webview.Core
256  * @since 12
257  */
258 void OH_ArkWebRequestHeaderList_GetHeader(const ArkWeb_RequestHeaderList* requestHeaderList,
259                                           int32_t index,
260                                           char** key,
261                                           char** value);
262 
263 /*
264  * @brief Set a user data to ArkWeb_ResourceRequest.
265  * @param resourceRequest The ArkWeb_ResourceRequest.
266  * @param userData The user data to set.
267  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
268  *
269  * @syscap SystemCapability.Web.Webview.Core
270  * @since 12
271  */
272 int32_t OH_ArkWebResourceRequest_SetUserData(ArkWeb_ResourceRequest* resourceRequest, void* userData);
273 
274 /*
275  * @brief Get the user data from ArkWeb_ResourceRequest.
276  * @param resourceRequest The ArkWeb_ResourceRequest.
277  * @return The set user data.
278  *
279  * @syscap SystemCapability.Web.Webview.Core
280  * @since 12
281  */
282 void* OH_ArkWebResourceRequest_GetUserData(const ArkWeb_ResourceRequest* resourceRequest);
283 
284 /*
285  * @brief Get the method of request.
286  * @param resourceRequest The ArkWeb_ResourceRequest.
287  * @param method The request's http method. This function will allocate memory for the method string and caller must
288  *               release the string by OH_ArkWeb_ReleaseString.
289  *
290  * @syscap SystemCapability.Web.Webview.Core
291  * @since 12
292  */
293 void OH_ArkWebResourceRequest_GetMethod(const ArkWeb_ResourceRequest* resourceRequest, char** method);
294 
295 /*
296  * @brief Get the url of request.
297  * @param resourceRequest The ArkWeb_ResourceRequest.
298  * @param url The request's url. This function will allocate memory for the url string and caller must release the
299  *            string by OH_ArkWeb_ReleaseString.
300  *
301  * @syscap SystemCapability.Web.Webview.Core
302  * @since 12
303  */
304 void OH_ArkWebResourceRequest_GetUrl(const ArkWeb_ResourceRequest* resourceRequest, char** url);
305 
306 /*
307  * @brief Create a ArkWeb_HttpBodyStream which used to read the http body.
308  * @param resourceRequest The ArkWeb_ResourceRequest.
309  * @param httpBodyStream The request's http body. This function will allocate memory for the http body stream and
310  *                       caller must release the httpBodyStream by OH_ArkWebResourceRequest_DestroyHttpBodyStream.
311  *
312  * @syscap SystemCapability.Web.Webview.Core
313  * @since 12
314  */
315 void OH_ArkWebResourceRequest_GetHttpBodyStream(const ArkWeb_ResourceRequest* resourceRequest,
316                                                 ArkWeb_HttpBodyStream** httpBodyStream);
317 
318 /*
319  * @brief Destroy the http body stream.
320  * @param httpBodyStream The httpBodyStream to be destroyed.
321  *
322  * @syscap SystemCapability.Web.Webview.Core
323  * @since 12
324  */
325 void OH_ArkWebResourceRequest_DestroyHttpBodyStream(ArkWeb_HttpBodyStream* httpBodyStream);
326 
327 /*
328  * @brief Get the resource type of request.
329  * @param resourceRequest The ArkWeb_ResourceRequest.
330  * @return The resource type of request. -1 if resourceRequest is invalid.
331  *
332  * @syscap SystemCapability.Web.Webview.Core
333  * @since 12
334  */
335 int32_t OH_ArkWebResourceRequest_GetResourceType(const ArkWeb_ResourceRequest* resourceRequest);
336 
337 /*
338  * @brief Get the url of frame which trigger this request.
339  * @param resourceRequest The ArkWeb_ResourceRequest.
340  * @param frameUrl The url of frame which trigger this request. This function will allocate memory for the url string
341  *            and caller must release the string by OH_ArkWeb_ReleaseString.
342  *
343  * @syscap SystemCapability.Web.Webview.Core
344  * @since 12
345  */
346 void OH_ArkWebResourceRequest_GetFrameUrl(const ArkWeb_ResourceRequest* resourceRequest, char** frameUrl);
347 
348 /*
349  * @brief Set a user data to ArkWeb_HttpBodyStream.
350  * @param httpBodyStream The ArkWeb_HttpBodyStream.
351  * @param userData The user data to set.
352  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
353  *
354  * @syscap SystemCapability.Web.Webview.Core
355  * @since 12
356  */
357 int32_t OH_ArkWebHttpBodyStream_SetUserData(ArkWeb_HttpBodyStream* httpBodyStream, void* userData);
358 
359 /*
360  * @brief Get the user data from ArkWeb_HttpBodyStream.
361  * @param httpBodyStream The ArkWeb_HttpBodyStream.
362  * @return The set user data.
363  *
364  * @syscap SystemCapability.Web.Webview.Core
365  * @since 12
366  */
367 void* OH_ArkWebHttpBodyStream_GetUserData(const ArkWeb_HttpBodyStream* httpBodyStream);
368 
369 /*
370  * @brief Set the callback for OH_ArkWebHttpBodyStream_Read, the result of OH_ArkWebHttpBodyStream_Read will be
371  *        notified to caller through the readCallback. The callback will runs in the same thread as
372  *        OH_ArkWebHttpBodyStream_Read.
373  * @param httpBodyStream The ArkWeb_HttpBodyStream.
374  * @param readCallback The callback of read function.
375  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
376  *
377  * @syscap SystemCapability.Web.Webview.Core
378  * @since 12
379  */
380 int32_t OH_ArkWebHttpBodyStream_SetReadCallback(ArkWeb_HttpBodyStream* httpBodyStream,
381                                                 ArkWeb_HttpBodyStreamReadCallback readCallback);
382 
383 /*
384  * @brief Init the http body stream. This function must be called before calling any other functions.
385  * @param httpBodyStream The ArkWeb_HttpBodyStream.
386  * @param initCallback The callback of init.
387  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
388  *
389  * @syscap SystemCapability.Web.Webview.Core
390  * @since 12
391  */
392 int32_t OH_ArkWebHttpBodyStream_Init(ArkWeb_HttpBodyStream* httpBodyStream,
393                                      ArkWeb_HttpBodyStreamInitCallback initCallback);
394 
395 /*
396  * @brief Read the http body to the buffer. The buffer must be larger than the bufLen. We will be reading data from a
397  *        worker thread to the buffer, so should not use the buffer in other threads before the callback to avoid
398  *        concurrency issues.
399  * @param httpBodyStream The ArkWeb_HttpBodyStream.
400  * @param buffer The buffer to receive data.
401  * @param bufLen The size of bytes to read.
402  *
403  * @syscap SystemCapability.Web.Webview.Core
404  * @since 12
405  */
406 void OH_ArkWebHttpBodyStream_Read(const ArkWeb_HttpBodyStream* httpBodyStream, uint8_t* buffer, int bufLen);
407 
408 /*
409  * @brief Get the total size of the data stream.
410  *        When data is chunked or httpBodyStream is invalid, always return zero.
411  * @param httpBodyStream The ArkWeb_HttpBodyStream.
412  * @return The size of data stream.
413  *
414  * @syscap SystemCapability.Web.Webview.Core
415  * @since 12
416  */
417 uint64_t OH_ArkWebHttpBodyStream_GetSize(const ArkWeb_HttpBodyStream* httpBodyStream);
418 
419 /*
420  * @brief Get the current position of the data stream.
421  * @param httpBodyStream The ArkWeb_HttpBodyStream.
422  * @return The current position of data stream. 0 if httpBodyStream is invalid.
423  *
424  * @syscap SystemCapability.Web.Webview.Core
425  * @since 12
426  */
427 uint64_t OH_ArkWebHttpBodyStream_GetPosition(const ArkWeb_HttpBodyStream* httpBodyStream);
428 
429 /*
430  * @brief Get if the data stream is chunked.
431  * @param httpBodyStream The ArkWeb_HttpBodyStream.
432  * @return True if is chunked; false otherwise.
433  *
434  * @syscap SystemCapability.Web.Webview.Core
435  * @since 12
436  */
437 bool OH_ArkWebHttpBodyStream_IsChunked(const ArkWeb_HttpBodyStream* httpBodyStream);
438 
439 
440 /*
441  * @brief Returns true if all data has been consumed from this upload data stream. For chunked uploads, returns false
442  *        until the first read attempt.
443  * @param httpBodyStream The ArkWeb_HttpBodyStream.
444  * @return True if all data has been consumed; false otherwise.
445  *
446  * @syscap SystemCapability.Web.Webview.Core
447  * @since 12
448  */
449 bool OH_ArkWebHttpBodyStream_IsEof(const ArkWeb_HttpBodyStream* httpBodyStream);
450 
451 /*
452  * @brief Returns true if the upload data in the stream is entirely in memory, and all read requests will succeed
453  *        synchronously. Expected to return false for chunked requests.
454  * @param httpBodyStream The ArkWeb_HttpBodyStream.
455  * @return True if the upload data is in memory; false otherwise.
456  *
457  * @syscap SystemCapability.Web.Webview.Core
458  * @since 12
459  */
460 bool OH_ArkWebHttpBodyStream_IsInMemory(const ArkWeb_HttpBodyStream* httpBodyStream);
461 
462 /*
463  * @brief Destroy the ArkWeb_ResourceRequest.
464  * @param resourceRequest The ArkWeb_ResourceRequest.
465  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
466  *
467  * @syscap SystemCapability.Web.Webview.Core
468  * @since 12
469  */
470 int32_t OH_ArkWebResourceRequest_Destroy(const ArkWeb_ResourceRequest* resourceRequest);
471 
472 /*
473  * @brief Get the referrer of request.
474  * @param resourceRequest The ArkWeb_ResourceRequest.
475  * @param referrer The request's referrer. This function will allocate memory for the referrer string and caller
476  *                 must release the string by OH_ArkWeb_ReleaseString.
477  *
478  * @syscap SystemCapability.Web.Webview.Core
479  * @since 12
480  */
481 void OH_ArkWebResourceRequest_GetReferrer(const ArkWeb_ResourceRequest* resourceRequest, char** referrer);
482 
483 /*
484  * @brief Get the OH_ArkWeb_RequestHeaderList of the request.
485  * @param resourceRequest The ArkWeb_ResourceRequest.
486  * @param requestHeaderList The RequestHeaderList of request.
487  *
488  * @syscap SystemCapability.Web.Webview.Core
489  * @since 12
490  */
491 void OH_ArkWebResourceRequest_GetRequestHeaders(const ArkWeb_ResourceRequest* resourceRequest,
492                                                 ArkWeb_RequestHeaderList** requestHeaderList);
493 
494 /*
495  * @brief Get if this is a redirect request.
496  * @param resourceRequest The ArkWeb_ResourceRequest.
497  * @return True if this is a redirect; false otherwise.
498  *
499  * @syscap SystemCapability.Web.Webview.Core
500  * @since 12
501  */
502 bool OH_ArkWebResourceRequest_IsRedirect(const ArkWeb_ResourceRequest* resourceRequest);
503 
504 /*
505  * @brief Get if this is a request from main frame.
506  * @param resourceRequest The ArkWeb_ResourceRequest.
507  * @return True if this is from main frame; false otherwise.
508  *
509  * @syscap SystemCapability.Web.Webview.Core
510  * @since 12
511  */
512 bool OH_ArkWebResourceRequest_IsMainFrame(const ArkWeb_ResourceRequest* resourceRequest);
513 
514 /*
515  * @brief Get if this is a request is triggered by user gesutre.
516  * @param resourceRequest The ArkWeb_ResourceRequest.
517  * @return True if this is triggered by user gesture; false otherwise.
518  *
519  * @syscap SystemCapability.Web.Webview.Core
520  * @since 12
521  */
522 bool OH_ArkWebResourceRequest_HasGesture(const ArkWeb_ResourceRequest* resourceRequest);
523 
524 /*
525  * @brief Register custom scheme to the ArkWeb. Should not be called for built-in HTTP, HTTPS, FILE, FTP, ABOUT and
526  *        DATA schemes. This function should be called on main thread.
527  * @param scheme The scheme to regist.
528  * @param option The configuration of the scheme.
529  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
530  *
531  * @syscap SystemCapability.Web.Webview.Core
532  * @since 12
533  */
534 int32_t OH_ArkWeb_RegisterCustomSchemes(const char* scheme, int32_t option);
535 
536 /*
537  * @brief Set a ArkWeb_SchemeHandler for a specific scheme to intercept requests of that scheme type.
538  *        SchemeHandler should be set after the BrowserContext created.
539  *        Use WebviewController.initializeWebEngine to initialize the BrowserContext without create a ArkWeb.
540  *
541  * @param scheme Scheme that need to be intercepted.
542  * @param schemeHandler The SchemeHandler for the scheme. Only requests triggered by ServiceWorker will be notified
543  *                      through this handler.
544  * @return Return true if set SchemeHandler for specific scheme successful, return false otherwise.
545  *
546  * @syscap SystemCapability.Web.Webview.Core
547  * @since 12
548  */
549 bool OH_ArkWebServiceWorker_SetSchemeHandler(const char* scheme, ArkWeb_SchemeHandler* schemeHandler);
550 
551 /*
552  * @brief Set a ArkWeb_SchemeHandler for a specific scheme to intercept requests of that scheme type.
553  *        SchemeHandler should be set after the BrowserContext created.
554  *        Use WebviewController.initializeWebEngine to initialize the BrowserContext without create a ArkWeb.
555  *
556  * @param scheme Scheme that need to be intercepted.
557  * @param webTag The name of the web component.
558  * @param schemeHandler The SchemeHandler for the scheme. Only requests triggered from the specified web will be
559  *                      notified through this handler.
560  * @return Return true if set SchemeHandler for specific scheme successful, return false otherwise.
561  *
562  * @syscap SystemCapability.Web.Webview.Core
563  * @since 12
564  */
565 bool OH_ArkWeb_SetSchemeHandler(const char* scheme, const char* webTag, ArkWeb_SchemeHandler* schemeHandler);
566 
567 /*
568  * @brief Clear the handler registered on the specified web for service worker.
569  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
570  *
571  * @syscap SystemCapability.Web.Webview.Core
572  * @since 12
573  */
574 int32_t OH_ArkWebServiceWorker_ClearSchemeHandlers();
575 
576 /*
577  * @brief Clear the handler registered on the specified web.
578  * @param webTag The name of the web component.
579  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
580  *
581  * @syscap SystemCapability.Web.Webview.Core
582  * @since 12
583  */
584 int32_t OH_ArkWeb_ClearSchemeHandlers(const char* webTag);
585 
586 /*
587  * @brief Create a SchemeHandler.
588  * @param schemeHandler Return the created SchemeHandler. Use OH_ArkWeb_DestroySchemeHandler destroy it when donn't
589  *                      need it.
590  *
591  * @syscap SystemCapability.Web.Webview.Core
592  * @since 12
593  */
594 void OH_ArkWeb_CreateSchemeHandler(ArkWeb_SchemeHandler** schemeHandler);
595 
596 /*
597  * @brief Destroy a SchemeHandler.
598  * @param The ArkWeb_SchemeHandler to be destroy.
599  *
600  * @syscap SystemCapability.Web.Webview.Core
601  * @since 12
602  */
603 void OH_ArkWeb_DestroySchemeHandler(ArkWeb_SchemeHandler* schemeHandler);
604 
605 /*
606  * @brief Set a user data to ArkWeb_SchemeHandler.
607  * @param schemeHandler The ArkWeb_SchemeHandler.
608  * @param userData The user data to set.
609  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
610  *
611  * @syscap SystemCapability.Web.Webview.Core
612  * @since 12
613  */
614 int32_t OH_ArkWebSchemeHandler_SetUserData(ArkWeb_SchemeHandler* schemeHandler, void* userData);
615 
616 /*
617  * @brief Get the user data from ArkWeb_SchemeHandler.
618  * @param schemeHandler The ArkWeb_SchemeHandler.
619  * @return The set user data.
620  *
621  * @syscap SystemCapability.Web.Webview.Core
622  * @since 12
623  */
624 void* OH_ArkWebSchemeHandler_GetUserData(const ArkWeb_SchemeHandler* schemeHandler);
625 
626 /*
627  * @brief Set the OnRequestStart callback for SchemeHandler.
628  * @param schemeHandler The SchemeHandler for the scheme.
629  * @param onRequestStart The OnRequestStart callback.
630  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
631  *
632  * @syscap SystemCapability.Web.Webview.Core
633  * @since 12
634  */
635 int32_t OH_ArkWebSchemeHandler_SetOnRequestStart(ArkWeb_SchemeHandler* schemeHandler,
636                                                  ArkWeb_OnRequestStart onRequestStart);
637 
638 /*
639  * @brief Set the OnRequestStop callback for SchemeHandler.
640  * @param schemeHandler The SchemeHandler for the scheme.
641  * @param onRequestStop The OnRequestStop callback.
642  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
643  *
644  * @syscap SystemCapability.Web.Webview.Core
645  * @since 12
646  */
647 int32_t OH_ArkWebSchemeHandler_SetOnRequestStop(ArkWeb_SchemeHandler* schemeHandler,
648                                                 ArkWeb_OnRequestStop onRequestStop);
649 
650 /*
651  * @brief Create a Response for a request.
652  * @param Return the created Response. Use OH_ArkWeb_DestroyResponse to destroy when donn't need it.
653  *
654  * @syscap SystemCapability.Web.Webview.Core
655  * @since 12
656  */
657 void OH_ArkWeb_CreateResponse(ArkWeb_Response** response);
658 
659 /*
660  * @brief Destroy the Reponse.
661  * @param response The Response needs destroy.
662  *
663  * @syscap SystemCapability.Web.Webview.Core
664  * @since 12
665  */
666 void OH_ArkWeb_DestroyResponse(ArkWeb_Response* response);
667 
668 /*
669  * @brief Set the resolved URL after redirects or changed as a result of HSTS.
670  * @param response The ArkWeb_Response.
671  * @param url The resolved URL.
672  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
673  *
674  * @syscap SystemCapability.Web.Webview.Core
675  * @since 12
676  */
677 int32_t OH_ArkWebResponse_SetUrl(ArkWeb_Response* response, const char* url);
678 
679 /*
680  * @brief Get the resolved URL after redirects or changed as a result of HSTS.
681  * @param response The ArkWeb_Response.
682  * @param url The resolved URL.
683  *
684  * @syscap SystemCapability.Web.Webview.Core
685  * @since 12
686  */
687 void OH_ArkWebResponse_GetUrl(const ArkWeb_Response* response, char** url);
688 
689 /*
690  * @brief Set a error code to ArkWeb_Response.
691  * @param response The ArkWeb_Response.
692  * @param errorCode The error code for the failed request.
693  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
694  *
695  * @syscap SystemCapability.Web.Webview.Core
696  * @since 12
697  */
698 int32_t OH_ArkWebResponse_SetError(ArkWeb_Response* response, ArkWeb_NetError errorCode);
699 
700 /*
701  * @brief Get the response's error code.
702  * @param response The ArkWeb_Response.
703  * @return The response's error code.
704  *
705  * @syscap SystemCapability.Web.Webview.Core
706  * @since 12
707  */
708 ArkWeb_NetError OH_ArkWebResponse_GetError(const ArkWeb_Response* response);
709 
710 /*
711  * @brief Set a status code to ArkWebResponse.
712  * @param response The ArkWeb_Response.
713  * @param status The http status code for the request.
714  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
715  *
716  * @syscap SystemCapability.Web.Webview.Core
717  * @since 12
718  */
719 int32_t OH_ArkWebResponse_SetStatus(ArkWeb_Response* response, int status);
720 
721 /*
722  * @brief Get the response's status code.
723  * @param response The ArkWeb_Response.
724  * @return The response's http status code. -1 if response is invalid.
725  *
726  * @syscap SystemCapability.Web.Webview.Core
727  * @since 12
728  */
729 int OH_ArkWebResponse_GetStatus(const ArkWeb_Response* response);
730 
731 /*
732  * @brief Set a status text to ArkWebResponse.
733  * @param response The ArkWeb_Response.
734  * @param statusText The status text for the request.
735  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
736  *
737  * @syscap SystemCapability.Web.Webview.Core
738  * @since 12
739  */
740 int32_t OH_ArkWebResponse_SetStatusText(ArkWeb_Response* response, const char* statusText);
741 
742 /*
743  * @brief Get the response's status text.
744  * @param response The ArkWeb_Response.
745  * @param statusText Return the response's statusText. This function will allocate memory for the statusText string and
746  *                   caller must release the string by OH_ArkWeb_ReleaseString.
747  *
748  * @syscap SystemCapability.Web.Webview.Core
749  * @since 12
750  */
751 void OH_ArkWebResponse_GetStatusText(const ArkWeb_Response* response, char** statusText);
752 
753 /*
754  * @brief Set mime type to ArkWebResponse.
755  * @param response The ArkWeb_Response.
756  * @param mimeType The mime type for the request.
757  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
758  *
759  * @syscap SystemCapability.Web.Webview.Core
760  * @since 12
761  */
762 int32_t OH_ArkWebResponse_SetMimeType(ArkWeb_Response* response, const char* mimeType);
763 
764 /*
765  * @brief Get the response's mime type.
766  * @param response The ArkWeb_Response.
767  * @param mimeType Return the response's mime type. This function will allocate memory for the mime type string and
768  *                 caller must release the string by OH_ArkWeb_ReleaseString.
769  *
770  * @syscap SystemCapability.Web.Webview.Core
771  * @since 12
772  */
773 void OH_ArkWebResponse_GetMimeType(const ArkWeb_Response* response, char** mimeType);
774 
775 /*
776  * @brief Set charset to ArkWeb_Response.
777  * @param response The ArkWeb_Response.
778  * @param charset The charset for the request.
779  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
780  *
781  * @syscap SystemCapability.Web.Webview.Core
782  * @since 12
783  */
784 int32_t OH_ArkWebResponse_SetCharset(ArkWeb_Response* response, const char* charset);
785 
786 /*
787  * @brief Get the response's charset.
788  * @param response The ArkWeb_Response.
789  * @param charset Return the response's charset. This function will allocate memory for the charset string and caller
790  *                must release the string by OH_ArkWeb_ReleaseString.
791  *
792  * @syscap SystemCapability.Web.Webview.Core
793  * @since 12
794  */
795 void OH_ArkWebResponse_GetCharset(const ArkWeb_Response* response, char** charset);
796 
797 /*
798  * @brief Set a header to ArkWeb_Response.
799  * @param response The ArkWeb_Response.
800  * @param name The name of the header.
801  * @param value The value of the header.
802  * @bool overwirte If true will overwrite the exsits header, if false otherwise.
803  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
804  *
805  * @syscap SystemCapability.Web.Webview.Core
806  * @since 12
807  */
808 int32_t OH_ArkWebResponse_SetHeaderByName(ArkWeb_Response* response,
809                                           const char* name,
810                                           const char* value,
811                                           bool overwrite);
812 
813 /*
814  * @brief Get the header from the response.
815  * @param response The ArkWeb_Response.
816  * @param name The name of the header.
817  * @param value Return the header's value. This function will allocate memory for the value string and caller must
818  *              release the string by OH_ArkWeb_ReleaseString.
819  *
820  * @syscap SystemCapability.Web.Webview.Core
821  * @since 12
822  */
823 void OH_ArkWebResponse_GetHeaderByName(const ArkWeb_Response* response, const char* name, char** value);
824 
825 /*
826  * @brief Destroy the ArkWeb_ResourceHandler.
827  * @param resourceHandler The ArkWeb_ResourceHandler.
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_ArkWebResourceHandler_Destroy(const ArkWeb_ResourceHandler* resourceHandler);
834 
835 /*
836  * @brief Pass response headers to intercepted requests.
837  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
838  * @param response The ArkWeb_Response for the intercepting requests.
839  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
840  *
841  * @syscap SystemCapability.Web.Webview.Core
842  * @since 12
843  */
844 int32_t OH_ArkWebResourceHandler_DidReceiveResponse(const ArkWeb_ResourceHandler* resourceHandler,
845                                                     const ArkWeb_Response* response);
846 
847 /*
848  * @brief Pass response body data to intercepted requests.
849  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
850  * @param buffer Buffer data to send.
851  * @param bufLen The size of buffer.
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_ArkWebResourceHandler_DidReceiveData(const ArkWeb_ResourceHandler* resourceHandler,
858                                                 const uint8_t* buffer,
859                                                 int64_t bufLen);
860 
861 /*
862  * @brief Notify the ArkWeb that this request should be finished and there is no more data available.
863  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
864  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
865  *
866  * @syscap SystemCapability.Web.Webview.Core
867  * @since 12
868  */
869 int32_t OH_ArkWebResourceHandler_DidFinish(const ArkWeb_ResourceHandler* resourceHandler);
870 
871 /*
872  * @brief Notify the ArkWeb that this request should be failed.
873  * @param resourceHandler The ArkWeb_ResourceHandler for the request.
874  * @param errorCode The error code for this request. refer to arkweb_net_error_list.h
875  * @return 0 if success; otherwise if fail. refer to arkweb_error_code.h.
876  *
877  * @syscap SystemCapability.Web.Webview.Core
878  * @since 12
879  */
880 int32_t OH_ArkWebResourceHandler_DidFailWithError(const ArkWeb_ResourceHandler* resourceHandler,
881                                                   ArkWeb_NetError errorCode);
882 
883 /*
884  * @brief Release the string acquired by native function.
885  * @param string The string to be released.
886  *
887  * @syscap SystemCapability.Web.Webview.Core
888  * @since 12
889  */
890 void OH_ArkWeb_ReleaseString(char* string);
891 
892 /*
893  * @brief Release the byte array acquired by native function.
894  * @param byteArray The byte array to be released.
895  *
896  * @syscap SystemCapability.Web.Webview.Core
897  * @since 12
898  */
899 void OH_ArkWeb_ReleaseByteArray(uint8_t* byteArray);
900 
901 int32_t OH_ArkWebSchemeHandler_SetFromEts(ArkWeb_SchemeHandler* schemeHandler,
902                                           bool fromEts);
903 
904 #ifdef __cplusplus
905 };
906 #endif
907 #endif // ARKWEB_SCHEME_HANDLER_H
908