Changeset 161527 in webkit


Ignore:
Timestamp:
Jan 8, 2014 5:07:05 PM (10 years ago)
Author:
ryuan.choi@samsung.com
Message:

[EFL][WK2] Add API to execute js script
https://bugs.webkit.org/show_bug.cgi?id=101904

Reviewed by Gyuyoung Kim.

Add ewk_view_script_execute() which provides a way to execute user
script.

  • UIProcess/API/efl/ewk_context.cpp:

Added JS Global Contexta which is required to deserialize to the script
value from the callback.
(EwkContext::EwkContext):
(EwkContext::~EwkContext):
(EwkContext::jsGlobalContext):

  • UIProcess/API/efl/ewk_context_private.h:
  • UIProcess/API/efl/ewk_view.cpp:

(Ewk_View_Script_Execute_Callback_Context::Ewk_View_Script_Execute_Callback_Context):
(runJavaScriptCallback):
(ewk_view_script_execute):

  • UIProcess/API/efl/ewk_view.h:
  • UIProcess/API/efl/tests/test_ewk2_view.cpp:

Added unit test for ewk_view_script_execute.
(scriptExecuteCallback):
(TEST_F):

Location:
trunk/Source/WebKit2
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r161525 r161527  
     12014-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
    1282014-01-08  Gyuyoung Kim  <gyuyoung.kim@samsung.com>
    229
  • trunk/Source/WebKit2/UIProcess/API/efl/ewk_context.cpp

    r159190 r161527  
    4040#include "ewk_storage_manager_private.h"
    4141#include "ewk_url_scheme_request_private.h"
     42#include <JavaScriptCore/JSContextRef.h>
    4243#include <WebCore/FileSystem.h>
    4344#include <WebCore/IconDatabase.h>
     
    7374    , m_requestManagerClient(std::make_unique<RequestManagerClientEfl>(context))
    7475    , m_historyClient(std::make_unique<ContextHistoryClientEfl>(context))
     76    , m_jsGlobalContext(nullptr)
    7577{
    7678    ContextMap::AddResult result = contextMap().add(context, this);
     
    9799{
    98100    ASSERT(contextMap().get(m_context.get()) == this);
     101
     102    if (m_jsGlobalContext)
     103        JSGlobalContextRelease(m_jsGlobalContext);
     104
    99105    contextMap().remove(m_context.get());
    100106}
     
    213219{
    214220    WKResourceCacheManagerClearCacheForAllOrigins(WKContextGetResourceCacheManager(m_context.get()), WKResourceCachesToClearAll);
     221}
     222
     223
     224JSGlobalContextRef EwkContext::jsGlobalContext()
     225{
     226    if (!m_jsGlobalContext)
     227        m_jsGlobalContext = JSGlobalContextCreate(0);
     228
     229    return m_jsGlobalContext;
    215230}
    216231
  • trunk/Source/WebKit2/UIProcess/API/efl/ewk_context_private.h

    r159190 r161527  
    2323#include "ewk_context.h"
    2424#include "ewk_object_private.h"
     25#include <JavaScriptCore/JSContextRef.h>
    2526#include <WebKit2/WKBase.h>
    2627#include <WebKit2/WKRetainPtr.h>
     
    8687    void clearResourceCache();
    8788
     89    JSGlobalContextRef jsGlobalContext();
     90
    8891private:
    8992    explicit EwkContext(WKContextRef);
     
    107110
    108111    std::unique_ptr<WebKit::ContextHistoryClientEfl> m_historyClient;
     112
     113    JSGlobalContextRef m_jsGlobalContext;
    109114};
    110115
  • trunk/Source/WebKit2/UIProcess/API/efl/ewk_view.cpp

    r159190 r161527  
    4141#include "ewk_settings_private.h"
    4242#include <Ecore_Evas.h>
     43#include <JavaScriptCore/JSRetainPtr.h>
    4344#include <WebKit2/WKAPICast.h>
    4445#include <WebKit2/WKData.h>
     
    4849#include <WebKit2/WKPageGroup.h>
    4950#include <WebKit2/WKRetainPtr.h>
     51#include <WebKit2/WKSerializedScriptValue.h>
    5052#include <WebKit2/WKString.h>
    5153#include <WebKit2/WKURL.h>
     
    626628    return WKViewGetShowsAsSource(impl->wkView());
    627629}
     630
     631struct 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
     644static 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
     668Eina_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  
    280280
    281281/**
     282 * @typedef Ewk_View_Script_Execute_Cb Ewk_View_Script_Execute_Cb
     283 * @brief Callback type for use with ewk_view_script_execute()
     284 */
     285typedef void (*Ewk_View_Script_Execute_Cb)(Evas_Object *o, const char *return_value, void *user_data);
     286
     287/**
    282288 * Creates a type name for the callback function used to get the page contents.
    283289 *
     
    885891EAPI Eina_Bool ewk_view_source_mode_get(const Evas_Object *o);
    886892
     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 */
     905EAPI Eina_Bool ewk_view_script_execute(Evas_Object *o, const char *script, Ewk_View_Script_Execute_Cb callback, void *user_data);
     906
    887907#ifdef __cplusplus
    888908}
  • trunk/Source/WebKit2/UIProcess/API/efl/tests/test_ewk2_view.cpp

    r161478 r161527  
    3131static bool fullScreenCallbackCalled;
    3232static bool obtainedPageContents = false;
     33static bool scriptExecuteCallbackCalled;
    3334
    3435static struct {
     
    10341035    eina_stringshare_del(defaultUserAgent);
    10351036}
     1037
     1038static 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
     1049TEST_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.