2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding;
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 public class WebView : View
33 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
34 private delegate void WebViewPageLoadCallbackDelegate(IntPtr data, string pageUrl);
36 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
37 private delegate void WebViewPageLoadErrorCallbackDelegate(IntPtr data, string pageUrl, int errorCode);
39 private readonly WebViewPageLoadSignal pageLoadStartedSignal;
40 private EventHandler<WebViewPageLoadEventArgs> pageLoadStartedEventHandler;
41 private WebViewPageLoadCallbackDelegate pageLoadStartedCallback;
43 private readonly WebViewPageLoadSignal pageLoadFinishedSignal;
44 private EventHandler<WebViewPageLoadEventArgs> pageLoadFinishedEventHandler;
45 private WebViewPageLoadCallbackDelegate pageLoadFinishedCallback;
47 private readonly WebViewPageLoadErrorSignal pageLoadErrorSignal;
48 private EventHandler<WebViewPageLoadErrorEventArgs> pageLoadErrorEventHandler;
49 private WebViewPageLoadErrorCallbackDelegate pageLoadErrorCallback;
51 internal WebView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.WebView.Upcast(cPtr), cMemoryOwn)
53 pageLoadStartedSignal = new WebViewPageLoadSignal(Interop.WebView.NewWebViewPageLoadSignalPageLoadStarted(SwigCPtr));
54 pageLoadFinishedSignal = new WebViewPageLoadSignal(Interop.WebView.NewWebViewPageLoadSignalPageLoadFinished(SwigCPtr));
55 pageLoadErrorSignal = new WebViewPageLoadErrorSignal(Interop.WebView.NewWebViewPageLoadErrorSignalPageLoadError(SwigCPtr));
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);
63 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(WebView obj)
65 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
68 internal WebView Assign(WebView webView)
70 WebView ret = new WebView(Interop.WebView.Assign(SwigCPtr, WebView.getCPtr(webView)), false);
71 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
75 internal static WebView DownCast(BaseHandle handle)
77 WebView ret = new WebView(Interop.WebView.DownCast(BaseHandle.getCPtr(handle)), true);
78 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
83 /// Dispose for IDisposable pattern
85 [EditorBrowsable(EditorBrowsableState.Never)]
86 protected override void Dispose(DisposeTypes type)
93 if (type == DisposeTypes.Explicit)
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();
102 BackForwardList.Dispose();
104 CookieManager.Dispose();
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)
116 Interop.WebView.DeleteWebView(swigCPtr);
119 private void OnPageLoadStarted(IntPtr data, string pageUrl)
121 WebViewPageLoadEventArgs e = new WebViewPageLoadEventArgs();
123 e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView;
126 pageLoadStartedEventHandler?.Invoke(this, e);
129 private void OnPageLoadFinished(IntPtr data, string pageUrl)
131 WebViewPageLoadEventArgs e = new WebViewPageLoadEventArgs();
133 e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView;
136 pageLoadFinishedEventHandler?.Invoke(this, e);
139 private void OnPageLoadError(IntPtr data, string pageUrl, int errorCode)
141 WebViewPageLoadErrorEventArgs e = new WebViewPageLoadErrorEventArgs();
143 e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView;
145 e.ErrorCode = (WebViewPageLoadErrorEventArgs.LoadErrorCode)errorCode;
147 pageLoadErrorEventHandler?.Invoke(this, e);
150 internal static new class Property
152 internal static readonly int Url = Interop.WebView.UrlGet();
153 internal static readonly int UserAgent = Interop.WebView.UserAgentGet();
156 private static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(WebView), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
158 var webview = (WebView)bindable;
159 if (newValue != null)
161 Tizen.NUI.Object.SetProperty(webview.SwigCPtr, WebView.Property.Url, new Tizen.NUI.PropertyValue((string)newValue));
164 defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
166 var webview = (WebView)bindable;
168 Tizen.NUI.Object.GetProperty(webview.SwigCPtr, WebView.Property.Url).Get(out temp);
172 private static readonly BindableProperty UserAgentProperty = BindableProperty.Create(nameof(UserAgent), typeof(string), typeof(WebView), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
174 var webview = (WebView)bindable;
175 if (newValue != null)
177 Tizen.NUI.Object.SetProperty((HandleRef)webview.SwigCPtr, WebView.Property.UserAgent, new Tizen.NUI.PropertyValue((string)newValue));
180 defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
182 var webview = (WebView)bindable;
184 Tizen.NUI.Object.GetProperty((HandleRef)webview.SwigCPtr, WebView.Property.UserAgent).Get(out temp);
189 /// Creates an uninitialized WebView.
191 [EditorBrowsable(EditorBrowsableState.Never)]
192 public WebView() : this(Interop.WebView.New(), true)
194 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
198 /// Creates an uninitialized WebView.
199 /// <param name="locale">The locale of Web</param>
200 /// <param name="timezoneId">The timezoneId of Web</param>
202 [EditorBrowsable(EditorBrowsableState.Never)]
203 public WebView(string locale, string timezoneId) : this(Interop.WebView.New2(locale, timezoneId), true)
205 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
209 /// Copy constructor.
210 /// <param name="webView">WebView to copy. The copied WebView will point at the same implementation</param>
212 [EditorBrowsable(EditorBrowsableState.Never)]
213 public WebView(WebView webView) : this(Interop.WebView.NewWebView(WebView.getCPtr(webView)), true)
215 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
221 [EditorBrowsable(EditorBrowsableState.Never)]
222 public WebBackForwardList BackForwardList { get; }
227 [EditorBrowsable(EditorBrowsableState.Never)]
228 public WebContext Context { get; }
233 [EditorBrowsable(EditorBrowsableState.Never)]
234 public WebCookieManager CookieManager { get; }
239 [EditorBrowsable(EditorBrowsableState.Never)]
240 public WebSettings Settings { get; }
245 [EditorBrowsable(EditorBrowsableState.Never)]
250 return (string)GetValue(UrlProperty);
254 SetValue(UrlProperty, value);
255 NotifyPropertyChanged();
260 /// Deprecated. The cache model of the current WebView.
262 [EditorBrowsable(EditorBrowsableState.Never)]
263 public CacheModel CacheModel
267 return (CacheModel)Context.CacheModel;
271 Context.CacheModel = (WebContext.CacheModelType)value;
276 /// Deprecated. The cookie acceptance policy.
278 [EditorBrowsable(EditorBrowsableState.Never)]
279 public CookieAcceptPolicy CookieAcceptPolicy
283 return (CookieAcceptPolicy)CookieManager.CookieAcceptPolicy;
287 CookieManager.CookieAcceptPolicy = (WebCookieManager.CookieAcceptPolicyType)value;
292 /// The user agent string.
294 [EditorBrowsable(EditorBrowsableState.Never)]
295 public string UserAgent
299 return (string)GetValue(UserAgentProperty);
303 SetValue(UserAgentProperty, value);
304 NotifyPropertyChanged();
309 /// Deprecated. Whether JavaScript is enabled.
311 [EditorBrowsable(EditorBrowsableState.Never)]
312 public bool EnableJavaScript
316 return Settings.EnableJavaScript;
320 Settings.EnableJavaScript = value;
325 /// Deprecated. Whether images can be loaded automatically.
327 [EditorBrowsable(EditorBrowsableState.Never)]
328 public bool LoadImagesAutomatically
332 return Settings.AllowImagesLoadAutomatically;
336 Settings.AllowImagesLoadAutomatically = value;
341 /// The default text encoding name.<br />
342 /// e.g. "UTF-8"<br />
344 [EditorBrowsable(EditorBrowsableState.Never)]
345 public string DefaultTextEncodingName
349 return Settings.DefaultTextEncodingName;
353 Settings.DefaultTextEncodingName = value;
358 /// The default font size in pixel.
360 [EditorBrowsable(EditorBrowsableState.Never)]
361 public int DefaultFontSize
365 return Settings.DefaultFontSize;
369 Settings.DefaultFontSize = value;
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 />
377 [EditorBrowsable(EditorBrowsableState.Never)]
378 public event EventHandler<WebViewPageLoadEventArgs> PageLoadStarted
382 if (pageLoadStartedEventHandler == null)
384 pageLoadStartedCallback = (OnPageLoadStarted);
385 pageLoadStartedSignal.Connect(pageLoadStartedCallback);
387 pageLoadStartedEventHandler += value;
391 pageLoadStartedEventHandler -= value;
392 if (pageLoadStartedEventHandler == null && pageLoadStartedCallback != null)
394 pageLoadStartedSignal.Disconnect(pageLoadStartedCallback);
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 />
403 [EditorBrowsable(EditorBrowsableState.Never)]
404 public event EventHandler<WebViewPageLoadEventArgs> PageLoadFinished
408 if (pageLoadFinishedEventHandler == null)
410 pageLoadFinishedCallback = (OnPageLoadFinished);
411 pageLoadFinishedSignal.Connect(pageLoadFinishedCallback);
413 pageLoadFinishedEventHandler += value;
417 pageLoadFinishedEventHandler -= value;
418 if (pageLoadFinishedEventHandler == null && pageLoadFinishedCallback != null)
420 pageLoadFinishedSignal.Disconnect(pageLoadFinishedCallback);
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 />
429 [EditorBrowsable(EditorBrowsableState.Never)]
430 public event EventHandler<WebViewPageLoadErrorEventArgs> PageLoadError
434 if (pageLoadErrorEventHandler == null)
436 pageLoadErrorCallback = (OnPageLoadError);
437 pageLoadErrorSignal.Connect(pageLoadErrorCallback);
439 pageLoadErrorEventHandler += value;
443 pageLoadErrorEventHandler -= value;
444 if (pageLoadErrorEventHandler == null && pageLoadErrorCallback != null)
446 pageLoadErrorSignal.Disconnect(pageLoadErrorCallback);
453 /// <param name="url">The path of Web</param>
455 [EditorBrowsable(EditorBrowsableState.Never)]
456 public void LoadUrl(string url)
458 Interop.WebView.LoadUrl(SwigCPtr, url);
459 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
463 /// Deprecated. Loads a html by string.
464 /// <param name="data">The data of Web</param>
466 [EditorBrowsable(EditorBrowsableState.Never)]
467 public void LoadHTMLString(string data)
469 Interop.WebView.LoadHtmlString(SwigCPtr, data);
470 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
474 /// Loads a html by string.
475 /// <param name="data">The data of Web</param>
477 [EditorBrowsable(EditorBrowsableState.Never)]
478 public void LoadHtmlString(string data)
480 Interop.WebView.LoadHtmlString(SwigCPtr, data);
481 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
487 [EditorBrowsable(EditorBrowsableState.Never)]
490 Interop.WebView.Reload(SwigCPtr);
491 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
495 /// Stops loading the Web
497 [EditorBrowsable(EditorBrowsableState.Never)]
498 public void StopLoading()
500 Interop.WebView.StopLoading(SwigCPtr);
501 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
505 /// Suspends the operation.
507 [EditorBrowsable(EditorBrowsableState.Never)]
508 public void Suspend()
510 Interop.WebView.Suspend(SwigCPtr);
511 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
515 /// Resumes the operation after calling Suspend()
517 [EditorBrowsable(EditorBrowsableState.Never)]
520 Interop.WebView.Resume(SwigCPtr);
521 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
527 [EditorBrowsable(EditorBrowsableState.Never)]
530 Interop.WebView.GoBack(SwigCPtr);
531 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
535 /// Goes to the forward
537 [EditorBrowsable(EditorBrowsableState.Never)]
538 public void GoForward()
540 Interop.WebView.GoForward(SwigCPtr);
541 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
545 /// Returns whether backward is possible.
546 /// <returns>True if backward is possible, false otherwise</returns>
548 [EditorBrowsable(EditorBrowsableState.Never)]
549 public bool CanGoBack()
551 bool ret = Interop.WebView.CanGoBack(SwigCPtr);
552 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
557 /// Returns whether forward is possible.
558 /// <returns>True if forward is possible, false otherwise</returns>
560 [EditorBrowsable(EditorBrowsableState.Never)]
561 public bool CanGoForward()
563 bool ret = Interop.WebView.CanGoForward(SwigCPtr);
564 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
569 /// The callback function that is invoked when the message is received from the script.
571 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
572 [EditorBrowsable(EditorBrowsableState.Never)]
573 public delegate void JavaScriptMessageHandler(string message);
576 /// Evaluates JavaScript code represented as a string.
577 /// <param name="script">The JavaScript code</param>
579 [EditorBrowsable(EditorBrowsableState.Never)]
580 public void EvaluateJavaScript(string script)
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();
586 // For rooting handlers
587 internal Dictionary<string, JavaScriptMessageHandler> handlerRootMap = new Dictionary<string, JavaScriptMessageHandler>();
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>
594 [EditorBrowsable(EditorBrowsableState.Never)]
595 public void AddJavaScriptMessageHandler(string objectName, JavaScriptMessageHandler handler)
597 if (handlerRootMap.ContainsKey(objectName))
602 handlerRootMap.Add(objectName, handler);
604 System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler);
605 Interop.WebView.AddJavaScriptMessageHandler(SwigCPtr, objectName, new System.Runtime.InteropServices.HandleRef(this, ip));
607 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
611 /// Clears the history of current WebView.
613 [EditorBrowsable(EditorBrowsableState.Never)]
614 public void ClearHistory()
616 Interop.WebView.ClearHistory(SwigCPtr);
617 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
621 /// Deprecated. Clears the cache of current WebView.
623 [EditorBrowsable(EditorBrowsableState.Never)]
624 public void ClearCache()
626 Context.ClearCache();
630 /// Deprecated. Clears all the cookies of current WebView.
632 [EditorBrowsable(EditorBrowsableState.Never)]
633 public void ClearCookies()
635 CookieManager.ClearCookies();