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