fix ui::WebView callback operate OpenGL issue.

All the OpenGL operations should be in OpenGL thread.
This PR wraps the callbacks in OpenGL thread.
This commit is contained in:
zilongshanren 2015-09-23 11:22:36 +08:00
parent 2560b5fc6f
commit bee52e378d
3 changed files with 31 additions and 5 deletions

View File

@ -58,29 +58,51 @@ public class Cocos2dxWebView extends WebView {
class Cocos2dxWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
Cocos2dxActivity activity = (Cocos2dxActivity)getContext();
final String finalizedUrlString = urlString;
try {
URI uri = URI.create(urlString);
if (uri != null && uri.getScheme().equals(mJSScheme)) {
Cocos2dxWebViewHelper._onJsCallback(mViewTag, urlString);
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebViewHelper._onJsCallback(mViewTag, finalizedUrlString);
}
});
return true;
}
} catch (Exception e) {
Log.d(TAG, "Failed to create URI from url");
}
return Cocos2dxWebViewHelper._shouldStartLoading(mViewTag, urlString);
return Cocos2dxWebViewHelper._shouldStartLoading(mViewTag, finalizedUrlString);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Cocos2dxWebViewHelper._didFinishLoading(mViewTag, url);
Cocos2dxActivity activity = (Cocos2dxActivity)getContext();
final String finalizedUrl = url;
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebViewHelper._didFinishLoading(mViewTag, finalizedUrl);
}
});
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Cocos2dxWebViewHelper._didFailLoading(mViewTag, failingUrl);
Cocos2dxActivity activity = (Cocos2dxActivity)getContext();
final String finalizedUrl = failingUrl;
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebViewHelper._didFailLoading(mViewTag, finalizedUrl);
}
});
}
}

View File

@ -147,6 +147,7 @@ public:
/**
* Call before a web view begins loading.
* Note: Any OpenGL related operations are forbidden in this callback.
*
* @param callback The web view that is about to load new content.
* @return YES if the web view should begin loading content; otherwise, NO .

View File

@ -141,6 +141,7 @@ bool WebViewTest::init()
evalJsBtn->addClickEventListener([=](Ref*){
_webView->evaluateJS("alert(\"hello\")");
});
evalJsBtn->setName("evalJs");
this->addChild(evalJsBtn);
return true;
@ -151,12 +152,14 @@ bool WebViewTest::init()
bool WebViewTest::onWebViewShouldStartLoading(experimental::ui::WebView *sender, const std::string &url)
{
CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str());
//don't do any OpenGL operation here!! It's forbidden!
return true;
}
void WebViewTest::onWebViewDidFinishLoading(experimental::ui::WebView *sender, const std::string &url)
{
auto node = (ui::Button*)this->getChildByName("evalJs");
node->setTitleText("start loading...");
CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str());
}