From a76ab01e4bdaf91bbf3b2588e1a8c628a152bc6a Mon Sep 17 00:00:00 2001 From: Jiyun Yang Date: Wed, 21 Nov 2018 19:53:39 +0900 Subject: [PATCH] Change WebView API * Rename AddJavaScriptInterface to AddJavaScriptMessageHandler * Change arguments of AddJavaScriptMessageHandler * Remove RemoveJavascriptInterface Change-Id: I8bd1eba255fec8913410cac82527be53e5daa832 Signed-off-by: Jiyun Yang --- .../dali-toolkit-test-utils/toolkit-web-engine.cpp | 6 +-- .../src/dali-toolkit/utc-Dali-WebView.cpp | 8 ++-- .../devel-api/controls/web-view/web-view.cpp | 9 +---- .../devel-api/controls/web-view/web-view.h | 47 +++++++++++++--------- .../internal/controls/web-view/web-view-impl.cpp | 12 +----- .../internal/controls/web-view/web-view-impl.h | 9 +---- 6 files changed, 38 insertions(+), 53 deletions(-) diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp index 3f2797a..29f26b9 100755 --- a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp @@ -307,11 +307,7 @@ void WebEngine::EvaluateJavaScript( const std::string& script ) { } -void WebEngine::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > cb ) -{ -} - -void WebEngine::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName ) +void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler ) { } diff --git a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp index e2ee8e6..cd49363 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp @@ -265,13 +265,11 @@ int UtcDaliWebViewMethodsForCoverage(void) WebView view = WebView::New( "ko-KR", "Asia/Seoul" ); view.LoadHTMLString( "Hello World!" ); - view.AddJavaScriptInterface( "jsObject", "jsFunction", - []( const std::string& arg ) -> std::string { - return arg + " World!"; + view.AddJavaScriptMessageHandler( "jsObject", + []( const std::string& arg ) { } ); - view.EvaluateJavaScript( "jsObject.jsFunction('Hello')" ); - view.RemoveJavascriptInterface( "jsObject", "jsFunction" ); + view.EvaluateJavaScript( "jsObject.postMessage('Hello')" ); DALI_TEST_CHECK( view ); diff --git a/dali-toolkit/devel-api/controls/web-view/web-view.cpp b/dali-toolkit/devel-api/controls/web-view/web-view.cpp index 0b3698b..0d1c680 100644 --- a/dali-toolkit/devel-api/controls/web-view/web-view.cpp +++ b/dali-toolkit/devel-api/controls/web-view/web-view.cpp @@ -115,14 +115,9 @@ void WebView::EvaluateJavaScript( const std::string& script ) Dali::Toolkit::GetImpl( *this ).EvaluateJavaScript( script ); } -void WebView::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > callback ) +void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler ) { - Dali::Toolkit::GetImpl( *this ).AddJavaScriptInterface( exposedObjectName, jsFunctionName, callback ); -} - -void WebView::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName ) -{ - Dali::Toolkit::GetImpl( *this ).RemoveJavascriptInterface( exposedObjectName, jsFunctionName ); + Dali::Toolkit::GetImpl( *this ).AddJavaScriptMessageHandler( exposedObjectName, handler ); } void WebView::ClearHistory() diff --git a/dali-toolkit/devel-api/controls/web-view/web-view.h b/dali-toolkit/devel-api/controls/web-view/web-view.h index 71d05d3..8d33f3a 100644 --- a/dali-toolkit/devel-api/controls/web-view/web-view.h +++ b/dali-toolkit/devel-api/controls/web-view/web-view.h @@ -191,28 +191,37 @@ public: void GoBack(); /** - * @brief Evaluates JavaScript code represented as a string. - * - * @param[in] script The JavaScript code - */ + * @brief Evaluates JavaScript code represented as a string. + * + * @param[in] script The JavaScript code + */ void EvaluateJavaScript( const std::string& script ); /** - * @brief Adds a JavaScript interface. - * - * @param[in] exposedObjectName The name of exposed object - * @param[in] jsFunctionName The name of JavaScript function - * @param[in] callback The callback function - */ - void AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > callback ); - - /** - * @brief Removes a JavaScript interface. - * - * @param[in] exposedObjectName The name of exposed object - * @param[in] jsFunctionName The name of JavaScript function - */ - void RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName ); + * @brief Inject a JavaScript object with a message handler into the WebView. + * + * @note The injected object will appear in the JavaScript context to be loaded next. + * + * Example: + * + * 1. Native + * + * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) { + * printf( "Received a message from JS: %s", message.c_str() ); + * }); + * + * // Start WebView by loading URL + * webview.LoadUrl( url ); + * + * 2. JavaScript + * + * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!" + * + * + * @param[in] exposedObjectName The name of exposed object + * @param[in] handler The callback function + */ + void AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler ); /** * @brief Clears the history of Web. diff --git a/dali-toolkit/internal/controls/web-view/web-view-impl.cpp b/dali-toolkit/internal/controls/web-view/web-view-impl.cpp index 9cc4454..5836f42 100644 --- a/dali-toolkit/internal/controls/web-view/web-view-impl.cpp +++ b/dali-toolkit/internal/controls/web-view/web-view-impl.cpp @@ -200,19 +200,11 @@ void WebView::EvaluateJavaScript( const std::string& script ) } } -void WebView::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > callback ) +void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler ) { if ( mWebEngine ) { - mWebEngine.AddJavaScriptInterface( exposedObjectName, jsFunctionName, callback ); - } -} - -void WebView::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName ) -{ - if ( mWebEngine ) - { - mWebEngine.RemoveJavascriptInterface( exposedObjectName, jsFunctionName ); + mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler ); } } diff --git a/dali-toolkit/internal/controls/web-view/web-view-impl.h b/dali-toolkit/internal/controls/web-view/web-view-impl.h index 54a679f..2bc7ddf 100644 --- a/dali-toolkit/internal/controls/web-view/web-view-impl.h +++ b/dali-toolkit/internal/controls/web-view/web-view-impl.h @@ -113,14 +113,9 @@ public: void EvaluateJavaScript( const std::string& script ); /** - * @copydoc Dali::WebEngine::AddJavaScriptInterface() + * @copydoc Dali::WebEngine::AddJavaScriptMessageHandler() */ - void AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > callback ); - - /** - * @copydoc Dali::WebEngine::RemoveJavascriptInterface() - */ - void RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName ); + void AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler ); /** * @copydoc Dali::WebEngine::ClearHistory() -- 2.7.4