Fix crash in web view on android

This commit is contained in:
dimon4eg 2015-10-15 16:43:53 +03:00
parent e789ab97be
commit ee3c000b43
1 changed files with 57 additions and 26 deletions

View File

@ -35,8 +35,11 @@
#include "UIWebView.h"
#include "platform/CCGLView.h"
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include "platform/CCFileUtils.h"
#include "ui/UIHelper.h"
#include <mutex>
#include <condition_variable>
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxWebViewHelper"
@ -401,18 +404,39 @@ namespace cocos2d {
setScalesPageToFitJNI(_viewTag, scalesPageToFit);
}
// executed from Java's thread (we should invoke callback on cocos thread)
bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) {
std::mutex m;
std::condition_variable cv;
bool finished = false;
bool allowLoad = true;
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&]{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end()) {
auto webView = s_WebViewImpls[viewTag]->_webView;
if (webView->_onShouldStartLoading) {
return webView->_onShouldStartLoading(webView, url);
allowLoad = webView->_onShouldStartLoading(webView, url);
}
}
return true;
}
m.lock();
finished = true;
m.unlock();
cv.notify_one();
});
// wait for result from cocos thread
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [&finished]{ return finished; });
return allowLoad;
}
// executed from Java's thread (we should invoke callback on cocos thread)
void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){
Director::getInstance()->getScheduler()->performFunctionInCocosThread([viewTag, url]{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end()) {
auto webView = s_WebViewImpls[viewTag]->_webView;
@ -420,9 +444,12 @@ namespace cocos2d {
webView->_onDidFinishLoading(webView, url);
}
}
});
}
// executed from Java's thread (we should invoke callback on cocos thread)
void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){
Director::getInstance()->getScheduler()->performFunctionInCocosThread([viewTag, url]{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end()) {
auto webView = s_WebViewImpls[viewTag]->_webView;
@ -430,9 +457,12 @@ namespace cocos2d {
webView->_onDidFailLoading(webView, url);
}
}
});
}
// executed from Java's thread (we should invoke callback on cocos thread)
void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){
Director::getInstance()->getScheduler()->performFunctionInCocosThread([viewTag, message]{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end()) {
auto webView = s_WebViewImpls[viewTag]->_webView;
@ -440,6 +470,7 @@ namespace cocos2d {
webView->_onJSCallback(webView, message);
}
}
});
}
void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) {