Add some new APIs like Context, Settings, etc. (#2419)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / WebView.cs
1 /*
2  * Copyright (c) 2017 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.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// WebView
29     /// </summary>
30     [EditorBrowsable(EditorBrowsableState.Never)]
31     public class WebView : View
32     {
33         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
34         private delegate void WebViewPageLoadCallbackDelegate(IntPtr data, string pageUrl);
35
36         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
37         private delegate void WebViewPageLoadErrorCallbackDelegate(IntPtr data, string pageUrl, int errorCode);
38
39         private readonly WebViewPageLoadSignal pageLoadStartedSignal;
40         private EventHandler<WebViewPageLoadEventArgs> pageLoadStartedEventHandler;
41         private WebViewPageLoadCallbackDelegate pageLoadStartedCallback;
42
43         private readonly WebViewPageLoadSignal pageLoadFinishedSignal;
44         private EventHandler<WebViewPageLoadEventArgs> pageLoadFinishedEventHandler;
45         private WebViewPageLoadCallbackDelegate pageLoadFinishedCallback;
46
47         private readonly WebViewPageLoadErrorSignal pageLoadErrorSignal;
48         private EventHandler<WebViewPageLoadErrorEventArgs> pageLoadErrorEventHandler;
49         private WebViewPageLoadErrorCallbackDelegate pageLoadErrorCallback;
50
51         internal WebView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.WebView.Upcast(cPtr), cMemoryOwn)
52         {
53             pageLoadStartedSignal = new WebViewPageLoadSignal(Interop.WebView.NewWebViewPageLoadSignalPageLoadStarted(SwigCPtr));
54             pageLoadFinishedSignal = new WebViewPageLoadSignal(Interop.WebView.NewWebViewPageLoadSignalPageLoadFinished(SwigCPtr));
55             pageLoadErrorSignal = new WebViewPageLoadErrorSignal(Interop.WebView.NewWebViewPageLoadErrorSignalPageLoadError(SwigCPtr));
56
57             BackForwardList = new WebBackForwardList(Interop.WebView.GetWebBackForwardList(SwigCPtr), false);
58             Context = new WebContext(Interop.WebView.GetWebContext(SwigCPtr), false);
59             CookieManager = new WebCookieManager(Interop.WebView.GetWebCookieManager(SwigCPtr), false);
60             Settings = new WebSettings(Interop.WebView.GetWebSettings(SwigCPtr), false);
61         }
62
63         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(WebView obj)
64         {
65             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
66         }
67
68         internal WebView Assign(WebView webView)
69         {
70             WebView ret = new WebView(Interop.WebView.Assign(SwigCPtr, WebView.getCPtr(webView)), false);
71             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
72             return ret;
73         }
74
75         internal static WebView DownCast(BaseHandle handle)
76         {
77             WebView ret = new WebView(Interop.WebView.DownCast(BaseHandle.getCPtr(handle)), true);
78             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
79             return ret;
80         }
81
82         /// <summary>
83         /// Dispose for IDisposable pattern
84         /// </summary>
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         protected override void Dispose(DisposeTypes type)
87         {
88             if (disposed)
89             {
90                 return;
91             }
92
93             if (type == DisposeTypes.Explicit)
94             {
95                 //Called by User
96                 //Release your own managed resources here.
97                 //You should release all of your own disposable objects here.
98                 pageLoadStartedSignal.Dispose();
99                 pageLoadFinishedSignal.Dispose();
100                 pageLoadErrorSignal.Dispose();
101
102                 BackForwardList.Dispose();
103                 Context.Dispose();
104                 CookieManager.Dispose();
105                 Settings.Dispose();
106             }
107
108             base.Dispose(type);
109         }
110
111         /// This will not be public opened.
112         /// <param name="swigCPtr"></param>
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
115         {
116             Interop.WebView.DeleteWebView(swigCPtr);
117         }
118
119         private void OnPageLoadStarted(IntPtr data, string pageUrl)
120         {
121             WebViewPageLoadEventArgs e = new WebViewPageLoadEventArgs();
122
123             e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView;
124             e.PageUrl = pageUrl;
125
126             pageLoadStartedEventHandler?.Invoke(this, e);
127         }
128
129         private void OnPageLoadFinished(IntPtr data, string pageUrl)
130         {
131             WebViewPageLoadEventArgs e = new WebViewPageLoadEventArgs();
132
133             e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView;
134             e.PageUrl = pageUrl;
135
136             pageLoadFinishedEventHandler?.Invoke(this, e);
137         }
138
139         private void OnPageLoadError(IntPtr data, string pageUrl, int errorCode)
140         {
141             WebViewPageLoadErrorEventArgs e = new WebViewPageLoadErrorEventArgs();
142
143             e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView;
144             e.PageUrl = pageUrl;
145             e.ErrorCode = (WebViewPageLoadErrorEventArgs.LoadErrorCode)errorCode;
146
147             pageLoadErrorEventHandler?.Invoke(this, e);
148         }
149
150         internal static new class Property
151         {
152             internal static readonly int Url = Interop.WebView.UrlGet();
153             internal static readonly int UserAgent = Interop.WebView.UserAgentGet();
154         }
155
156         private static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(WebView), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
157         {
158             var webview = (WebView)bindable;
159             if (newValue != null)
160             {
161                 Tizen.NUI.Object.SetProperty(webview.SwigCPtr, WebView.Property.Url, new Tizen.NUI.PropertyValue((string)newValue));
162             }
163         }),
164         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
165         {
166             var webview = (WebView)bindable;
167             string temp;
168             Tizen.NUI.Object.GetProperty(webview.SwigCPtr, WebView.Property.Url).Get(out temp);
169             return temp;
170         }));
171
172         private static readonly BindableProperty UserAgentProperty = BindableProperty.Create(nameof(UserAgent), typeof(string), typeof(WebView), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
173         {
174             var webview = (WebView)bindable;
175             if (newValue != null)
176             {
177                 Tizen.NUI.Object.SetProperty((HandleRef)webview.SwigCPtr, WebView.Property.UserAgent, new Tizen.NUI.PropertyValue((string)newValue));
178             }
179         }),
180         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
181         {
182             var webview = (WebView)bindable;
183             string temp;
184             Tizen.NUI.Object.GetProperty((HandleRef)webview.SwigCPtr, WebView.Property.UserAgent).Get(out temp);
185             return temp;
186         }));
187
188         /// <summary>
189         /// Creates an uninitialized WebView.
190         /// </summary>
191         [EditorBrowsable(EditorBrowsableState.Never)]
192         public WebView() : this(Interop.WebView.New(), true)
193         {
194             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
195         }
196
197         /// <summary>
198         /// Creates an uninitialized WebView.
199         /// <param name="locale">The locale of Web</param>
200         /// <param name="timezoneId">The timezoneId of Web</param>
201         /// </summary>
202         [EditorBrowsable(EditorBrowsableState.Never)]
203         public WebView(string locale, string timezoneId) : this(Interop.WebView.New2(locale, timezoneId), true)
204         {
205             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206         }
207
208         /// <summary>
209         /// Copy constructor.
210         /// <param name="webView">WebView to copy. The copied WebView will point at the same implementation</param>
211         /// </summary>
212         [EditorBrowsable(EditorBrowsableState.Never)]
213         public WebView(WebView webView) : this(Interop.WebView.NewWebView(WebView.getCPtr(webView)), true)
214         {
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216         }
217
218         /// <summary>
219         /// BackForwardList.
220         /// </summary>
221         [EditorBrowsable(EditorBrowsableState.Never)]
222         public WebBackForwardList BackForwardList { get; }
223
224         /// <summary>
225         /// Context.
226         /// </summary>
227         [EditorBrowsable(EditorBrowsableState.Never)]
228         public WebContext Context { get; }
229
230         /// <summary>
231         /// CookieManager.
232         /// </summary>
233         [EditorBrowsable(EditorBrowsableState.Never)]
234         public WebCookieManager CookieManager { get; }
235
236         /// <summary>
237         /// BackForwardList.
238         /// </summary>
239         [EditorBrowsable(EditorBrowsableState.Never)]
240         public WebSettings Settings { get; }
241
242         /// <summary>
243         /// The url to load.
244         /// </summary>
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public string Url
247         {
248             get
249             {
250                 return (string)GetValue(UrlProperty);
251             }
252             set
253             {
254                 SetValue(UrlProperty, value);
255                 NotifyPropertyChanged();
256             }
257         }
258
259         /// <summary>
260         /// Deprecated. The cache model of the current WebView.
261         /// </summary>
262         [EditorBrowsable(EditorBrowsableState.Never)]
263         public CacheModel CacheModel
264         {
265             get
266             {
267                 return (CacheModel)Context.CacheModel;
268             }
269             set
270             {
271                 Context.CacheModel = (WebContext.CacheModelType)value;
272             }
273         }
274
275         /// <summary>
276         /// Deprecated. The cookie acceptance policy.
277         /// </summary>
278         [EditorBrowsable(EditorBrowsableState.Never)]
279         public CookieAcceptPolicy CookieAcceptPolicy
280         {
281             get
282             {
283                 return (CookieAcceptPolicy)CookieManager.CookieAcceptPolicy;
284             }
285             set
286             {
287                 CookieManager.CookieAcceptPolicy = (WebCookieManager.CookieAcceptPolicyType)value;
288             }
289         }
290
291         /// <summary>
292         /// The user agent string.
293         /// </summary>
294         [EditorBrowsable(EditorBrowsableState.Never)]
295         public string UserAgent
296         {
297             get
298             {
299                 return (string)GetValue(UserAgentProperty);
300             }
301             set
302             {
303                 SetValue(UserAgentProperty, value);
304                 NotifyPropertyChanged();
305             }
306         }
307
308         /// <summary>
309         /// Deprecated. Whether JavaScript is enabled.
310         /// </summary>
311         [EditorBrowsable(EditorBrowsableState.Never)]
312         public bool EnableJavaScript
313         {
314             get
315             {
316                 return Settings.EnableJavaScript;
317             }
318             set
319             {
320                 Settings.EnableJavaScript = value;
321             }
322         }
323
324         /// <summary>
325         /// Deprecated. Whether images can be loaded automatically.
326         /// </summary>
327         [EditorBrowsable(EditorBrowsableState.Never)]
328         public bool LoadImagesAutomatically
329         {
330             get
331             {
332                 return Settings.AllowImagesLoadAutomatically;
333             }
334             set
335             {
336                 Settings.AllowImagesLoadAutomatically = value;
337             }
338         }
339
340         /// <summary>
341         /// The default text encoding name.<br />
342         /// e.g. "UTF-8"<br />
343         /// </summary>
344         [EditorBrowsable(EditorBrowsableState.Never)]
345         public string DefaultTextEncodingName
346         {
347             get
348             {
349                 return Settings.DefaultTextEncodingName;
350             }
351             set
352             {
353                 Settings.DefaultTextEncodingName = value;
354             }
355         }
356
357         /// <summary>
358         /// The default font size in pixel.
359         /// </summary>
360         [EditorBrowsable(EditorBrowsableState.Never)]
361         public int DefaultFontSize
362         {
363             get
364             {
365                 return Settings.DefaultFontSize;
366             }
367             set
368             {
369                 Settings.DefaultFontSize = value;
370             }
371         }
372
373         /// <summary>
374         /// Event for the PageLoadStarted signal which can be used to subscribe or unsubscribe the event handler.<br />
375         /// This signal is emitted when page loading has started.<br />
376         /// </summary>
377         [EditorBrowsable(EditorBrowsableState.Never)]
378         public event EventHandler<WebViewPageLoadEventArgs> PageLoadStarted
379         {
380             add
381             {
382                 if (pageLoadStartedEventHandler == null)
383                 {
384                     pageLoadStartedCallback = (OnPageLoadStarted);
385                     pageLoadStartedSignal.Connect(pageLoadStartedCallback);
386                 }
387                 pageLoadStartedEventHandler += value;
388             }
389             remove
390             {
391                 pageLoadStartedEventHandler -= value;
392                 if (pageLoadStartedEventHandler == null && pageLoadStartedCallback != null)
393                 {
394                     pageLoadStartedSignal.Disconnect(pageLoadStartedCallback);
395                 }
396             }
397         }
398
399         /// <summary>
400         /// Event for the PageLoadFinished signal which can be used to subscribe or unsubscribe the event handler.<br />
401         /// This signal is emitted when page loading has finished.<br />
402         /// </summary>
403         [EditorBrowsable(EditorBrowsableState.Never)]
404         public event EventHandler<WebViewPageLoadEventArgs> PageLoadFinished
405         {
406             add
407             {
408                 if (pageLoadFinishedEventHandler == null)
409                 {
410                     pageLoadFinishedCallback = (OnPageLoadFinished);
411                     pageLoadFinishedSignal.Connect(pageLoadFinishedCallback);
412                 }
413                 pageLoadFinishedEventHandler += value;
414             }
415             remove
416             {
417                 pageLoadFinishedEventHandler -= value;
418                 if (pageLoadFinishedEventHandler == null && pageLoadFinishedCallback != null)
419                 {
420                     pageLoadFinishedSignal.Disconnect(pageLoadFinishedCallback);
421                 }
422             }
423         }
424
425         /// <summary>
426         /// Event for the PageLoadError signal which can be used to subscribe or unsubscribe the event handler.<br />
427         /// This signal is emitted when there's an error in page loading.<br />
428         /// </summary>
429         [EditorBrowsable(EditorBrowsableState.Never)]
430         public event EventHandler<WebViewPageLoadErrorEventArgs> PageLoadError
431         {
432             add
433             {
434                 if (pageLoadErrorEventHandler == null)
435                 {
436                     pageLoadErrorCallback = (OnPageLoadError);
437                     pageLoadErrorSignal.Connect(pageLoadErrorCallback);
438                 }
439                 pageLoadErrorEventHandler += value;
440             }
441             remove
442             {
443                 pageLoadErrorEventHandler -= value;
444                 if (pageLoadErrorEventHandler == null && pageLoadErrorCallback != null)
445                 {
446                     pageLoadErrorSignal.Disconnect(pageLoadErrorCallback);
447                 }
448             }
449         }
450
451         /// <summary>
452         /// Loads a html.
453         /// <param name="url">The path of Web</param>
454         /// </summary>
455         [EditorBrowsable(EditorBrowsableState.Never)]
456         public void LoadUrl(string url)
457         {
458             Interop.WebView.LoadUrl(SwigCPtr, url);
459             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
460         }
461
462         /// <summary>
463         /// Deprecated. Loads a html by string.
464         /// <param name="data">The data of Web</param>
465         /// </summary>
466         [EditorBrowsable(EditorBrowsableState.Never)]
467         public void LoadHTMLString(string data)
468         {
469             Interop.WebView.LoadHtmlString(SwigCPtr, data);
470             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
471         }
472
473         /// <summary>
474         /// Loads a html by string.
475         /// <param name="data">The data of Web</param>
476         /// </summary>
477         [EditorBrowsable(EditorBrowsableState.Never)]
478         public void LoadHtmlString(string data)
479         {
480             Interop.WebView.LoadHtmlString(SwigCPtr, data);
481             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482         }
483
484         /// <summary>
485         /// Reloads the Web
486         /// </summary>
487         [EditorBrowsable(EditorBrowsableState.Never)]
488         public void Reload()
489         {
490             Interop.WebView.Reload(SwigCPtr);
491             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
492         }
493
494         /// <summary>
495         /// Stops loading the Web
496         /// </summary>
497         [EditorBrowsable(EditorBrowsableState.Never)]
498         public void StopLoading()
499         {
500             Interop.WebView.StopLoading(SwigCPtr);
501             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
502         }
503
504         /// <summary>
505         /// Suspends the operation.
506         /// </summary>
507         [EditorBrowsable(EditorBrowsableState.Never)]
508         public void Suspend()
509         {
510             Interop.WebView.Suspend(SwigCPtr);
511             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
512         }
513
514         /// <summary>
515         /// Resumes the operation after calling Suspend()
516         /// </summary>
517         [EditorBrowsable(EditorBrowsableState.Never)]
518         public void Resume()
519         {
520             Interop.WebView.Resume(SwigCPtr);
521             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
522         }
523
524         /// <summary>
525         /// Goes to the back
526         /// </summary>
527         [EditorBrowsable(EditorBrowsableState.Never)]
528         public void GoBack()
529         {
530             Interop.WebView.GoBack(SwigCPtr);
531             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
532         }
533
534         /// <summary>
535         /// Goes to the forward
536         /// </summary>
537         [EditorBrowsable(EditorBrowsableState.Never)]
538         public void GoForward()
539         {
540             Interop.WebView.GoForward(SwigCPtr);
541             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
542         }
543
544         /// <summary>
545         /// Returns whether backward is possible.
546         /// <returns>True if backward is possible, false otherwise</returns>
547         /// </summary>
548         [EditorBrowsable(EditorBrowsableState.Never)]
549         public bool CanGoBack()
550         {
551             bool ret = Interop.WebView.CanGoBack(SwigCPtr);
552             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
553             return ret;
554         }
555
556         /// <summary>
557         /// Returns whether forward is possible.
558         /// <returns>True if forward is possible, false otherwise</returns>
559         /// </summary>
560         [EditorBrowsable(EditorBrowsableState.Never)]
561         public bool CanGoForward()
562         {
563             bool ret = Interop.WebView.CanGoForward(SwigCPtr);
564             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
565             return ret;
566         }
567
568         /// <summary>
569         /// The callback function that is invoked when the message is received from the script.
570         /// </summary>
571         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
572         [EditorBrowsable(EditorBrowsableState.Never)]
573         public delegate void JavaScriptMessageHandler(string message);
574
575         /// <summary>
576         /// Evaluates JavaScript code represented as a string.
577         /// <param name="script">The JavaScript code</param>
578         /// </summary>
579         [EditorBrowsable(EditorBrowsableState.Never)]
580         public void EvaluateJavaScript(string script)
581         {
582             Interop.WebView.EvaluateJavaScript(SwigCPtr, script, new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero));
583             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
584         }
585
586         // For rooting handlers
587         internal Dictionary<string, JavaScriptMessageHandler> handlerRootMap = new Dictionary<string, JavaScriptMessageHandler>();
588
589         /// <summary>
590         /// Add a message handler into the WebView.
591         /// <param name="objectName">The name of exposed object</param>
592         /// <param name="handler">The callback function</param>
593         /// </summary>
594         [EditorBrowsable(EditorBrowsableState.Never)]
595         public void AddJavaScriptMessageHandler(string objectName, JavaScriptMessageHandler handler)
596         {
597             if (handlerRootMap.ContainsKey(objectName))
598             {
599                 return;
600             }
601
602             handlerRootMap.Add(objectName, handler);
603
604             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler);
605             Interop.WebView.AddJavaScriptMessageHandler(SwigCPtr, objectName, new System.Runtime.InteropServices.HandleRef(this, ip));
606
607             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
608         }
609
610         /// <summary>
611         /// Clears the history of current WebView.
612         /// </summary>
613         [EditorBrowsable(EditorBrowsableState.Never)]
614         public void ClearHistory()
615         {
616             Interop.WebView.ClearHistory(SwigCPtr);
617             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
618         }
619
620         /// <summary>
621         /// Deprecated. Clears the cache of current WebView.
622         /// </summary>
623         [EditorBrowsable(EditorBrowsableState.Never)]
624         public void ClearCache()
625         {
626             Context.ClearCache();
627         }
628
629         /// <summary>
630         /// Deprecated. Clears all the cookies of current WebView.
631         /// </summary>
632         [EditorBrowsable(EditorBrowsableState.Never)]
633         public void ClearCookies()
634         {
635             CookieManager.ClearCookies();
636         }
637     }
638 }