4 using Tizen.NUI.BaseComponents;
5 using Tizen.NUI.Components;
9 namespace Tizen.NUI.Samples
11 using tlog = Tizen.Log;
12 public class WebViewInterceptTest : IExample
14 const string tag = "NUITEST";
17 private Button btn1, btn2, btn3;
18 private WebView webView1, webView2;
19 private string invalidUrl = "https://test/";
21 public void Activate()
23 win = NUIApplication.GetDefaultWindow();
27 Size = new Size(win.Size.Width, win.Size.Height, 0),
28 BackgroundColor = Color.Green,
29 Layout = new LinearLayout()
31 LinearOrientation = LinearLayout.Orientation.Vertical,
32 Padding = new Extents(3, 3, 3, 3),
37 webView1 = new WebView();
38 webView1.Url = "https://m.naver.com/";
39 webView1.WidthSpecification = LayoutParamPolicies.MatchParent;
40 webView1.HeightSpecification = LayoutParamPolicies.MatchParent;
43 webView2 = new WebView();
44 webView2.Url = "https://m.google.com/";
45 webView2.WidthSpecification = LayoutParamPolicies.MatchParent;
46 webView2.HeightSpecification = LayoutParamPolicies.MatchParent;
51 HeightSpecification = 50,
52 WidthSpecification = 300,
53 Text = "register intercept callback",
55 btn1.Clicked += (s, e) =>
57 webView1.Context.RegisterHttpRequestInterceptedCallback(Callback);
58 // only this Callback2 will be invoked for all created WebViews, because the Context is global(Callback will be ignored).
59 webView2.Context.RegisterHttpRequestInterceptedCallback(Callback2);
65 HeightSpecification = 50,
66 WidthSpecification = 300,
67 Text = "WebView1 load invalid url",
69 btn2.Clicked += (s, e) =>
71 webView1.LoadUrl(invalidUrl);
77 HeightSpecification = 50,
78 WidthSpecification = 300,
79 Text = "WebView2 load invalid url",
81 btn3.Clicked += (s, e) =>
83 webView2.LoadUrl(invalidUrl);
89 private void Callback(WebHttpRequestInterceptor interceptor)
91 tlog.Debug(tag, $"callback: http request intercepted start, Url: {interceptor.Url}");
93 //interceptor.Ignore();
94 if (interceptor.Url.Equals(invalidUrl))
96 byte[] bData = Encoding.UTF8.GetBytes("<html><body><img src='test.jpg'></body></html>");
97 interceptor.SetResponseStatus(200, "OK");
98 interceptor.AddResponseHeader("Content-Type", "text/html; charset=UTF-8");
99 interceptor.AddResponseHeader("Content-Length", bData.Length.ToString());
100 interceptor.SetResponseBody(bData);
101 tlog.Debug(tag, $"http request intercepted set response body end");
103 else if (interceptor.Url.Equals($"{invalidUrl}test.jpg"))
105 string path = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "soundcloud.png";
106 using (FileStream fs = File.OpenRead(path))
108 byte[] bData = new byte[1024];
109 while (fs.Read(bData, 0, bData.Length) > 0)
111 interceptor.WriteResponseChunk(bData);
113 interceptor.WriteResponseChunk((byte[])null);
114 tlog.Debug(tag, $"http request intercepted write chunk end");
118 if (interceptor.InterceptedWebView != null)
120 tlog.Debug(tag, $"http request intercepted web view is not null");
121 if(webView2 == interceptor.InterceptedWebView)
123 tlog.Debug(tag, $"InterceptedWebView is webView2!");
125 else if(webView1 == interceptor.InterceptedWebView)
127 tlog.Debug(tag, $"InterceptedWebView is webView1!");
131 tlog.Debug(tag, $"InterceptedWebView is not either webView1 or webView2");
135 tlog.Debug(tag, $"http request intercepted end");
136 tlog.Debug(tag, $"");
139 private void Callback2(WebHttpRequestInterceptor interceptor)
141 tlog.Debug(tag, $"callback2: http request intercepted start, Url: {interceptor.Url}");
143 //interceptor.Ignore();
144 if (interceptor.Url.Equals(invalidUrl))
146 byte[] bData = Encoding.UTF8.GetBytes("<html><body><img src='test.jpg'></body></html>");
147 interceptor.SetResponseStatus(200, "OK");
148 interceptor.AddResponseHeader("Content-Type", "text/html; charset=UTF-8");
149 interceptor.AddResponseHeader("Content-Length", bData.Length.ToString());
150 interceptor.SetResponseBody(bData);
151 tlog.Debug(tag, $"http request intercepted set response body end");
153 else if (interceptor.Url.Equals($"{invalidUrl}test.jpg"))
155 string path = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "netflix.png";
157 if (interceptor.InterceptedWebView == webView2)
159 path = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "hbo.png";
162 using (FileStream fs = File.OpenRead(path))
164 byte[] bData = new byte[1024];
165 while (fs.Read(bData, 0, bData.Length) > 0)
167 interceptor.WriteResponseChunk(bData);
169 interceptor.WriteResponseChunk((byte[])null);
170 tlog.Debug(tag, $"http request intercepted write chunk end");
174 if (interceptor.InterceptedWebView != null)
176 tlog.Debug(tag, $"http request intercepted web view is not null");
177 if (webView2 == interceptor.InterceptedWebView)
179 tlog.Debug(tag, $"InterceptedWebView is webView2!");
181 else if (webView1 == interceptor.InterceptedWebView)
183 tlog.Debug(tag, $"InterceptedWebView is webView1!");
187 tlog.Debug(tag, $"InterceptedWebView is not either webView1 or webView2");
191 tlog.Debug(tag, $"http request intercepted end");
192 tlog.Debug(tag, $"");
195 public void Deactivate()