axmol/cocos/ui/UIWebView/UIWebViewImpl-android.cpp

382 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-07-15 23:03:43 +08:00
Copyright (c) 2021 Bytedance Inc.
https://adxe.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ui/UIWebView/UIWebViewImpl-android.h"
#include <unordered_map>
#include <stdlib.h>
#include <string>
#include "platform/android/jni/JniHelper.h"
#include "ui/UIWebView/UIWebView.h"
#include "platform/CCGLView.h"
#include "base/CCDirector.h"
#include "platform/CCFileUtils.h"
#include "ui/UIHelper.h"
2021-07-15 23:03:43 +08:00
#include "yasio/cxx17/string_view.hpp"
2019-11-23 20:27:39 +08:00
static const std::string className = "org.cocos2dx.lib.Cocos2dxWebViewHelper";
2021-12-25 10:04:45 +08:00
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "", __VA_ARGS__)
2019-11-23 20:27:39 +08:00
static const std::string s_defaultBaseUrl = "file:///android_asset/";
2021-12-25 10:04:45 +08:00
static const std::string s_sdRootBaseUrl = "file://";
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
static std::string getFixedBaseUrl(std::string_view baseUrl)
2019-11-23 20:27:39 +08:00
{
std::string fixedBaseUrl;
if (baseUrl.empty())
{
fixedBaseUrl = s_defaultBaseUrl;
}
2021-12-25 10:04:45 +08:00
else if (baseUrl.find(s_sdRootBaseUrl) != std::string::npos)
2019-11-23 20:27:39 +08:00
{
fixedBaseUrl = baseUrl;
}
2021-12-25 10:04:45 +08:00
else if (baseUrl.c_str()[0] != '/')
{
using namespace cxx17; // for cxx17::string_view literal
if (cxx20::starts_with(cxx17::string_view{baseUrl}, "assets/"_sv))
{
2019-11-23 20:27:39 +08:00
fixedBaseUrl = s_defaultBaseUrl + baseUrl.c_str()[7];
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
fixedBaseUrl = s_defaultBaseUrl + baseUrl;
}
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
fixedBaseUrl = s_sdRootBaseUrl + baseUrl;
}
2021-12-25 10:04:45 +08:00
if (fixedBaseUrl.c_str()[fixedBaseUrl.length() - 1] != '/')
{
2019-11-23 20:27:39 +08:00
fixedBaseUrl += "/";
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return fixedBaseUrl;
}
extern "C" {
2021-12-25 10:04:45 +08:00
/*
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
* Method: shouldStartLoading
* Signature: (ILjava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv* env,
jclass,
jint index,
jstring jurl)
{
auto charUrl = env->GetStringUTFChars(jurl, NULL);
std::string url = charUrl;
env->ReleaseStringUTFChars(jurl, charUrl);
return cocos2d::ui::WebViewImpl::shouldStartLoading(index, url);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/*
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
* Method: didFinishLoading
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv* env,
jclass,
jint index,
jstring jurl)
{
// LOGD("didFinishLoading");
auto charUrl = env->GetStringUTFChars(jurl, NULL);
std::string url = charUrl;
env->ReleaseStringUTFChars(jurl, charUrl);
cocos2d::ui::WebViewImpl::didFinishLoading(index, url);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/*
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
* Method: didFailLoading
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv* env,
jclass,
jint index,
jstring jurl)
{
// LOGD("didFailLoading");
auto charUrl = env->GetStringUTFChars(jurl, NULL);
std::string url = charUrl;
env->ReleaseStringUTFChars(jurl, charUrl);
cocos2d::ui::WebViewImpl::didFailLoading(index, url);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/*
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
* Method: onJsCallback
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv* env,
jclass,
jint index,
jstring jmessage)
{
// LOGD("jsCallback");
auto charMessage = env->GetStringUTFChars(jmessage, NULL);
std::string message = charMessage;
env->ReleaseStringUTFChars(jmessage, charMessage);
cocos2d::ui::WebViewImpl::onJsCallback(index, message);
}
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
namespace
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
int createWebViewJNI()
{
2019-11-23 20:27:39 +08:00
cocos2d::JniMethodInfo t;
2021-12-25 10:04:45 +08:00
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), "createWebView", "()I"))
{
2019-11-23 20:27:39 +08:00
// LOGD("error: %s,%d",__func__,__LINE__);
jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
return viewTag;
}
return -1;
}
2021-12-26 23:26:34 +08:00
std::string getUrlStringByFileName(std::string_view fileName)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
// LOGD("error: %s,%d",__func__,__LINE__);
const std::string basePath("file:///android_asset/");
2021-07-15 23:03:43 +08:00
2021-12-25 10:04:45 +08:00
using namespace cxx17; // for cxx17::string_view literal
std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
2019-11-23 20:27:39 +08:00
std::string urlString;
2021-12-25 10:04:45 +08:00
if (fullPath.empty())
{
return urlString;
2021-12-25 10:04:45 +08:00
}
else if (fullPath[0] == '/')
{
urlString.append("file://").append(fullPath);
2021-12-25 10:04:45 +08:00
}
else if (cxx20::starts_with(cxx17::string_view{fullPath}, "assets/"_sv))
{
2021-07-15 23:03:43 +08:00
urlString = fullPath;
2021-12-25 10:04:45 +08:00
}
else
{
urlString.append(basePath).append(fullPath);
}
2021-07-15 23:03:43 +08:00
2019-11-23 20:27:39 +08:00
return urlString;
}
2021-12-25 10:04:45 +08:00
} // namespace
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace cocos2d
{
namespace ui
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static std::unordered_map<int, cocos2d::ui::WebViewImpl*> s_WebViewImpls;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
WebViewImpl::WebViewImpl(WebView* webView) : _viewTag(-1), _webView(webView)
{
_viewTag = createWebViewJNI();
s_WebViewImpls[_viewTag] = this;
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
WebViewImpl::~WebViewImpl()
{
JniHelper::callStaticVoidMethod(className, "removeWebView", _viewTag);
s_WebViewImpls.erase(_viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::loadData(const Data& data,
2021-12-26 23:26:34 +08:00
std::string_view MIMEType,
std::string_view encoding,
std::string_view baseURL)
2021-12-25 10:04:45 +08:00
{
std::string dataString(reinterpret_cast<char*>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, dataString, MIMEType, encoding,
baseURL);
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::loadHTMLString(std::string_view string, std::string_view baseURL)
2021-12-25 10:04:45 +08:00
{
JniHelper::callStaticVoidMethod(className, "loadHTMLString", _viewTag, string, getFixedBaseUrl(baseURL));
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::loadURL(std::string_view url)
2021-12-25 10:04:45 +08:00
{
this->loadURL(url, false);
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::loadURL(std::string_view url, bool cleanCachedData)
2021-12-25 10:04:45 +08:00
{
JniHelper::callStaticVoidMethod(className, "loadUrl", _viewTag, url, cleanCachedData);
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::loadFile(std::string_view fileName)
2021-12-25 10:04:45 +08:00
{
auto fullPath = getUrlStringByFileName(fileName);
JniHelper::callStaticVoidMethod(className, "loadFile", _viewTag, fullPath);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::stopLoading()
{
JniHelper::callStaticVoidMethod(className, "stopLoading", _viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::reload()
{
JniHelper::callStaticVoidMethod(className, "reload", _viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool WebViewImpl::canGoBack()
{
return JniHelper::callStaticBooleanMethod(className, "canGoBack", _viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool WebViewImpl::canGoForward()
{
return JniHelper::callStaticBooleanMethod(className, "canGoForward", _viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::goBack()
{
JniHelper::callStaticVoidMethod(className, "goBack", _viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::goForward()
{
JniHelper::callStaticVoidMethod(className, "goForward", _viewTag);
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::setJavascriptInterfaceScheme(std::string_view scheme)
2021-12-25 10:04:45 +08:00
{
JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, scheme);
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::evaluateJS(std::string_view js)
2021-12-25 10:04:45 +08:00
{
JniHelper::callStaticVoidMethod(className, "evaluateJS", _viewTag, js);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit)
{
JniHelper::callStaticVoidMethod(className, "setScalesPageToFit", _viewTag, scalesPageToFit);
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
bool WebViewImpl::shouldStartLoading(const int viewTag, std::string_view url)
2021-12-25 10:04:45 +08:00
{
bool allowLoad = true;
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end())
{
auto webView = it->second->_webView;
if (webView->_onShouldStartLoading)
{
allowLoad = webView->_onShouldStartLoading(webView, url);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
return allowLoad;
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::didFinishLoading(const int viewTag, std::string_view url)
2021-12-25 10:04:45 +08:00
{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end())
{
auto webView = it->second->_webView;
if (webView->_onDidFinishLoading)
{
webView->_onDidFinishLoading(webView, url);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::didFailLoading(const int viewTag, std::string_view url)
2021-12-25 10:04:45 +08:00
{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end())
{
auto webView = it->second->_webView;
if (webView->_onDidFailLoading)
{
webView->_onDidFailLoading(webView, url);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void WebViewImpl::onJsCallback(const int viewTag, std::string_view message)
2021-12-25 10:04:45 +08:00
{
auto it = s_WebViewImpls.find(viewTag);
if (it != s_WebViewImpls.end())
{
auto webView = it->second->_webView;
if (webView->_onJSCallback)
{
webView->_onJSCallback(webView, message);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::draw(cocos2d::Renderer* renderer, cocos2d::Mat4 const& transform, uint32_t flags)
{
if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY)
{
auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(_webView);
JniHelper::callStaticVoidMethod(className, "setWebViewRect", _viewTag, (int)uiRect.origin.x,
(int)uiRect.origin.y, (int)uiRect.size.width, (int)uiRect.size.height);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void WebViewImpl::setVisible(bool visible)
{
JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
}
void WebViewImpl::setOpacityWebView(const float opacity)
{
JniHelper::callStaticVoidMethod(className, "setOpacityWebView", _viewTag, opacity);
};
float WebViewImpl::getOpacityWebView() const
{
return JniHelper::callStaticFloatMethod(className, "getOpacityWebView", _viewTag);
};
void WebViewImpl::setBackgroundTransparent()
{
JniHelper::callStaticVoidMethod(className, "setBackgroundTransparent", _viewTag);
};
void WebViewImpl::setBounces(bool bounces)
{
// empty function as this was mainly a fix for iOS
}
} // namespace ui
} // namespace cocos2d