From c317eb428a9a935c29f252542324983bef7baba7 Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Thu, 11 Jun 2015 17:28:02 +0800 Subject: [PATCH] fix webview crash in android --- ...b_cocos2dx_experimental_webView_manual.cpp | 113 ++++++++++-------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp b/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp index 9e57d3f3c0..a134f9e171 100644 --- a/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp +++ b/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp @@ -20,20 +20,23 @@ static bool jsb_cocos2dx_experimental_webView_setOnShouldStartLoading(JSContext JS::CallArgs args = JS::CallArgsFromVp(argc, vp); std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnShouldStartLoading([=](experimental::ui::WebView *sender, const std::string &url)->bool{ - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] + { + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } - return true; + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); }); return true; } @@ -50,19 +53,23 @@ static bool jsb_cocos2dx_experimental_webView_setOnDidFinishLoading(JSContext *c JS::CallArgs args = JS::CallArgsFromVp(argc, vp); std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnDidFinishLoading([=](experimental::ui::WebView *sender, const std::string &url)->void{ - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] + { + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); }); return true; } @@ -79,19 +86,23 @@ static bool jsb_cocos2dx_experimental_webView_setOnDidFailLoading(JSContext *cx, JS::CallArgs args = JS::CallArgsFromVp(argc, vp); std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnDidFailLoading([=](experimental::ui::WebView *sender, const std::string &url)->void{ - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] + { + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); }); return true; } @@ -108,19 +119,23 @@ static bool jsb_cocos2dx_experimental_webView_setOnJSCallback(JSContext *cx, uin JS::CallArgs args = JS::CallArgsFromVp(argc, vp); std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnJSCallback([=](experimental::ui::WebView *sender, const std::string &url)->void{ - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] + { + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); }); return true; }