DALi Version 2.0.17
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_WEB_VIEW_H
2 #define DALI_TOOLKIT_INTERNAL_WEB_VIEW_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/web-engine.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/public-api/object/property-notification.h>
25 #include <memory>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/web-view/web-view.h>
29 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 class KeyEvent;
38 class TouchEvent;
39 class WebBackForwardList;
40 class WebContext;
41 class WebCookieManager;
42 class WebSettings;
43 class WebView;
44
45 namespace Internal
46 {
47 class WebView : public Control
48 {
49 protected:
50   WebView();
51
52   WebView(const std::string& locale, const std::string& timezoneId);
53
54   WebView(int argc, char** argv);
55
56   virtual ~WebView();
57
58 public:
59   /**
60    * @copydoc Dali::Toolkit::WebView::New()
61    */
62   static Toolkit::WebView New();
63
64   /**
65    * @copydoc Dali::Toolkit::WebView::New( const std::string&, const std::string& )
66    */
67   static Toolkit::WebView New(const std::string& locale, const std::string& timezoneId);
68
69   /**
70    * @brief Get settings of WebEngine.
71    */
72   Dali::Toolkit::WebSettings* GetSettings() const;
73
74   /**
75    * @brief Get context of WebEngine.
76    */
77   Dali::Toolkit::WebContext* GetContext() const;
78
79   /**
80    * @brief Get cookie manager of WebEngine.
81    */
82   Dali::Toolkit::WebCookieManager* GetCookieManager() const;
83
84   /**
85    * @brief Get WebBackForwardList of WebEngine.
86    */
87   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
88
89   /**
90    * @copydoc Dali::Toolkit::WebView::New( int, char** )
91    */
92   static Toolkit::WebView New(int argc, char** argv);
93
94   /**
95    * @brief Get Favicon of web page.
96    *
97    * @return Handle to a fav icon
98    */
99   Dali::Toolkit::ImageView& GetFavicon();
100
101   /**
102    * @copydoc Dali::Toolkit::WebView::LoadUrl()
103    */
104   void LoadUrl(const std::string& url);
105
106   /**
107    * @copydoc Dali::WebEngine::LoadHTMLString()
108    */
109   void LoadHtmlString(const std::string& htmlString);
110
111   /**
112    * @copydoc Dali::Toolkit::WebView::Reload()
113    */
114   void Reload();
115
116   /**
117    * @copydoc Dali::Toolkit::WebView::StopLoading()
118    */
119   void StopLoading();
120
121   /**
122    * @copydoc Dali::Toolkit::WebView::StopLoading()
123    */
124   void Suspend();
125
126   /**
127    * @copydoc Dali::Toolkit::WebView::Resume()
128    */
129   void Resume();
130
131   /**
132    * @copydoc Dali::Toolkit::WebView::ScrollBy()
133    */
134   void ScrollBy(int deltaX, int deltaY);
135
136   /**
137    * @copydoc Dali::Toolkit::WebView::CanGoForward()
138    */
139   bool CanGoForward();
140
141   /**
142    * @copydoc Dali::Toolkit::WebView::GoForward()
143    */
144   void GoForward();
145
146   /**
147    * @copydoc Dali::Toolkit::WebView::CanGoBack()
148    */
149   bool CanGoBack();
150
151   /**
152    * @copydoc Dali::Toolkit::WebView::GoBack()
153    */
154   void GoBack();
155
156   /**
157    * @copydoc Dali::Toolkit::WebView::EvaluateJavaScript()
158    */
159   void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
160
161   /**
162    * @copydoc Dali::Toolkit::WebView::AddJavaScriptMessageHandler()
163    */
164   void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
165
166   /**
167    * @copydoc Dali::Toolkit::WebView::RegisterJavaScriptAlertCallback()
168    */
169   void RegisterJavaScriptAlertCallback( Dali::WebEnginePlugin::JavaScriptAlertCallback callback );
170
171   /**
172    * @copydoc Dali::Toolkit::WebView::JavaScriptAlertReply()
173    */
174   void JavaScriptAlertReply();
175
176   /**
177    * @copydoc Dali::Toolkit::WebView::RegisterJavaScriptConfirmCallback()
178    */
179   void RegisterJavaScriptConfirmCallback( Dali::WebEnginePlugin::JavaScriptConfirmCallback callback );
180
181   /**
182    * @copydoc Dali::Toolkit::WebView::JavaScriptConfirmReply()
183    */
184   void JavaScriptConfirmReply( bool confirmed );
185
186   /**
187    * @copydoc Dali::Toolkit::WebView::RegisterJavaScriptPromptCallback()
188    */
189   void RegisterJavaScriptPromptCallback( Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
190
191   /**
192    * @copydoc Dali::Toolkit::WebView::JavaScriptPromptReply()
193    */
194   void JavaScriptPromptReply( const std::string& result );
195
196   /**
197    * @copydoc Dali::Toolkit::WebView::ClearHistory()
198    */
199   void ClearHistory();
200
201   /**
202    * @brief Clears all tiles resources of Web.
203    */
204   void ClearAllTilesResources();
205
206   /**
207    * @copydoc Dali::Toolkit::WebView::PageLoadStartedSignal()
208    */
209   Dali::Toolkit::WebView::WebViewPageLoadSignalType& PageLoadStartedSignal();
210
211   /**
212    * @copydoc Dali::Toolkit::WebView::PageLoadInProgressSignal()
213    */
214   Dali::Toolkit::WebView::WebViewPageLoadSignalType& PageLoadInProgressSignal();
215
216   /**
217    * @copydoc Dali::Toolkit::WebView::PageLoadFinishedSignal()
218    */
219   Dali::Toolkit::WebView::WebViewPageLoadSignalType& PageLoadFinishedSignal();
220
221   /**
222    * @copydoc Dali::Toolkit::WebView::PageLoadErrorSignal()
223    */
224   Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
225
226   /**
227    * @copydoc Dali::Toolkit::WebView::ScrollEdgeReachedSignal()
228    */
229   Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
230
231   /**
232    * @copydoc Dali::Toolkit::WebView::UrlChangedSignal()
233    */
234   Dali::Toolkit::WebView::WebViewUrlChangedSignalType& UrlChangedSignal();
235
236 public: // Properties
237   /**
238    * @brief Called when a property of an object of this type is set.
239    *
240    * @param[in] object The object whose property is set.
241    * @param[in] index The property index.
242    * @param[in] value The new property value.
243    */
244   static void SetProperty(Dali::BaseObject* object, Dali::Property::Index index, const Dali::Property::Value& value);
245
246   /**
247    * @brief Called to retrieve a property of an object of this type.
248    *
249    * @param[in] object The object whose property is to be retrieved.
250    * @param[in] index The property index.
251    * @return The current value of the property.
252    */
253   static Dali::Property::Value GetProperty(Dali::BaseObject* object, Dali::Property::Index propertyIndex);
254
255   /**
256    * Connects a callback function with the object's signals.
257    * @param[in] object The object providing the signal.
258    * @param[in] tracker Used to disconnect the signal.
259    * @param[in] signalName The signal to connect to.
260    * @param[in] functor A newly allocated FunctorDelegate.
261    * @return True if the signal was connected.
262    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the c
263    */
264   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
265
266 private: // From Control
267   /**
268    * @copydoc Toolkit::Control::OnInitialize()
269    */
270   void OnInitialize() override;
271
272   /**
273    * @copydoc Toolkit::Control::GetNaturalSize
274    */
275   Vector3 GetNaturalSize() override;
276
277   /**
278    * Signal occurs when the Web View has been touched.
279    * @param[in] actor The Actor Touched
280    * @param[in] touch The Touch Data.
281    * @return Whether to consume event or not.
282    */
283   bool OnTouchEvent(Actor actor, const Dali::TouchEvent& touch);
284
285   /**
286    * @copydoc Toolkit::Control::OnKeyEvent()
287    */
288   bool OnKeyEvent(const Dali::KeyEvent& event) override;
289
290   /**
291    * @copydoc Toolkit::Control::OnKeyInputFocusGained()
292    */
293   void OnKeyInputFocusGained() override;
294
295   /**
296    * @copydoc Toolkit::Control::OnKeyInputFocusLost()
297    */
298   void OnKeyInputFocusLost() override;
299
300   /**
301    * @copydoc Toolkit::Control::OnSceneConnection()
302    */
303   void OnSceneConnection(int depth) override;
304
305 private:
306   // Undefined
307   WebView(const WebView& webView);
308
309   WebView& operator=(const WebView& webView);
310
311   /**
312    * @brief Sets an absolute scroll of the given view.
313    * @param[in] x The coordinate x of scroll
314    * @param[in] y The coordinate y of scroll
315    */
316   void SetScrollPosition(int x, int y);
317
318   /**
319    * @brief Gets the current scroll position of the given view.
320    * @param[out] x The coordinate x of scroll
321    * @param[out] y The coordinate y of scroll
322    */
323   Dali::Vector2 GetScrollPosition() const;
324
325   /**
326    * @brief Gets the possible scroll size of the given view.
327    * @param[out] width The width of scroll size
328    * @param[out] height The height of scroll size
329    */
330   Dali::Vector2 GetScrollSize() const;
331
332   /**
333    * @brief Gets the last known content's size.
334    * @param[out] width The width of content's size
335    * @param[out] height The height of content's size
336    */
337   Dali::Vector2 GetContentSize() const;
338
339   /**
340    * @brief Returns the title of the Web.
341    *
342    * @return The title of web page
343    */
344   std::string GetTitle() const;
345
346   /**
347    * @brief Get user agent string.
348    * @return The string value of user agent
349    */
350   const std::string& GetUserAgent() const;
351
352   /**
353    * @brief Set user agent string.
354    * @param[in] userAgent The string value of user agent
355    */
356   void SetUserAgent(const std::string& userAgent);
357
358   /**
359    * @brief Updates display area of web view.
360    * @param[in] source The soource triggers Notification.
361    */
362   void UpdateDisplayArea(Dali::PropertyNotification& source);
363
364   /**
365    * @brief Enable/Disable video hole for video playing.
366    * @param[in] enabled True if video hole is enabled, false otherwise.
367    */
368   void EnableVideoHole(bool enabled);
369
370   /**
371    * @brief Enable blend mode.
372    * @param[in] blendEnabled True if turn on blend mode, false otherwise.
373    */
374   void EnableBlendMode(bool blendEnabled);
375
376   /**
377    * @brief Callback function to be called when page load started.
378    * @param[in] url The url currently being loaded
379    */
380   void OnPageLoadStarted(const std::string& url);
381
382   /**
383    * @brief Callback function to be called when page is loading in progress.
384    * @param[in] url The url currently being loaded
385    */
386   void OnPageLoadInProgress( const std::string& url );
387
388   /**
389    * @brief Callback function to be called when page load finished.
390    * @param[in] url The url currently being loaded
391    */
392   void OnPageLoadFinished(const std::string& url);
393
394   /**
395    * @brief Callback function to be called when there is an error in page loading.
396    * @param[in] url The url currently being loaded
397    * @param[in] errorCode The error code
398    */
399   void OnPageLoadError(const std::string& url, int errorCode);
400
401   /**
402    * @brief Callback function to be called when scroll edge is reached.
403    * @param[in] e The scroll edge reached.
404    */
405   void OnScrollEdgeReached(Dali::WebEnginePlugin::ScrollEdge edge);
406
407   /**
408    * @brief Callback function to be called when url is changed.
409    * @param[in] url The url currently being loaded
410    */
411   void OnUrlChanged( const std::string& url );
412
413 private:
414   std::string                                            mUrl;
415   Dali::Toolkit::Visual::Base                            mVisual;
416   Dali::Size                                             mWebViewSize;
417   Dali::WebEngine                                        mWebEngine;
418
419   Dali::Toolkit::WebView::WebViewPageLoadSignalType      mPageLoadStartedSignal;
420   Dali::Toolkit::WebView::WebViewPageLoadSignalType      mPageLoadInProgressSignal;
421   Dali::Toolkit::WebView::WebViewPageLoadSignalType      mPageLoadFinishedSignal;
422   Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType mPageLoadErrorSignal;
423   Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType mScrollEdgeReachedSignal;
424
425   std::unique_ptr<Dali::Toolkit::WebContext>             mWebContext;
426   std::unique_ptr<Dali::Toolkit::WebCookieManager>       mWebCookieManager;
427   std::unique_ptr<Dali::Toolkit::WebSettings>            mWebSettings;
428   std::unique_ptr<Dali::Toolkit::WebBackForwardList>     mWebBackForwardList;
429   Dali::Toolkit::ImageView mFaviconView;
430
431   Dali::PropertyNotification                             mPositionUpdateNotification;
432   Dali::PropertyNotification                             mSizeUpdateNotification;
433   Dali::PropertyNotification                             mScaleUpdateNotification;
434   bool                                                   mVideoHoleEnabled;
435   Dali::Rect< int >                                      mWebViewArea;
436   Dali::Toolkit::WebView::WebViewUrlChangedSignalType    mUrlChangedSignal;
437 };
438
439 } // namespace Internal
440
441 inline Toolkit::Internal::WebView& GetImpl(Toolkit::WebView& handle)
442 {
443   DALI_ASSERT_ALWAYS(handle);
444   Dali::RefObject& impl = handle.GetImplementation();
445   return static_cast<Toolkit::Internal::WebView&>(impl);
446 }
447
448 inline const Toolkit::Internal::WebView& GetImpl(const Toolkit::WebView& handle)
449 {
450   DALI_ASSERT_ALWAYS(handle);
451   const Dali::RefObject& impl = handle.GetImplementation();
452   return static_cast<const Toolkit::Internal::WebView&>(impl);
453 }
454
455 } // namespace Toolkit
456
457 } // namespace Dali
458
459 #endif // DALI_TOOLKIT_INTERNAL_WEB_VIEW_H