Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add webview jsbbinding #1662

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion frameworks/js-bindings/bindings/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ LOCAL_SRC_FILES := auto/jsb_cocos2dx_auto.cpp \
auto/jsb_cocos2dx_ui_auto.cpp \
auto/jsb_cocos2dx_3d_auto.cpp \
auto/jsb_cocos2dx_3d_extension_auto.cpp \
auto/jsb_cocos2dx_experimental_webView_auto.cpp \
manual/ScriptingCore.cpp \
manual/cocos2d_specifics.cpp \
manual/js_manual_conversions.cpp \
Expand All @@ -41,7 +42,8 @@ LOCAL_SRC_FILES := auto/jsb_cocos2dx_auto.cpp \
manual/network/XMLHTTPRequest.cpp \
manual/spine/jsb_cocos2dx_spine_manual.cpp \
manual/ui/jsb_cocos2dx_ui_manual.cpp \
manual/3d/jsb_cocos2dx_3d_manual.cpp
manual/3d/jsb_cocos2dx_3d_manual.cpp \
manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp


LOCAL_CFLAGS := -DCOCOS2D_JAVASCRIPT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "jsb_cocos2dx_experimental_webView_manual.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include "UIWebView.h"
#include "ScriptingCore.h"
#include "cocos2d_specifics.hpp"

using namespace cocos2d;


static bool jsb_cocos2dx_experimental_webView_setOnShouldStartLoading(JSContext *cx, uint32_t argc, jsval *vp)
{
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy = jsb_get_js_proxy(obj);
experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");

if(argc == 1){
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
std::shared_ptr<JSFunctionWrapper> 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);

bool ok = func->invoke(2, arg, &rval);
if (!ok && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
return true;
});
return true;
}
}

static bool jsb_cocos2dx_experimental_webView_setOnDidFinishLoading(JSContext *cx, uint32_t argc, jsval *vp)
{
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy = jsb_get_js_proxy(obj);
experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");

if(argc == 1){
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
std::shared_ptr<JSFunctionWrapper> 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);

bool ok = func->invoke(2, arg, &rval);
if (!ok && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
});
return true;
}
}

static bool jsb_cocos2dx_experimental_webView_setOnDidFailLoading(JSContext *cx, uint32_t argc, jsval *vp)
{
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy = jsb_get_js_proxy(obj);
experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");

if(argc == 1){
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
std::shared_ptr<JSFunctionWrapper> 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);

bool ok = func->invoke(2, arg, &rval);
if (!ok && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
});
return true;
}
}

static bool jsb_cocos2dx_experimental_webView_setOnJSCallback(JSContext *cx, uint32_t argc, jsval *vp)
{
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy = jsb_get_js_proxy(obj);
experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");

if(argc == 1){
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
std::shared_ptr<JSFunctionWrapper> 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);

bool ok = func->invoke(2, arg, &rval);
if (!ok && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
});
return true;
}
}
extern JSObject* jsb_cocos2d_experimental_ui_WebView_prototype;

void register_all_cocos2dx_experimental_webView_manual(JSContext* cx, JS::HandleObject global)
{
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnShouldStartLoading", jsb_cocos2dx_experimental_webView_setOnShouldStartLoading, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnDidFinishLoading", jsb_cocos2dx_experimental_webView_setOnDidFinishLoading, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnDidFailLoading", jsb_cocos2dx_experimental_webView_setOnDidFailLoading, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnJSCallback", jsb_cocos2dx_experimental_webView_setOnJSCallback, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __jsb_cocos2dx_experimental_webView_manual__
#define __jsb_cocos2dx_experimental_webView_manual__

#include "jsapi.h"
#include "jsfriendapi.h"

void register_all_cocos2dx_experimental_webView_manual(JSContext* cx, JS::HandleObject global);

#endif /* defined(__jsb_cocos2dx_experimental_webView_manual__) */
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@
420BBCF81AA48EE900493976 /* jsb_cocos2dx_3d_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 420BBCF51AA48EE900493976 /* jsb_cocos2dx_3d_manual.cpp */; };
420BBCF91AA48EE900493976 /* jsb_cocos2dx_3d_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 420BBCF61AA48EE900493976 /* jsb_cocos2dx_3d_manual.h */; };
420BBCFA1AA48EE900493976 /* jsb_cocos2dx_3d_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 420BBCF61AA48EE900493976 /* jsb_cocos2dx_3d_manual.h */; };
4BE089E41ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */; };
4BE089E51ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */; };
4BE089E61ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BE089E21ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp */; };
4BE089E71ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4BE089E31ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp */; };
4BE089EB1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BE089E91ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp */; };
4BE089EC1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */; };
83A5661918DA878400FC31A0 /* jsb_socketio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83A5661718DA878400FC31A0 /* jsb_socketio.cpp */; };
83A5661A18DA878400FC31A0 /* jsb_socketio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83A5661718DA878400FC31A0 /* jsb_socketio.cpp */; };
83A5661B18DA878400FC31A0 /* jsb_socketio.h in Headers */ = {isa = PBXBuildFile; fileRef = 83A5661818DA878400FC31A0 /* jsb_socketio.h */; };
Expand Down Expand Up @@ -256,6 +262,12 @@
420BBCEF1AA48EDE00493976 /* jsb_cocos2dx_3d_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_3d_auto.hpp; sourceTree = "<group>"; };
420BBCF51AA48EE900493976 /* jsb_cocos2dx_3d_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_3d_manual.cpp; sourceTree = "<group>"; };
420BBCF61AA48EE900493976 /* jsb_cocos2dx_3d_manual.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_cocos2dx_3d_manual.h; sourceTree = "<group>"; };
4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_webView_auto.cpp; sourceTree = "<group>"; };
4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_experimental_webView_auto.hpp; sourceTree = "<group>"; };
4BE089E21ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_video_auto.cpp; sourceTree = "<group>"; };
4BE089E31ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_experimental_video_auto.hpp; sourceTree = "<group>"; };
4BE089E91ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_webView_manual.cpp; sourceTree = "<group>"; };
4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_cocos2dx_experimental_webView_manual.h; sourceTree = "<group>"; };
83A5661718DA878400FC31A0 /* jsb_socketio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_socketio.cpp; sourceTree = "<group>"; };
83A5661818DA878400FC31A0 /* jsb_socketio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_socketio.h; sourceTree = "<group>"; };
BA4095C01A6F730A005E53F6 /* jsb_cocos2dx_studio_conversions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_studio_conversions.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -323,6 +335,10 @@
1A119E2E18BDF19200352BAA /* auto */ = {
isa = PBXGroup;
children = (
4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */,
4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */,
4BE089E21ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp */,
4BE089E31ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp */,
420BBCEE1AA48EDE00493976 /* jsb_cocos2dx_3d_auto.cpp */,
420BBCEF1AA48EDE00493976 /* jsb_cocos2dx_3d_auto.hpp */,
BAEE4D6F1AC3FFAD003BEB0F /* jsb_cocos2dx_3d_extension_auto.cpp */,
Expand All @@ -349,6 +365,7 @@
1A119E4118BDF19200352BAA /* manual */ = {
isa = PBXGroup;
children = (
4BE089E81ADF967400D65D4B /* experimental */,
420BBCF41AA48EE900493976 /* 3d */,
0541A74C1973876100E45470 /* ios */,
1A119E4218BDF19200352BAA /* chipmunk */,
Expand Down Expand Up @@ -525,6 +542,15 @@
path = 3d;
sourceTree = "<group>";
};
4BE089E81ADF967400D65D4B /* experimental */ = {
isa = PBXGroup;
children = (
4BE089E91ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp */,
4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */,
);
path = experimental;
sourceTree = "<group>";
};
BA623DFB191A192700761F37 /* pluginx */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -604,6 +630,7 @@
1A119F0218BDF19200352BAA /* ScriptingCore.h in Headers */,
1A119EBE18BDF19200352BAA /* cocos2d_specifics.hpp in Headers */,
83A5661C18DA878400FC31A0 /* jsb_socketio.h in Headers */,
4BE089E71ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp in Headers */,
1A1D3B7B18C44FD000922D3C /* jsb_event_dispatcher_manual.h in Headers */,
1A119EF618BDF19200352BAA /* js_bindings_system_registration.h in Headers */,
BA4095C51A6F730A005E53F6 /* jsb_cocos2dx_studio_conversions.h in Headers */,
Expand All @@ -614,6 +641,7 @@
1A119EFA18BDF19200352BAA /* jsb_websocket.h in Headers */,
1A119E9218BDF19200352BAA /* jsb_cocos2dx_extension_auto.hpp in Headers */,
1A119E8C18BDF19200352BAA /* jsb_cocos2dx_builder_auto.hpp in Headers */,
4BE089E51ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp in Headers */,
BA623E19191A196F00761F37 /* jsb_cocos2dx_pluginx_auto.hpp in Headers */,
1A119EAA18BDF19200352BAA /* js_bindings_chipmunk_auto_classes.h in Headers */,
1A119E9E18BDF19200352BAA /* jsb_cocos2dx_spine_auto.hpp in Headers */,
Expand All @@ -640,6 +668,7 @@
420BBCF31AA48EDE00493976 /* jsb_cocos2dx_3d_auto.hpp in Headers */,
1A119EBA18BDF19200352BAA /* js_bindings_chipmunk_registration.h in Headers */,
1A119EB018BDF19200352BAA /* js_bindings_chipmunk_functions.h in Headers */,
4BE089EC1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h in Headers */,
BA623E0E191A195F00761F37 /* jsb_pluginx_manual_callback.h in Headers */,
1A119EB218BDF19200352BAA /* js_bindings_chipmunk_functions_registration.h in Headers */,
1A119EF218BDF19200352BAA /* js_bindings_system_functions_registration.h in Headers */,
Expand Down Expand Up @@ -768,8 +797,10 @@
1A119EEE18BDF19200352BAA /* js_bindings_system_functions.cpp in Sources */,
1A119EB418BDF19200352BAA /* js_bindings_chipmunk_manual.cpp in Sources */,
1A119EEA18BDF19200352BAA /* jsb_opengl_registration.cpp in Sources */,
4BE089E61ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp in Sources */,
1A119EF818BDF19200352BAA /* jsb_websocket.cpp in Sources */,
1A119ED418BDF19200352BAA /* js_bindings_core.cpp in Sources */,
4BE089EB1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp in Sources */,
420BBCF11AA48EDE00493976 /* jsb_cocos2dx_3d_auto.cpp in Sources */,
1A119EC218BDF19200352BAA /* js_bindings_ccbreader.cpp in Sources */,
1A119EFC18BDF19200352BAA /* XMLHTTPRequest.cpp in Sources */,
Expand All @@ -778,6 +809,7 @@
1A119ED818BDF19200352BAA /* js_bindings_opengl.cpp in Sources */,
1AB5E62C18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.cpp in Sources */,
1A119E9C18BDF19200352BAA /* jsb_cocos2dx_spine_auto.cpp in Sources */,
4BE089E41ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp in Sources */,
1A119EF418BDF19200352BAA /* js_bindings_system_registration.cpp in Sources */,
1A119EBC18BDF19200352BAA /* cocos2d_specifics.cpp in Sources */,
1A119EE618BDF19200352BAA /* jsb_opengl_manual.cpp in Sources */,
Expand Down
1 change: 1 addition & 0 deletions samples/js-tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"src/GUITest/UITextFieldTest/UITextFieldTest.js",
"src/GUITest/UIRichTextTest/UIRichTextTest.js",
"src/GUITest/UITextTest/UITextTest.js",
"src/GUITest/UIWebViewTest/UIWebViewTest.js",

"src/CocoStudioTest/SceneTest/TriggerCode/Acts.js",
"src/CocoStudioTest/SceneTest/TriggerCode/Cons.js",
Expand Down
10 changes: 10 additions & 0 deletions samples/js-tests/project/Classes/AppDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
#include "js_Effect3D_bindings.h"
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "jsb_cocos2dx_experimental_webView_auto.hpp"
#include "experimental/jsb_cocos2dx_experimental_webView_manual.h"
#endif

USING_NS_CC;
USING_NS_CC_EXT;
using namespace CocosDenshion;
Expand Down Expand Up @@ -126,6 +131,11 @@ bool AppDelegate::applicationDidFinishLaunching()
sc->addRegisterCallback(register_Effect3D_bindings);
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
sc->addRegisterCallback(register_all_cocos2dx_experimental_webView);
sc->addRegisterCallback(register_all_cocos2dx_experimental_webView_manual);
#endif

sc->start();
sc->runScript("script/jsb_boot.js");
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
Expand Down
5 changes: 5 additions & 0 deletions samples/js-tests/res/cocosui/Test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body style="font-size:100px;">
Hello World
</body>
</html>
8 changes: 8 additions & 0 deletions samples/js-tests/src/GUITest/UISceneManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
var currentTestingArray = null;

var testingItems = {
"UIWebViewTest": [
{
title: "UIWebViewTest",
func: function () {
return new UIWebViewTest();
}
}
],
"UIButton": [
{
title: "UIButtonTest",
Expand Down
Loading