Changeset 161527 in webkit
- Timestamp:
- Jan 8, 2014 5:07:05 PM (10 years ago)
- Location:
- trunk/Source/WebKit2
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit2/ChangeLog
r161525 r161527 1 2014-01-08 Ryuan Choi <ryuan.choi@samsung.com> 2 3 [EFL][WK2] Add API to execute js script 4 https://bugs.webkit.org/show_bug.cgi?id=101904 5 6 Reviewed by Gyuyoung Kim. 7 8 Add ewk_view_script_execute() which provides a way to execute user 9 script. 10 11 * UIProcess/API/efl/ewk_context.cpp: 12 Added JS Global Contexta which is required to deserialize to the script 13 value from the callback. 14 (EwkContext::EwkContext): 15 (EwkContext::~EwkContext): 16 (EwkContext::jsGlobalContext): 17 * UIProcess/API/efl/ewk_context_private.h: 18 * UIProcess/API/efl/ewk_view.cpp: 19 (Ewk_View_Script_Execute_Callback_Context::Ewk_View_Script_Execute_Callback_Context): 20 (runJavaScriptCallback): 21 (ewk_view_script_execute): 22 * UIProcess/API/efl/ewk_view.h: 23 * UIProcess/API/efl/tests/test_ewk2_view.cpp: 24 Added unit test for ewk_view_script_execute. 25 (scriptExecuteCallback): 26 (TEST_F): 27 1 28 2014-01-08 Gyuyoung Kim <gyuyoung.kim@samsung.com> 2 29 -
trunk/Source/WebKit2/UIProcess/API/efl/ewk_context.cpp
r159190 r161527 40 40 #include "ewk_storage_manager_private.h" 41 41 #include "ewk_url_scheme_request_private.h" 42 #include <JavaScriptCore/JSContextRef.h> 42 43 #include <WebCore/FileSystem.h> 43 44 #include <WebCore/IconDatabase.h> … … 73 74 , m_requestManagerClient(std::make_unique<RequestManagerClientEfl>(context)) 74 75 , m_historyClient(std::make_unique<ContextHistoryClientEfl>(context)) 76 , m_jsGlobalContext(nullptr) 75 77 { 76 78 ContextMap::AddResult result = contextMap().add(context, this); … … 97 99 { 98 100 ASSERT(contextMap().get(m_context.get()) == this); 101 102 if (m_jsGlobalContext) 103 JSGlobalContextRelease(m_jsGlobalContext); 104 99 105 contextMap().remove(m_context.get()); 100 106 } … … 213 219 { 214 220 WKResourceCacheManagerClearCacheForAllOrigins(WKContextGetResourceCacheManager(m_context.get()), WKResourceCachesToClearAll); 221 } 222 223 224 JSGlobalContextRef EwkContext::jsGlobalContext() 225 { 226 if (!m_jsGlobalContext) 227 m_jsGlobalContext = JSGlobalContextCreate(0); 228 229 return m_jsGlobalContext; 215 230 } 216 231 -
trunk/Source/WebKit2/UIProcess/API/efl/ewk_context_private.h
r159190 r161527 23 23 #include "ewk_context.h" 24 24 #include "ewk_object_private.h" 25 #include <JavaScriptCore/JSContextRef.h> 25 26 #include <WebKit2/WKBase.h> 26 27 #include <WebKit2/WKRetainPtr.h> … … 86 87 void clearResourceCache(); 87 88 89 JSGlobalContextRef jsGlobalContext(); 90 88 91 private: 89 92 explicit EwkContext(WKContextRef); … … 107 110 108 111 std::unique_ptr<WebKit::ContextHistoryClientEfl> m_historyClient; 112 113 JSGlobalContextRef m_jsGlobalContext; 109 114 }; 110 115 -
trunk/Source/WebKit2/UIProcess/API/efl/ewk_view.cpp
r159190 r161527 41 41 #include "ewk_settings_private.h" 42 42 #include <Ecore_Evas.h> 43 #include <JavaScriptCore/JSRetainPtr.h> 43 44 #include <WebKit2/WKAPICast.h> 44 45 #include <WebKit2/WKData.h> … … 48 49 #include <WebKit2/WKPageGroup.h> 49 50 #include <WebKit2/WKRetainPtr.h> 51 #include <WebKit2/WKSerializedScriptValue.h> 50 52 #include <WebKit2/WKString.h> 51 53 #include <WebKit2/WKURL.h> … … 626 628 return WKViewGetShowsAsSource(impl->wkView()); 627 629 } 630 631 struct Ewk_View_Script_Execute_Callback_Context { 632 Ewk_View_Script_Execute_Callback_Context(Ewk_View_Script_Execute_Cb callback, Evas_Object* ewkView, void* userData) 633 : m_callback(callback) 634 , m_view(ewkView) 635 , m_userData(userData) 636 { 637 } 638 639 Ewk_View_Script_Execute_Cb m_callback; 640 Evas_Object* m_view; 641 void* m_userData; 642 }; 643 644 static void runJavaScriptCallback(WKSerializedScriptValueRef scriptValue, WKErrorRef, void* context) 645 { 646 ASSERT(context); 647 648 auto callbackContext = std::unique_ptr<Ewk_View_Script_Execute_Callback_Context>(static_cast<Ewk_View_Script_Execute_Callback_Context*>(context)); 649 ASSERT(callbackContext->m_view); 650 651 if (!callbackContext->m_callback) 652 return; 653 654 if (scriptValue) { 655 EWK_VIEW_IMPL_GET_OR_RETURN(callbackContext->m_view, impl); 656 JSGlobalContextRef jsGlobalContext = impl->ewkContext()->jsGlobalContext(); 657 658 JSValueRef value = WKSerializedScriptValueDeserialize(scriptValue, jsGlobalContext, 0); 659 JSRetainPtr<JSStringRef> jsStringValue(Adopt, JSValueToStringCopy(jsGlobalContext, value, 0)); 660 size_t length = JSStringGetMaximumUTF8CStringSize(jsStringValue.get()); 661 auto buffer = std::make_unique<char[]>(length); 662 JSStringGetUTF8CString(jsStringValue.get(), buffer.get(), length); 663 callbackContext->m_callback(callbackContext->m_view, buffer.get(), callbackContext->m_userData); 664 } else 665 callbackContext->m_callback(callbackContext->m_view, 0, callbackContext->m_userData); 666 } 667 668 Eina_Bool ewk_view_script_execute(Evas_Object* ewkView, const char* script, Ewk_View_Script_Execute_Cb callback, void* userData) 669 { 670 EWK_VIEW_IMPL_GET_OR_RETURN(ewkView, impl, false); 671 EINA_SAFETY_ON_NULL_RETURN_VAL(script, false); 672 673 Ewk_View_Script_Execute_Callback_Context* context = new Ewk_View_Script_Execute_Callback_Context(callback, ewkView, userData); 674 WKRetainPtr<WKStringRef> scriptString(AdoptWK, WKStringCreateWithUTF8CString(script)); 675 WKPageRunJavaScriptInMainFrame(impl->wkPage(), scriptString.get(), context, runJavaScriptCallback); 676 return true; 677 } -
trunk/Source/WebKit2/UIProcess/API/efl/ewk_view.h
r157502 r161527 280 280 281 281 /** 282 * @typedef Ewk_View_Script_Execute_Cb Ewk_View_Script_Execute_Cb 283 * @brief Callback type for use with ewk_view_script_execute() 284 */ 285 typedef void (*Ewk_View_Script_Execute_Cb)(Evas_Object *o, const char *return_value, void *user_data); 286 287 /** 282 288 * Creates a type name for the callback function used to get the page contents. 283 289 * … … 885 891 EAPI Eina_Bool ewk_view_source_mode_get(const Evas_Object *o); 886 892 893 /** 894 * Requests execution of the given script. 895 * 896 * The result value for the execution can be retrieved from the asynchronous callback. 897 * 898 * @param o The view to execute script 899 * @param script JavaScript to execute 900 * @param callback The function to call when the execution is completed, may be @c NULL 901 * @param user_data User data, may be @c NULL 902 * 903 * @return @c EINA_TRUE on success or @c EINA_FALSE on failure 904 */ 905 EAPI Eina_Bool ewk_view_script_execute(Evas_Object *o, const char *script, Ewk_View_Script_Execute_Cb callback, void *user_data); 906 887 907 #ifdef __cplusplus 888 908 } -
trunk/Source/WebKit2/UIProcess/API/efl/tests/test_ewk2_view.cpp
r161478 r161527 31 31 static bool fullScreenCallbackCalled; 32 32 static bool obtainedPageContents = false; 33 static bool scriptExecuteCallbackCalled; 33 34 34 35 static struct { … … 1034 1035 eina_stringshare_del(defaultUserAgent); 1035 1036 } 1037 1038 static void scriptExecuteCallback(Evas_Object*, const char* returnValue, void* userData) 1039 { 1040 Eina_Strbuf* buffer = static_cast<Eina_Strbuf*>(userData); 1041 eina_strbuf_reset(buffer); 1042 1043 if (returnValue) 1044 eina_strbuf_append(buffer, returnValue); 1045 1046 scriptExecuteCallbackCalled = true; 1047 } 1048 1049 TEST_F(EWK2UnitTestBase, ewk_view_script_execute) 1050 { 1051 const char scriptExecuteHTML[] = 1052 "<!DOCTYPE html>" 1053 "<body>" 1054 "<p id=\"TestContent\">test content</p>" 1055 "</body>"; 1056 1057 ewk_view_html_string_load(webView(), scriptExecuteHTML, 0, 0); 1058 ASSERT_TRUE(waitUntilLoadFinished()); 1059 1060 Eina_Strbuf* result = eina_strbuf_new(); 1061 1062 // 1. Get the innerHTML for "TestContent" 1063 const char getDataScript[] = "document.getElementById('TestContent').innerHTML"; 1064 1065 scriptExecuteCallbackCalled = false; 1066 ASSERT_TRUE(ewk_view_script_execute(webView(), getDataScript, scriptExecuteCallback, static_cast<void*>(result))); 1067 waitUntilTrue(scriptExecuteCallbackCalled); 1068 ASSERT_STREQ("test content", eina_strbuf_string_get(result)); 1069 1070 // 2. Change the innerHTML for "TestContent" 1071 const char changeDataScript[] = 1072 "document.getElementById('TestContent').innerHTML = \"test\";"; 1073 ASSERT_TRUE(ewk_view_script_execute(webView(), changeDataScript, 0, 0)); 1074 1075 // 3. Check the change of the innerHTML. 1076 eina_strbuf_reset(result); 1077 scriptExecuteCallbackCalled = false; 1078 ASSERT_TRUE(ewk_view_script_execute(webView(), getDataScript, scriptExecuteCallback, static_cast<void*>(result))); 1079 waitUntilTrue(scriptExecuteCallbackCalled); 1080 ASSERT_STREQ("test", eina_strbuf_string_get(result)); 1081 1082 eina_strbuf_free(result); 1083 1084 ASSERT_FALSE(ewk_view_script_execute(webView(), 0, 0, 0)); 1085 }
Note: See TracChangeset
for help on using the changeset viewer.