summaryrefslogtreecommitdiff
path: root/src/Tizen.WebView/Tizen.WebView/WebView.cs
blob: 3b30cd8a8aff6a5f3755828894c76c5ad9200125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using ElmSharp;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Tizen.WebView
{
    /// <summary>
    /// A view used to render web contents.
    /// </summary>
    public class WebView: EvasObject
    {
        private static IDictionary<string, JavaScriptMessageHandler> _javaScriptMessageHandlerMap = new Dictionary<string, JavaScriptMessageHandler>();

        private IntPtr _handle;
        private IntPtr _realHandle;
        private Context _context;
        private Settings _settings;

        // focus dummy
        private SmartEvent _focusIn;
        private SmartEvent _focusOut;

        // Smart events
        private SmartEvent _loadStarted;
        private SmartEvent _loadFinished;
        private SmartEvent<SmartCallbackLoadErrorArgs> _loadError;
        private SmartEvent<SmartCallbackArgs> _titleChanged;
        private SmartEvent<SmartCallbackArgs> _urlChanged;



        /// <summary>
        /// Event that occurs when load started.
        /// </summary>
        public event EventHandler LoadStarted;

        /// <summary>
        /// Event that occurs when load finished.
        /// </summary>
        public event EventHandler LoadFinished;

        /// <summary>
        /// Event that occurs when load error.
        /// </summary>
        public event EventHandler<SmartCallbackLoadErrorArgs> LoadError;

        /// <summary>
        /// Event that occurs when title of main frame was changed.
        /// </summary>
        public event EventHandler<SmartCallbackArgs> TitleChanged;

        /// <summary>
        /// Event that occurs when URL of main frame was changed.
        /// </summary>
        public event EventHandler<SmartCallbackArgs> UrlChanged;

        /// <summary>
        /// Current URL of the main frame.
        /// </summary>
        public string Url
        {
            get
            {
                return Interop.ChromiumEwk.ewk_view_url_get(_realHandle);
            }
        }

        /// <summary>
        /// Current title of the main frame.
        /// </summary>
        public string Title
        {
            get
            {
                return Interop.ChromiumEwk.ewk_view_title_get(_realHandle);
            }
        }

        /// <summary>
        /// Current user agent string of this view.
        /// </summary>
        public string UserAgent
        {
            get
            {
                return Interop.ChromiumEwk.ewk_view_user_agent_get(_realHandle);
            }

            set
            {
                Interop.ChromiumEwk.ewk_view_user_agent_set(_realHandle, value);
            }
        }

        /// <summary>
        /// Whether a view has the focus.
        /// </summary>
        public bool HasFocus
        {
            get
            {
                return Interop.ChromiumEwk.ewk_view_focus_get(_realHandle);
            }
        }

        /// <summary>
        /// Create a WebView object.
        /// </summary>
        /// <param name="parent">Parent object of WebView</param>
        public WebView(EvasObject parent) : base(parent)
        {
            InitializeSmartEvent();
        }

        /// <summary>
        /// Gets the Context object of this view.
        /// </summary>
        /// <returns>The Context object of this view</returns>
        public Context GetContext()
        {
            if (_context == null)
            {
                IntPtr contextHandle = Interop.ChromiumEwk.ewk_view_context_get(_realHandle);
                if (contextHandle == IntPtr.Zero)
                {
                    return null;
                }
                _context = new Context(contextHandle);
            }
            return _context;
        }

        /// <summary>
        /// Gets the Settings object of this view.
        /// </summary>
        /// <returns>The Settings object of this view</returns>
        public Settings GetSettings()
        {
            if (_settings == null)
            {
                IntPtr settingsHandle = Interop.ChromiumEwk.ewk_view_settings_get(_realHandle);
                if (settingsHandle == IntPtr.Zero)
                {
                    return null;
                }
                _settings = new Settings(settingsHandle);
            }
            return _settings;
        }

        /// <summary>
        /// Asks the object to load the given URL.
        /// </summary>
        /// <remarks>
        /// You can only be sure that url changed after UrlChanged event.
        /// </remarks>
        /// <param name="url">The uniform resource identifier to load</param>
        public void LoadUrl(string url)
        {
            Interop.ChromiumEwk.ewk_view_url_set(_realHandle, url);
        }

        /// <summary>
        /// Loads the specified html string as the content of the view.
        /// </summary>
        /// <param name="html">HTML data to load</param>
        /// <param name="baseUrl">Base URL used for relative paths to external objects</param>
        public void LoadHtml(string html, string baseUrl)
        {
            Interop.ChromiumEwk.ewk_view_html_string_load(_realHandle, html, baseUrl, null);
        }

        /// <summary>
        /// Asks the main frame to stop loading.
        /// </summary>
        public void StopLoading()
        {
            Interop.ChromiumEwk.ewk_view_stop(_realHandle);
        }

        /// <summary>
        /// Asks the main frame to reload the current document.
        /// </summary>
        public void Reload()
        {
            Interop.ChromiumEwk.ewk_view_reload(_realHandle);
        }

        /// <summary>
        /// Asks the main frame to navigate back in history.
        /// </summary>
        public void GoBack()
        {
            Interop.ChromiumEwk.ewk_view_back(_realHandle);
        }

        /// <summary>
        /// Asks the main frame to navigate forward in history.
        /// </summary>
        public void GoForward()
        {
            Interop.ChromiumEwk.ewk_view_forward(_realHandle);
        }

        /// <summary>
        /// Checks whether it is possible to navigate backwards one item in history.
        /// </summary>
        /// <returns>Whether it is possible to navigate backwards one item in history</returns>
        public bool CanGoBack()
        {
            return Interop.ChromiumEwk.ewk_view_back_possible(_realHandle);
        }

        /// <summary>
        /// Checks whether it is possible to navigate forwards one item in history.
        /// </summary>
        /// <returns>Whether it is possible to navigate forwards one item in history</returns>
        public bool CanGoForward()
        {
            return Interop.ChromiumEwk.ewk_view_forward_possible(_realHandle);
        }

        /// <summary>
        /// Injects the supplied javascript message handler into the view.
        /// </summary>
        /// <param name="name"> The message callback</param>
        /// <param name="handler">The name used to expose the object in JavaScript</param>
        /// <returns>'true' on success, otherwise 'false'</returns>
        public bool AddJavaScriptMessageHandler(string name, JavaScriptMessageHandler handler)
        {
            lock (_javaScriptMessageHandlerMap)
            {
                if (_javaScriptMessageHandlerMap.ContainsKey(name))
                {
                    return false;
                }
                _javaScriptMessageHandlerMap[name] = handler;
            }
            Interop.ChromiumEwk.ScriptMessageCallback callback = (handle, message) =>
            {
                JavaScriptMessage convertedMessage = new JavaScriptMessage(message);
                lock (_javaScriptMessageHandlerMap)
                {
                    if (_javaScriptMessageHandlerMap.ContainsKey(convertedMessage.Name))
                    {
                        _javaScriptMessageHandlerMap[convertedMessage.Name](convertedMessage);
                    }
                }
            };
            if (!Interop.ChromiumEwk.ewk_view_javascript_message_handler_add(_realHandle, callback, name))
            {
                lock (_javaScriptMessageHandlerMap)
                {
                    _javaScriptMessageHandlerMap.Remove(name);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Requests the execution of given name and result to the JavaScript runtime.
        /// </summary>
        /// <param name="name">The name used to expose the object in JavaScript</param>
        /// <param name="result">The result to the JavaScript runtime</param>
        public void EvalWithResult(string name, string result)
        {
            Interop.ChromiumEwk.ewk_view_evaluate_javascript(_realHandle, name, result);
        }

        /// <summary>
        /// Requests the execution of the given script.
        /// </summary>
        /// <param name="script">The JavaScript code string to execute</param>
        public void Eval(string script)
        {
            Interop.ChromiumEwk.ewk_view_script_execute(_realHandle, script, null, IntPtr.Zero);
        }

        /// <summary>
        /// Requests to set or unset a view as the currently focused one.
        /// </summary>
        /// <param name="focused">'true' to set the focus on the view, 'false' to remove the focus from the view</param>
        public void SetFocus(bool focused)
        {
            Interop.ChromiumEwk.ewk_view_focus_set(_realHandle, focused);
        }

        protected override IntPtr CreateHandle(EvasObject parent)
        {
            // focus dummy
            _handle = Interop.Elementary.elm_layout_add((IntPtr)parent);
            Interop.Elementary.elm_layout_theme_set(_handle, "layout", "elm_widget", "default");
            Interop.Elementary.elm_object_focus_allow_set(_handle, true);

            IntPtr evas = Interop.Evas.evas_object_evas_get(parent);
            _realHandle = Interop.ChromiumEwk.ewk_view_add(evas);
            Interop.Elementary.elm_object_part_content_set(_handle, "elm.swallow.content", _realHandle);

            return _handle;
        }

        private void InitializeSmartEvent()
        {
            // focus dummy
            _focusIn = new SmartEvent(this, "focused");
            _focusOut = new SmartEvent(this, "unfocused");

            _focusIn.On += (s, e) => { ((WebView)s).SetFocus(true); };
            _focusOut.On += (s, e) => { ((WebView)s).SetFocus(false); };

            _loadStarted = new SmartEvent(this, _realHandle, "load,started");
            _loadFinished = new SmartEvent(this, _realHandle, "load,finished");
            _loadError = new SmartEvent<SmartCallbackLoadErrorArgs>(this, _realHandle, "load,error", SmartCallbackLoadErrorArgs.CreateFromSmartEvent);
            _titleChanged = new SmartEvent<SmartCallbackArgs>(this, _realHandle, "title,changed", SmartCallbackArgs.CreateFromSmartEvent);
            _urlChanged = new SmartEvent<SmartCallbackArgs>(this, _realHandle, "url,changed", SmartCallbackArgs.CreateFromSmartEvent);

            _loadStarted.On += (s, e) => { LoadStarted?.Invoke(this, EventArgs.Empty); };
            _loadFinished.On += (s, e) => { LoadFinished?.Invoke(this, EventArgs.Empty); };
            _loadError.On += (s, e) => { LoadError?.Invoke(this, e); };
            _titleChanged.On += (s, e) => { TitleChanged?.Invoke(this, e); };
            _urlChanged.On += (s, e) => { UrlChanged?.Invoke(this, e); };
        }
    }
}