Implement more request interceptor APIs. (#3466)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / WebView / WebHttpRequestInterceptor.cs
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Runtime.InteropServices;
22
23 namespace Tizen.NUI
24 {
25     /// <summary>
26     /// It is a class for http request interceptor of web view.
27     /// </summary>
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class WebHttpRequestInterceptor : Disposable
30     {
31         private HandleRef interceptorHandle;
32
33         internal WebHttpRequestInterceptor(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
34         {
35             IntPtr ip = Interop.WebHttpRequestInterceptorPtr.Get(SwigCPtr);
36             interceptorHandle = new HandleRef(this, ip);
37         }
38
39         /// This will not be public opened.
40         /// <param name="swigCPtr"></param>
41         [EditorBrowsable(EditorBrowsableState.Never)]
42         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
43         {
44             Interop.WebHttpRequestInterceptorPtr.DeleteWebHttpRequestInterceptorPtr(swigCPtr);
45         }
46
47         /// <summary>
48         /// Gets url of intercepted request.
49         /// </summary>
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public string Url
52         {
53             get
54             {
55                 return Interop.WebHttpRequestInterceptor.GetUrl(interceptorHandle);
56             }
57         }
58
59         /// <summary>
60         /// Gets method of intercepted http request, for example, GET, POST, etc.
61         /// </summary>
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         public string Method
64         {
65             get
66             {
67                 return Interop.WebHttpRequestInterceptor.GetMethod(interceptorHandle);
68             }
69         }
70
71         /// <summary>
72         /// Gets headers of intercepted http request.
73         /// Headers is a map with string key-value pairs,
74         /// for example, "Accept: text/plain", "Accept-Charset: utf-8", etc.
75         /// </summary>
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public IDictionary<string, string> Headers
78         {
79             get
80             {
81                 IntPtr mapPtr = Interop.WebHttpRequestInterceptor.GetHeaders(interceptorHandle);
82                 IDictionary<string, string> dictionary = new Dictionary<string, string>();
83                 PropertyMap map = new PropertyMap(mapPtr, true);
84                 for (uint i = 0; i < map.Count(); i++)
85                 {
86                     using (PropertyKey key = map.GetKeyAt(i))
87                     {
88                         if (key.Type == PropertyKey.KeyType.String)
89                         {
90                             string outValue;
91                             using (PropertyValue mapValue = map.GetValue(i))
92                             {
93                                 if (mapValue.Get(out outValue))
94                                 {
95                                     dictionary.Add(key.StringKey, outValue);
96                                 }
97                             }
98                         }
99                     }
100                 }
101                 map.Dispose();
102                 return dictionary;
103             }
104         }
105
106         /// <summary>
107         /// Ignores the http request.
108         /// When application doesn't have a response for intercepted request,
109         /// this function would be called which notifies engine to proceed with normal resource loading.
110         /// It can be called only INSIDE WebContext.HttpRequestIntercepted.
111         /// After it called, any further call on WebHttpRequestInterceptor results in undefined behavior.
112         /// </summary>
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         public bool Ignore()
115         {
116             bool result = Interop.WebHttpRequestInterceptor.Ignore(interceptorHandle);
117             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
118             return result;
119         }
120
121         /// <summary>
122         /// Sets status code and status text of response for intercepted request.
123         /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
124         /// <param name="statusCode">Status code of response</param>
125         /// <param name="customStatusText">Status text of response</param>
126         /// </summary>
127         [EditorBrowsable(EditorBrowsableState.Never)]
128         public bool SetResponseStatus(int statusCode, string customStatusText)
129         {
130             bool result = Interop.WebHttpRequestInterceptor.SetResponseStatus(interceptorHandle, statusCode, customStatusText);
131             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
132             return result;
133         }
134
135         /// <summary>
136         /// Adds HTTP header to response for intercepted request.
137         /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
138         /// <param name="fieldName">Key of response header</param>
139         /// <param name="fieldValue">Value of response header</param>
140         /// </summary>
141         [EditorBrowsable(EditorBrowsableState.Never)]
142         public bool AddResponseHeader(string fieldName, string fieldValue)
143         {
144             bool result = Interop.WebHttpRequestInterceptor.AddResponseHeader(interceptorHandle, fieldName, fieldValue);
145             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
146             return result;
147         }
148
149         /// <summary>
150         /// Adds HTTP headers to response for intercepted request.
151         /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
152         /// <param name="headers">Map of response headers</param>
153         /// </summary>
154         [EditorBrowsable(EditorBrowsableState.Never)]
155         public bool AddResponseHeaders(IDictionary<string, string> headers)
156         {
157             if (headers == null)
158                 return false;
159             PropertyMap headerMap = new PropertyMap();
160             foreach (KeyValuePair<string, string> kvp in headers)
161             {
162                 using (PropertyValue value = new PropertyValue(kvp.Value))
163                 {
164                     headerMap.Add(kvp.Key, value);
165                 }
166             }
167             bool result = Interop.WebHttpRequestInterceptor.AddResponseHeaders(interceptorHandle, PropertyMap.getCPtr(headerMap));
168             headerMap.Dispose();
169             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170             return result;
171         }
172
173         /// <summary>
174         /// Writes whole response body at once.
175         /// To call it, application should have full response body ready for the intercepted request.
176         /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
177         /// After this call, any further call on WebHttpRequestInterceptor results in undefined behavior.
178         /// <param name="body">Contents of response</param>
179         /// </summary>
180         [EditorBrowsable(EditorBrowsableState.Never)]
181         public bool SetResponseBody(string body)
182         {
183             if (body == null)
184                 return false;
185             bool result = Interop.WebHttpRequestInterceptor.AddResponseBody(interceptorHandle, body, (uint)body.Length);
186             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187             return result;
188         }
189
190         /// <summary>
191         /// Writes whole response body with headers at once.
192         /// To call it, application should have full response headers and body ready for the intercepted request.
193         /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
194         /// After this call, any further call on WebHttpRequestInterceptor results in undefined behavior.
195         /// <param name="headers">Headers of response</param>
196         /// <param name="body">Contents of response</param>
197         /// </summary>
198         [EditorBrowsable(EditorBrowsableState.Never)]
199         public bool SetResponse(string headers, string body)
200         {
201             if (body == null)
202                 return false;
203             bool result = Interop.WebHttpRequestInterceptor.AddResponse(interceptorHandle, headers, body, (uint)body.Length);
204             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205             return result;
206         }
207
208         /// <summary>
209         /// Writes a part of response body.
210         /// This function can be called only OUTSIDE WebContext.HttpRequestIntercepted.
211         /// If it returns false, handling the request is done.
212         /// Any further calls result in undefined behavior.
213         /// User should always check return value, because response to this request might not be needed any more,
214         /// and the function can return false even though user still has data to write.
215         /// 
216         /// After writing full response body in chunks using this function,
217         /// call it again with null as chunk, to signal that response body is finished.
218         /// <param name="chunk">Chunk of response</param>
219         /// </summary>
220         [EditorBrowsable(EditorBrowsableState.Never)]
221         public bool WriteResponseChunk(string chunk)
222         {
223             int length = 0;
224             if (chunk != null)
225             {
226                 length = chunk.Length;
227             }
228             bool result = Interop.WebHttpRequestInterceptor.WriteResponseChunk(interceptorHandle, chunk, (uint)length);
229             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230             return result;
231         }
232     }
233 }