1
0

UIWebViewImpl-android.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifdef __ANDROID__
  21. #include "ui/UIWebViewImpl-android.h"
  22. #include <unordered_map>
  23. #include <stdlib.h>
  24. #include <string>
  25. #include "platform/android/jni/JniHelper.h"
  26. #include "ui/UIWebView.h"
  27. #include "platform/CCGLView.h"
  28. #include "base/CCDirector.h"
  29. #include "platform/CCFileUtils.h"
  30. #include "ui/UIHelper.h"
  31. static const std::string className = "org/cocos2dx/lib/Cocos2dxWebViewHelper";
  32. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__)
  33. static const std::string s_defaultBaseUrl = "file:///android_asset/";
  34. static const std::string s_sdRootBaseUrl = "file://";
  35. static std::string getFixedBaseUrl(const std::string& baseUrl)
  36. {
  37. std::string fixedBaseUrl;
  38. if (baseUrl.empty())
  39. {
  40. fixedBaseUrl = s_defaultBaseUrl;
  41. }
  42. else if (baseUrl.find(s_sdRootBaseUrl) != std::string::npos)
  43. {
  44. fixedBaseUrl = baseUrl;
  45. }
  46. else if (baseUrl.c_str()[0] != '/') {
  47. if(baseUrl.find("assets/") == 0) {
  48. fixedBaseUrl = s_defaultBaseUrl + baseUrl.c_str()[7];
  49. }
  50. else {
  51. fixedBaseUrl = s_defaultBaseUrl + baseUrl;
  52. }
  53. }
  54. else {
  55. fixedBaseUrl = s_sdRootBaseUrl + baseUrl;
  56. }
  57. if (fixedBaseUrl.c_str()[fixedBaseUrl.length() - 1] != '/') {
  58. fixedBaseUrl += "/";
  59. }
  60. return fixedBaseUrl;
  61. }
  62. extern "C" {
  63. /*
  64. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  65. * Method: shouldStartLoading
  66. * Signature: (ILjava/lang/String;)Z
  67. */
  68. JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
  69. auto charUrl = env->GetStringUTFChars(jurl, NULL);
  70. std::string url = charUrl;
  71. env->ReleaseStringUTFChars(jurl, charUrl);
  72. return cocos2d::experimental::ui::WebViewImpl::shouldStartLoading(index, url);
  73. }
  74. /*
  75. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  76. * Method: didFinishLoading
  77. * Signature: (ILjava/lang/String;)V
  78. */
  79. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
  80. // LOGD("didFinishLoading");
  81. auto charUrl = env->GetStringUTFChars(jurl, NULL);
  82. std::string url = charUrl;
  83. env->ReleaseStringUTFChars(jurl, charUrl);
  84. cocos2d::experimental::ui::WebViewImpl::didFinishLoading(index, url);
  85. }
  86. /*
  87. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  88. * Method: didFailLoading
  89. * Signature: (ILjava/lang/String;)V
  90. */
  91. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
  92. // LOGD("didFailLoading");
  93. auto charUrl = env->GetStringUTFChars(jurl, NULL);
  94. std::string url = charUrl;
  95. env->ReleaseStringUTFChars(jurl, charUrl);
  96. cocos2d::experimental::ui::WebViewImpl::didFailLoading(index, url);
  97. }
  98. /*
  99. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  100. * Method: onJsCallback
  101. * Signature: (ILjava/lang/String;)V
  102. */
  103. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) {
  104. // LOGD("jsCallback");
  105. auto charMessage = env->GetStringUTFChars(jmessage, NULL);
  106. std::string message = charMessage;
  107. env->ReleaseStringUTFChars(jmessage, charMessage);
  108. cocos2d::experimental::ui::WebViewImpl::onJsCallback(index, message);
  109. }
  110. }
  111. namespace {
  112. int createWebViewJNI() {
  113. cocos2d::JniMethodInfo t;
  114. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), "createWebView", "()I")) {
  115. // LOGD("error: %s,%d",__func__,__LINE__);
  116. jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID);
  117. t.env->DeleteLocalRef(t.classID);
  118. return viewTag;
  119. }
  120. return -1;
  121. }
  122. std::string getUrlStringByFileName(const std::string &fileName) {
  123. // LOGD("error: %s,%d",__func__,__LINE__);
  124. const std::string basePath("file:///android_asset/");
  125. std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
  126. const std::string assetsPath("assets/");
  127. std::string urlString;
  128. if (fullPath.find(assetsPath) != std::string::npos) {
  129. urlString = fullPath.replace(fullPath.find_first_of(assetsPath), assetsPath.length(), basePath);
  130. } else {
  131. urlString = fullPath;
  132. }
  133. return urlString;
  134. }
  135. } // namespace
  136. namespace cocos2d {
  137. namespace experimental {
  138. namespace ui{
  139. static std::unordered_map<int, cocos2d::experimental::ui::WebViewImpl*> s_WebViewImpls;
  140. WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) {
  141. _viewTag = createWebViewJNI();
  142. s_WebViewImpls[_viewTag] = this;
  143. }
  144. WebViewImpl::~WebViewImpl() {
  145. JniHelper::callStaticVoidMethod(className, "removeWebView", _viewTag);
  146. s_WebViewImpls.erase(_viewTag);
  147. }
  148. void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) {
  149. std::string dataString(reinterpret_cast<char *>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
  150. JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, dataString, MIMEType, encoding, baseURL);
  151. }
  152. void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) {
  153. JniHelper::callStaticVoidMethod(className, "loadHTMLString", _viewTag, string, getFixedBaseUrl(baseURL));
  154. }
  155. void WebViewImpl::loadURL(const std::string &url) {
  156. this->loadURL(url, false);
  157. }
  158. void WebViewImpl::loadURL(const std::string &url, bool cleanCachedData) {
  159. JniHelper::callStaticVoidMethod(className, "loadUrl", _viewTag, url, cleanCachedData);
  160. }
  161. void WebViewImpl::loadFile(const std::string &fileName) {
  162. auto fullPath = getUrlStringByFileName(fileName);
  163. JniHelper::callStaticVoidMethod(className, "loadFile", _viewTag, fullPath);
  164. }
  165. void WebViewImpl::stopLoading() {
  166. JniHelper::callStaticVoidMethod(className, "stopLoading", _viewTag);
  167. }
  168. void WebViewImpl::reload() {
  169. JniHelper::callStaticVoidMethod(className, "reload", _viewTag);
  170. }
  171. bool WebViewImpl::canGoBack() {
  172. return JniHelper::callStaticBooleanMethod(className, "canGoBack", _viewTag);
  173. }
  174. bool WebViewImpl::canGoForward() {
  175. return JniHelper::callStaticBooleanMethod(className, "canGoForward", _viewTag);
  176. }
  177. void WebViewImpl::goBack() {
  178. JniHelper::callStaticVoidMethod(className, "goBack", _viewTag);
  179. }
  180. void WebViewImpl::goForward() {
  181. JniHelper::callStaticVoidMethod(className, "goForward", _viewTag);
  182. }
  183. void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) {
  184. JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, scheme);
  185. }
  186. void WebViewImpl::evaluateJS(const std::string &js) {
  187. JniHelper::callStaticVoidMethod(className, "evaluateJS", _viewTag, js);
  188. }
  189. void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) {
  190. JniHelper::callStaticVoidMethod(className, "setScalesPageToFit", _viewTag, scalesPageToFit);
  191. }
  192. bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) {
  193. bool allowLoad = true;
  194. auto it = s_WebViewImpls.find(viewTag);
  195. if (it != s_WebViewImpls.end()) {
  196. auto webView = it->second->_webView;
  197. if (webView->_onShouldStartLoading) {
  198. allowLoad = webView->_onShouldStartLoading(webView, url);
  199. }
  200. }
  201. return allowLoad;
  202. }
  203. void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){
  204. auto it = s_WebViewImpls.find(viewTag);
  205. if (it != s_WebViewImpls.end()) {
  206. auto webView = it->second->_webView;
  207. if (webView->_onDidFinishLoading) {
  208. webView->_onDidFinishLoading(webView, url);
  209. }
  210. }
  211. }
  212. void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){
  213. auto it = s_WebViewImpls.find(viewTag);
  214. if (it != s_WebViewImpls.end()) {
  215. auto webView = it->second->_webView;
  216. if (webView->_onDidFailLoading) {
  217. webView->_onDidFailLoading(webView, url);
  218. }
  219. }
  220. }
  221. void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){
  222. auto it = s_WebViewImpls.find(viewTag);
  223. if (it != s_WebViewImpls.end()) {
  224. auto webView = it->second->_webView;
  225. if (webView->_onJSCallback) {
  226. webView->_onJSCallback(webView, message);
  227. }
  228. }
  229. }
  230. void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) {
  231. if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) {
  232. auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(_webView);
  233. JniHelper::callStaticVoidMethod(className, "setWebViewRect", _viewTag,
  234. (int)uiRect.origin.x, (int)uiRect.origin.y,
  235. (int)uiRect.size.width, (int)uiRect.size.height);
  236. }
  237. }
  238. void WebViewImpl::setVisible(bool visible) {
  239. JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
  240. }
  241. void WebViewImpl::setBounces(bool bounces) {
  242. // empty function as this was mainly a fix for iOS
  243. }
  244. } // namespace ui
  245. } // namespace experimental
  246. } //namespace cocos2d
  247. #endif // __ANDROID__