5ee9ddcf361e4c5d2c06915bdab3cc75ab01139c
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / WebView / WebContext.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.ComponentModel;
20 using System.Runtime.InteropServices;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// WebContext is a class for context of web view.
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class WebContext : Disposable
29     {
30         private string proxyUri;
31         private string certificateFilePath;
32         private bool disableCache;
33         private SecurityOriginListAcquiredCallback securityOriginListAcquiredCallback;
34         private readonly WebContextSecurityOriginListAcquiredProxyCallback securityOriginListAcquiredProxyCallback;
35         private PasswordDataListAcquiredCallback passwordDataListAcquiredCallback;
36         private readonly WebContextPasswordDataListAcquiredProxyCallback passwordDataListAcquiredProxyCallback;
37
38         internal WebContext(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
39         {
40             securityOriginListAcquiredProxyCallback = OnSecurityOriginListAcquired;
41             passwordDataListAcquiredProxyCallback = OnPasswordDataListAcquired;
42         }
43
44         /// <summary>
45         /// The callback function that is invoked when security origin list is acquired.
46         /// </summary>
47         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public delegate void SecurityOriginListAcquiredCallback(WebSecurityOriginList list);
50
51         /// <summary>
52         /// The callback function that is invoked when storage usage is acquired.
53         /// </summary>
54         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         public delegate void StorageUsageAcquiredCallback(ulong usage);
57
58         /// <summary>
59         /// The callback function that is invoked when security origin list is acquired.
60         /// </summary>
61         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         public delegate void PasswordDataListAcquiredCallback(WebPasswordDataList list);
64
65         /// <summary>
66         /// The callback function that is invoked when download is started.
67         /// </summary>
68         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public delegate void DownloadStartedCallback(string url);
71
72         /// <summary>
73         /// The callback function that is invoked when current mime type need be overridden.
74         /// </summary>
75         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public delegate bool MimeOverriddenCallback(string url, string currentMime, string newMime);
78
79         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
80         private delegate void WebContextSecurityOriginListAcquiredProxyCallback(IntPtr list);
81
82         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
83         private delegate void WebContextPasswordDataListAcquiredProxyCallback(IntPtr list);
84
85         /// <summary>
86         /// Cache model
87         /// </summary>
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public enum CacheModelType
90         {
91             /// <summary>
92             /// The smallest cache capacity
93             /// </summary>
94             [EditorBrowsable(EditorBrowsableState.Never)]
95             DocumentViewer,
96
97             /// <summary>
98             /// The bigger cache capacity than DocumentBrowser
99             /// </summary>
100             [EditorBrowsable(EditorBrowsableState.Never)]
101             DocumentBrowser,
102
103             /// <summary>
104             /// The biggest cache capacity.
105             /// </summary>
106             [EditorBrowsable(EditorBrowsableState.Never)]
107             PrimaryWebBrowser,
108         }
109
110         /// <summary>
111         /// Cache model
112         /// </summary>
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         public CacheModelType CacheModel
115         {
116             get
117             {
118                 return (CacheModelType)Interop.WebContext.GetCacheModel(SwigCPtr);
119             }
120             set
121             {
122                 Interop.WebContext.SetCacheModel(SwigCPtr, (int)value);
123             }
124         }
125
126         /// <summary>
127         /// Set the proxy uri.
128         /// </summary>
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         public Uri ProxyUri
131         {
132             get
133             {
134                 return new Uri(proxyUri);
135             }
136             set
137             {
138                 if (value != null)
139                 {
140                     proxyUri = value.AbsoluteUri;
141                     Interop.WebContext.SetProxyUri(SwigCPtr, proxyUri);
142                 }
143             }
144         }
145
146         /// <summary>
147         /// Set the Certificate File Path.
148         /// </summary>
149         [EditorBrowsable(EditorBrowsableState.Never)]
150         public string CertificateFilePath
151         {
152             get
153             {
154                 return certificateFilePath;
155             }
156             set
157             {
158                 certificateFilePath = value;
159                 Interop.WebContext.SetCertificateFilePath(SwigCPtr, value);
160             }
161         }
162
163         /// <summary>
164         /// Disable cache or not.
165         /// </summary>
166         [EditorBrowsable(EditorBrowsableState.Never)]
167         public bool DisableCache
168         {
169             get
170             {
171                 return disableCache;
172             }
173             set
174             {
175                 disableCache = value;
176                 Interop.WebContext.DisableCache(SwigCPtr, value);
177             }
178         }
179
180         /// <summary>
181         /// Set Default Proxy Auth.
182         /// </summary>
183         [EditorBrowsable(EditorBrowsableState.Never)]
184         public void SetDefaultProxyAuth(string username, string password)
185         {
186             Interop.WebContext.SetDefaultProxyAuth(SwigCPtr, username, password);
187             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188         }
189
190         /// <summary>
191         /// Delete all web database.
192         /// </summary>
193         [EditorBrowsable(EditorBrowsableState.Never)]
194         public void DeleteAllWebDatabase()
195         {
196             Interop.WebContext.DeleteAllWebDatabase(SwigCPtr);
197             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
198         }
199
200         /// <summary>
201         /// Gets security origins of web database asynchronously.
202         /// <param name="callback">callback for acquiring security origins</param>
203         /// </summary>
204         [EditorBrowsable(EditorBrowsableState.Never)]
205         public bool GetWebDatabaseOrigins(SecurityOriginListAcquiredCallback callback)
206         {
207             securityOriginListAcquiredCallback = callback;
208             IntPtr ip = Marshal.GetFunctionPointerForDelegate(securityOriginListAcquiredProxyCallback);
209             bool result = Interop.WebContext.GetWebDatabaseOrigins(SwigCPtr, new HandleRef(this, ip));
210             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
211             return result;
212         }
213
214         /// <summary>
215         /// Deletes web databases by origin.
216         /// <param name="origin">security origin of web database</param>
217         /// </summary>
218         [EditorBrowsable(EditorBrowsableState.Never)]
219         public bool DeleteWebDatabase(WebSecurityOrigin origin)
220         {
221             bool result = Interop.WebContext.DeleteWebDatabase(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223             return result;
224         }
225
226         /// <summary>
227         /// Gets a list of security origins of web storage asynchronously.
228         /// <param name="callback">callback for acquiring security origins</param>
229         /// </summary>
230         [EditorBrowsable(EditorBrowsableState.Never)]
231         public bool GetWebStorageOrigins(SecurityOriginListAcquiredCallback callback)
232         {
233             securityOriginListAcquiredCallback = callback;
234             IntPtr ip = Marshal.GetFunctionPointerForDelegate(securityOriginListAcquiredProxyCallback);
235             bool result = Interop.WebContext.GetWebStorageOrigins(SwigCPtr, new HandleRef(this, ip));
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return result;
238         }
239
240         /// <summary>
241         /// Gets a list of security origins of web storage asynchronously.
242         /// <param name="origin">security origin of web storage</param>
243         /// <param name="callback">callback for acquiring storage usage</param>
244         /// </summary>
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public bool GetWebStorageUsageForOrigin(WebSecurityOrigin origin, StorageUsageAcquiredCallback callback)
247         {
248             IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
249             bool result = Interop.WebContext.GetWebStorageUsageForOrigin(SwigCPtr, WebSecurityOrigin.getCPtr(origin), new HandleRef(this, ip));
250             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
251             return result;
252         }
253
254         /// <summary>
255         /// Delete all web storage.
256         /// </summary>
257         [EditorBrowsable(EditorBrowsableState.Never)]
258         public void DeleteAllWebStorage()
259         {
260             Interop.WebContext.DeleteAllWebStorage(SwigCPtr);
261             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
262         }
263
264         /// <summary>
265         /// Deletes web storage by origin.
266         /// <param name="origin">security origin of web storage</param>
267         /// </summary>
268         [EditorBrowsable(EditorBrowsableState.Never)]
269         public bool DeleteWebStorage(WebSecurityOrigin origin)
270         {
271             bool result = Interop.WebContext.DeleteWebStorageOrigin(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
272             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
273             return result;
274         }
275
276         /// <summary>
277         /// Delete local fileSystem.
278         /// </summary>
279         [EditorBrowsable(EditorBrowsableState.Never)]
280         public void DeleteLocalFileSystem()
281         {
282             Interop.WebContext.DeleteLocalFileSystem(SwigCPtr);
283             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
284         }
285
286         /// <summary>
287         /// Clear cache.
288         /// </summary>
289         [EditorBrowsable(EditorBrowsableState.Never)]
290         public void ClearCache()
291         {
292             Interop.WebContext.ClearCache(SwigCPtr);
293             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
294         }
295
296         /// <summary>
297         /// Deletes web application cache by origin.
298         /// <param name="origin">security origin of web application</param>
299         /// </summary>
300         [EditorBrowsable(EditorBrowsableState.Never)]
301         public bool DeleteApplicationCache(WebSecurityOrigin origin)
302         {
303             bool result = Interop.WebContext.DeleteApplicationCache(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
304             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305             return result;
306         }
307
308         /// <summary>
309         /// Gets a list of all password data asynchronously.
310         /// <param name="callback">callback for acquiring password data list</param>
311         /// </summary>
312         [EditorBrowsable(EditorBrowsableState.Never)]
313         public void GetFormPasswordList(PasswordDataListAcquiredCallback callback)
314         {
315             passwordDataListAcquiredCallback = callback;
316             IntPtr ip = Marshal.GetFunctionPointerForDelegate(passwordDataListAcquiredProxyCallback);
317             Interop.WebContext.GetFormPasswordList(SwigCPtr, new HandleRef(this, ip));
318             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
319         }
320
321         /// <summary>
322         /// Registers callback for download started.
323         /// <param name="callback">callback for download started</param>
324         /// </summary>
325         [EditorBrowsable(EditorBrowsableState.Never)]
326         public void RegisterDownloadStartedCallback(DownloadStartedCallback callback)
327         {
328             IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
329             Interop.WebContext.RegisterDownloadStartedCallback(SwigCPtr, new HandleRef(this, ip));
330             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331         }
332
333         /// <summary>
334         /// Registers callback for overriding mime type.
335         /// <param name="callback">callback for overriding mime type</param>
336         /// </summary>
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         public void RegisterMimeOverriddenCallback(MimeOverriddenCallback callback)
339         {
340             IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
341             Interop.WebContext.RegisterMimeOverriddenCallback(SwigCPtr, new HandleRef(this, ip));
342             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
343         }
344
345         private void OnSecurityOriginListAcquired(IntPtr list)
346         {
347             WebSecurityOriginList originList = new WebSecurityOriginList(list, true);
348             securityOriginListAcquiredCallback?.Invoke(originList);
349             originList.Dispose();
350         }
351
352         private void OnPasswordDataListAcquired(IntPtr list)
353         {
354             WebPasswordDataList passwordList = new WebPasswordDataList(list, true);
355             passwordDataListAcquiredCallback?.Invoke(passwordList);
356             passwordList.Dispose();
357         }
358     }
359 }