Merge "Add some APIs into web view." into devel/master
[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) 2020 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 <memory>
23 #include <dali/devel-api/adaptor-framework/web-engine.h>
24 #include <dali/public-api/images/image-operations.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/controls/web-view/web-view.h>
28 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 class KeyEvent;
39 class TouchEvent;
40 class WebBackForwardList;
41 class WebContext;
42 class WebCookieManager;
43 class WebSettings;
44 class WebView;
45
46 namespace Internal
47 {
48
49 class WebView : public Control
50 {
51 protected:
52
53   WebView();
54
55   WebView( const std::string& locale, const std::string& timezoneId );
56
57   WebView( int argc, char** argv );
58
59   virtual ~WebView();
60
61 public:
62
63   /**
64    * @copydoc Dali::Toolkit::WebView::New()
65    */
66   static Toolkit::WebView New();
67
68   /**
69    * @copydoc Dali::Toolkit::WebView::New( const std::string&, const std::string& )
70    */
71   static Toolkit::WebView New( const std::string& locale, const std::string& timezoneId );
72
73   /**
74    * @brief Get settings of WebEngine.
75    */
76   Dali::Toolkit::WebSettings* GetSettings() const;
77
78   /**
79    * @brief Get context of WebEngine.
80    */
81   Dali::Toolkit::WebContext* GetContext() const;
82
83   /**
84    * @brief Get cookie manager of WebEngine.
85    */
86   Dali::Toolkit::WebCookieManager* GetCookieManager() const;
87
88   /**
89    * @brief Get WebBackForwardList of WebEngine.
90    */
91   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
92
93   /**
94    * @copydoc Dali::Toolkit::WebView::New( int, char** )
95    */
96   static Toolkit::WebView New( int argc, char** argv );
97
98   /**
99    * @brief Get Favicon of web page.
100    *
101    * @return Handle to a fav icon
102    */
103   Dali::Toolkit::ImageView& GetFavicon();
104
105   /**
106    * @copydoc Dali::Toolkit::WebView::LoadUrl()
107    */
108   void LoadUrl( const std::string& url );
109
110   /**
111    * @copydoc Dali::WebEngine::LoadHTMLString()
112    */
113   void LoadHtmlString( const std::string& htmlString );
114
115   /**
116    * @copydoc Dali::Toolkit::WebView::Reload()
117    */
118   void Reload();
119
120   /**
121    * @copydoc Dali::Toolkit::WebView::StopLoading()
122    */
123   void StopLoading();
124
125   /**
126    * @copydoc Dali::Toolkit::WebView::StopLoading()
127    */
128   void Suspend();
129
130   /**
131    * @copydoc Dali::Toolkit::WebView::Resume()
132    */
133   void Resume();
134
135   /**
136    * @copydoc Dali::Toolkit::WebView::ScrollBy()
137    */
138   void ScrollBy( int deltaX, int deltaY );
139
140   /**
141    * @copydoc Dali::Toolkit::WebView::CanGoForward()
142    */
143   bool CanGoForward();
144
145   /**
146    * @copydoc Dali::Toolkit::WebView::GoForward()
147    */
148   void GoForward();
149
150   /**
151    * @copydoc Dali::Toolkit::WebView::CanGoBack()
152    */
153   bool CanGoBack();
154
155   /**
156    * @copydoc Dali::Toolkit::WebView::GoBack()
157    */
158   void GoBack();
159
160   /**
161    * @copydoc Dali::Toolkit::WebView::EvaluateJavaScript()
162    */
163   void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler );
164
165   /**
166    * @copydoc Dali::Toolkit::WebView::AddJavaScriptMessageHandler()
167    */
168   void AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler );
169
170   /**
171    * @brief Clears all tiles resources of Web.
172    */
173   void ClearAllTilesResources();
174
175   /**
176    * @copydoc Dali::Toolkit::WebView::ClearHistory()
177    */
178   void ClearHistory();
179
180   /**
181    * @copydoc Dali::Toolkit::WebView::PageLoadStartedSignal()
182    */
183   Dali::Toolkit::WebView::WebViewPageLoadSignalType& PageLoadStartedSignal();
184
185   /**
186    * @copydoc Dali::Toolkit::WebView::PageLoadFinishedSignal()
187    */
188   Dali::Toolkit::WebView::WebViewPageLoadSignalType& PageLoadFinishedSignal();
189
190   /**
191    * @copydoc Dali::Toolkit::WebView::PageLoadErrorSignal()
192    */
193   Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
194
195   /**
196    * @copydoc Dali::Toolkit::WebView::ScrollEdgeReachedSignal()
197    */
198   Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
199
200 public: // Properties
201
202   /**
203    * @brief Called when a property of an object of this type is set.
204    *
205    * @param[in] object The object whose property is set.
206    * @param[in] index The property index.
207    * @param[in] value The new property value.
208    */
209   static void SetProperty( Dali::BaseObject* object, Dali::Property::Index index, const Dali::Property::Value& value );
210
211   /**
212    * @brief Called to retrieve a property of an object of this type.
213    *
214    * @param[in] object The object whose property is to be retrieved.
215    * @param[in] index The property index.
216    * @return The current value of the property.
217    */
218   static Dali::Property::Value GetProperty( Dali::BaseObject* object, Dali::Property::Index propertyIndex );
219
220   /**
221    * Connects a callback function with the object's signals.
222    * @param[in] object The object providing the signal.
223    * @param[in] tracker Used to disconnect the signal.
224    * @param[in] signalName The signal to connect to.
225    * @param[in] functor A newly allocated FunctorDelegate.
226    * @return True if the signal was connected.
227    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the c
228    */
229   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
230
231 private: // From Control
232
233   /**
234    * @copydoc Toolkit::Control::OnInitialize()
235    */
236   void OnInitialize() override;
237
238   /**
239    * @copydoc Toolkit::Control::GetNaturalSize
240    */
241   Vector3 GetNaturalSize() override;
242
243   /**
244    * @copydoc Toolkit::Control::OnRelayout()
245    */
246   void OnRelayout( const Vector2& size, RelayoutContainer& container ) override;
247
248   /**
249    * Signal occurs when the Web View has been touched.
250    * @param[in] actor The Actor Touched
251    * @param[in] touch The Touch Data.
252    * @return Whether to consume event or not.
253    */
254   bool OnTouchEvent( Actor actor, const Dali::TouchEvent& touch );
255
256   /**
257    * @copydoc Toolkit::Control::OnKeyEvent()
258    */
259   bool OnKeyEvent( const Dali::KeyEvent& event ) override;
260
261   /**
262    * @copydoc Toolkit::Control::OnKeyInputFocusGained()
263    */
264   void OnKeyInputFocusGained() override;
265
266   /**
267    * @copydoc Toolkit::Control::OnKeyInputFocusLost()
268    */
269   void OnKeyInputFocusLost() override;
270
271 private:
272
273   // Undefined
274   WebView( const WebView& webView );
275
276   WebView& operator=( const WebView& webView );
277
278   /**
279    * @brief Sets an absolute scroll of the given view.
280    * @param[in] x The coordinate x of scroll
281    * @param[in] y The coordinate y of scroll
282    */
283   void SetScrollPosition( int x, int y );
284
285   /**
286    * @brief Gets the current scroll position of the given view.
287    * @param[out] x The coordinate x of scroll
288    * @param[out] y The coordinate y of scroll
289    */
290   void GetScrollPosition( int& x, int& y ) const;
291
292   /**
293    * @brief Gets the possible scroll size of the given view.
294    * @param[out] width The width of scroll size
295    * @param[out] height The height of scroll size
296    */
297   void GetScrollSize( int& width, int& height ) const;
298
299   /**
300    * @brief Gets the last known content's size.
301    * @param[out] width The width of content's size
302    * @param[out] height The height of content's size
303    */
304   void GetContentSize( int& width, int& height ) const;
305
306   /**
307    * @brief Returns the title of the Web.
308    *
309    * @return The title of web page
310    */
311   std::string GetTitle() const;
312
313   /**
314    * @brief Get user agent string.
315    * @return The string value of user agent
316    */
317   const std::string& GetUserAgent() const;
318
319   /**
320    * @brief Set user agent string.
321    * @param[in] userAgent The string value of user agent
322    */
323   void SetUserAgent( const std::string& userAgent );
324
325   /**
326    * @brief Callback function to be called when page load started.
327    * @param[in] url The url currently being loaded
328    */
329   void OnPageLoadStarted( const std::string& url );
330
331   /**
332    * @brief Callback function to be called when page load finished.
333    * @param[in] url The url currently being loaded
334    */
335   void OnPageLoadFinished( const std::string& url );
336
337   /**
338    * @brief Callback function to be called when there is an error in page loading.
339    * @param[in] url The url currently being loaded
340    * @param[in] errorCode The error code
341    */
342   void OnPageLoadError( const std::string& url, int errorCode );
343
344   /**
345    * @brief Callback function to be called when scroll edge is reached.
346    * @param[in] e The scroll edge reached.
347    */
348   void OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge );
349
350 private:
351
352   std::string                                            mUrl;
353   Dali::Toolkit::Visual::Base                            mVisual;
354   Dali::Size                                             mWebViewSize;
355   Dali::WebEngine                                        mWebEngine;
356
357   Dali::Toolkit::WebView::WebViewPageLoadSignalType      mPageLoadStartedSignal;
358   Dali::Toolkit::WebView::WebViewPageLoadSignalType      mPageLoadFinishedSignal;
359   Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType mPageLoadErrorSignal;
360   Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType mScrollEdgeReachedSignal;
361
362   std::unique_ptr<Dali::Toolkit::WebContext>             mWebContext;
363   std::unique_ptr<Dali::Toolkit::WebCookieManager>       mWebCookieManager;
364   std::unique_ptr<Dali::Toolkit::WebSettings>            mWebSettings;
365   std::unique_ptr<Dali::Toolkit::WebBackForwardList>     mWebBackForwardList;
366   Dali::Toolkit::ImageView mFaviconView;
367 };
368
369 } // namespace Internal
370
371 inline Toolkit::Internal::WebView& GetImpl( Toolkit::WebView& handle )
372 {
373   DALI_ASSERT_ALWAYS( handle );
374   Dali::RefObject& impl = handle.GetImplementation();
375   return static_cast< Toolkit::Internal::WebView& >( impl );
376 }
377
378 inline const Toolkit::Internal::WebView& GetImpl( const Toolkit::WebView& handle )
379 {
380   DALI_ASSERT_ALWAYS( handle );
381   const Dali::RefObject& impl = handle.GetImplementation();
382   return static_cast< const Toolkit::Internal::WebView& >( impl );
383 }
384
385 } // namespace Toolkit
386
387 } // namespace Dali
388
389 #endif // DALI_TOOLKIT_INTERNAL_WEB_VIEW_H