UniWebViewCocoa.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // UniWebViewInterface.cs
  3. // Created by Wang Wei(@onevcat) on 2017-04-11.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. #if (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS) && !UNITY_EDITOR_WIN
  18. using UnityEngine;
  19. using System;
  20. using System.Runtime.InteropServices;
  21. using AOT;
  22. using System.Reflection;
  23. public class UniWebViewInterface {
  24. static UniWebViewInterface() {
  25. ConnectMessageSender();
  26. }
  27. delegate void UnitySendMessageDelegate(IntPtr objectName, IntPtr methodName, IntPtr parameter);
  28. private const string DllLib =
  29. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  30. "UniWebView";
  31. #else
  32. "__Internal";
  33. #endif
  34. private static bool correctPlatform =
  35. #if UNITY_EDITOR_OSX
  36. Application.platform == RuntimePlatform.OSXEditor;
  37. #elif UNITY_STANDALONE_OSX
  38. Application.platform == RuntimePlatform.OSXPlayer;
  39. #else
  40. Application.platform == RuntimePlatform.IPhonePlayer;
  41. #endif
  42. [DllImport(DllLib)]
  43. private static extern void uv_connectMessageSender([MarshalAs(UnmanagedType.FunctionPtr)] UnitySendMessageDelegate sendMessageDelegate);
  44. static void ConnectMessageSender() {
  45. UniWebViewLogger.Instance.Info("Connecting to native side message sender.");
  46. CheckPlatform();
  47. uv_connectMessageSender(SendMessage);
  48. }
  49. [MonoPInvokeCallback(typeof(UnitySendMessageDelegate))]
  50. private static void SendMessage(IntPtr namePtr, IntPtr methodPtr, IntPtr parameterPtr) {
  51. string name = Marshal.PtrToStringAuto(namePtr);
  52. string method = Marshal.PtrToStringAuto(methodPtr);
  53. string parameters = Marshal.PtrToStringAuto(parameterPtr);
  54. UniWebViewLogger.Instance.Verbose("Received message sent from native. Name: " + name + " Method: " + method + " Params: " + parameters);
  55. var listener = UniWebViewNativeListener.GetListener(name);
  56. if (listener) {
  57. MethodInfo methodInfo = typeof(UniWebViewNativeListener).GetMethod(method);
  58. if (methodInfo != null) {
  59. methodInfo.Invoke(listener, new object[] { parameters });
  60. }
  61. }
  62. }
  63. [DllImport(DllLib)]
  64. private static extern void uv_setLogLevel(int level);
  65. public static void SetLogLevel(int level) {
  66. CheckPlatform();
  67. uv_setLogLevel(level);
  68. }
  69. [DllImport(DllLib)]
  70. private static extern void uv_init(string name, int x, int y, int width, int height);
  71. public static void Init(string name, int x, int y, int width, int height) {
  72. CheckPlatform();
  73. if (String.IsNullOrEmpty(name)) {
  74. return;
  75. }
  76. uv_init(name, x, y, width, height);
  77. }
  78. [DllImport(DllLib)]
  79. private static extern void uv_destroy(string name);
  80. public static void Destroy(string name) {
  81. CheckPlatform();
  82. uv_destroy(name);
  83. }
  84. [DllImport(DllLib)]
  85. private static extern void uv_load(string name, string url, bool skipEncoding, string readAccessURL);
  86. public static void Load(string name, string url, bool skipEncoding, string readAccessURL) {
  87. CheckPlatform();
  88. uv_load(name, url, skipEncoding, readAccessURL);
  89. }
  90. [DllImport(DllLib)]
  91. private static extern void uv_loadHTMLString(string name, string html, string baseUrl, bool skipEncoding);
  92. public static void LoadHTMLString(string name, string html, string baseUrl, bool skipEncoding) {
  93. CheckPlatform();
  94. uv_loadHTMLString(name, html, baseUrl, skipEncoding);
  95. }
  96. [DllImport(DllLib)]
  97. private static extern void uv_reload(string name);
  98. public static void Reload(string name) {
  99. CheckPlatform();
  100. uv_reload(name);
  101. }
  102. [DllImport(DllLib)]
  103. private static extern void uv_stop(string name);
  104. public static void Stop(string name) {
  105. CheckPlatform();
  106. uv_stop(name);
  107. }
  108. [DllImport(DllLib)]
  109. private static extern string uv_getUrl(string name);
  110. public static string GetUrl(string name) {
  111. CheckPlatform();
  112. return uv_getUrl(name);
  113. }
  114. [DllImport(DllLib)]
  115. private static extern void uv_setFrame(string name, int x, int y, int width, int height);
  116. public static void SetFrame(string name, int x, int y, int width, int height) {
  117. CheckPlatform();
  118. uv_setFrame(name, x, y, width, height);
  119. }
  120. [DllImport(DllLib)]
  121. private static extern void uv_setPosition(string name, int x, int y);
  122. public static void SetPosition(string name, int x, int y) {
  123. CheckPlatform();
  124. uv_setPosition(name, x, y);
  125. }
  126. [DllImport(DllLib)]
  127. private static extern void uv_setSize(string name, int width, int height);
  128. public static void SetSize(string name, int width, int height) {
  129. CheckPlatform();
  130. uv_setSize(name, width, height);
  131. }
  132. [DllImport(DllLib)]
  133. private static extern bool uv_show(string name, bool fade, int edge, float duration, string identifier);
  134. public static bool Show(string name, bool fade, int edge, float duration, string identifier) {
  135. CheckPlatform();
  136. return uv_show(name, fade, edge, duration, identifier);
  137. }
  138. [DllImport(DllLib)]
  139. private static extern bool uv_hide(string name, bool fade, int edge, float duration, string identifier);
  140. public static bool Hide(string name, bool fade, int edge, float duration, string identifier) {
  141. CheckPlatform();
  142. return uv_hide(name, fade, edge, duration, identifier);
  143. }
  144. [DllImport(DllLib)]
  145. private static extern bool uv_animateTo(string name, int x, int y, int width, int height, float duration, float delay, string identifier);
  146. public static bool AnimateTo(string name, int x, int y, int width, int height, float duration, float delay, string identifier) {
  147. CheckPlatform();
  148. return uv_animateTo(name, x, y, width, height, duration, delay, identifier);
  149. }
  150. [DllImport(DllLib)]
  151. private static extern void uv_addJavaScript(string name, string jsString, string identifier);
  152. public static void AddJavaScript(string name, string jsString, string identifier) {
  153. CheckPlatform();
  154. uv_addJavaScript(name, jsString, identifier);
  155. }
  156. [DllImport(DllLib)]
  157. private static extern void uv_evaluateJavaScript(string name, string jsString, string identifier);
  158. public static void EvaluateJavaScript(string name, string jsString, string identifier) {
  159. CheckPlatform();
  160. uv_evaluateJavaScript(name, jsString, identifier);
  161. }
  162. [DllImport(DllLib)]
  163. private static extern void uv_addUrlScheme(string name, string scheme);
  164. public static void AddUrlScheme(string name, string scheme) {
  165. CheckPlatform();
  166. uv_addUrlScheme(name, scheme);
  167. }
  168. [DllImport(DllLib)]
  169. private static extern void uv_removeUrlScheme(string name, string scheme);
  170. public static void RemoveUrlScheme(string name, string scheme) {
  171. CheckPlatform();
  172. uv_removeUrlScheme(name, scheme);
  173. }
  174. [DllImport(DllLib)]
  175. private static extern void uv_addSslExceptionDomain(string name, string domain);
  176. public static void AddSslExceptionDomain(string name, string domain) {
  177. CheckPlatform();
  178. uv_addSslExceptionDomain(name, domain);
  179. }
  180. [DllImport(DllLib)]
  181. private static extern void uv_removeSslExceptionDomain(string name, string domain);
  182. public static void RemoveSslExceptionDomain(string name, string domain) {
  183. CheckPlatform();
  184. uv_removeSslExceptionDomain(name, domain);
  185. }
  186. [DllImport(DllLib)]
  187. private static extern void uv_setHeaderField(string name, string key, string value);
  188. public static void SetHeaderField(string name, string key, string value) {
  189. CheckPlatform();
  190. uv_setHeaderField(name, key, value);
  191. }
  192. [DllImport(DllLib)]
  193. private static extern void uv_setUserAgent(string name, string userAgent);
  194. public static void SetUserAgent(string name, string userAgent) {
  195. CheckPlatform();
  196. uv_setUserAgent(name, userAgent);
  197. }
  198. [DllImport(DllLib)]
  199. private static extern string uv_getUserAgent(string name);
  200. public static string GetUserAgent(string name) {
  201. CheckPlatform();
  202. return uv_getUserAgent(name);
  203. }
  204. [DllImport(DllLib)]
  205. private static extern void uv_setAllowAutoPlay(bool flag);
  206. public static void SetAllowAutoPlay(bool flag) {
  207. CheckPlatform();
  208. uv_setAllowAutoPlay(flag);
  209. }
  210. [DllImport(DllLib)]
  211. private static extern void uv_setAllowInlinePlay(bool flag);
  212. public static void SetAllowInlinePlay(bool flag) {
  213. CheckPlatform();
  214. uv_setAllowInlinePlay(flag);
  215. }
  216. [DllImport(DllLib)]
  217. private static extern void uv_setAllowJavaScriptOpenWindow(bool flag);
  218. public static void SetAllowJavaScriptOpenWindow(bool flag) {
  219. CheckPlatform();
  220. uv_setAllowJavaScriptOpenWindow(flag);
  221. }
  222. [DllImport(DllLib)]
  223. private static extern void uv_setJavaScriptEnabled(bool flag);
  224. public static void SetJavaScriptEnabled(bool flag) {
  225. CheckPlatform();
  226. uv_setJavaScriptEnabled(flag);
  227. }
  228. [DllImport(DllLib)]
  229. private static extern void uv_cleanCache(string name);
  230. public static void CleanCache(string name) {
  231. CheckPlatform();
  232. uv_cleanCache(name);
  233. }
  234. [DllImport(DllLib)]
  235. private static extern void uv_clearCookies();
  236. public static void ClearCookies() {
  237. CheckPlatform();
  238. uv_clearCookies();
  239. }
  240. [DllImport(DllLib)]
  241. private static extern void uv_setCookie(string url, string cookie, bool skipEncoding);
  242. public static void SetCookie(string url, string cookie, bool skipEncoding) {
  243. CheckPlatform();
  244. uv_setCookie(url, cookie, skipEncoding);
  245. }
  246. [DllImport(DllLib)]
  247. private static extern string uv_getCookie(string url, string key, bool skipEncoding);
  248. public static string GetCookie(string url, string key, bool skipEncoding) {
  249. CheckPlatform();
  250. return uv_getCookie(url, key, skipEncoding);
  251. }
  252. [DllImport(DllLib)]
  253. private static extern void uv_clearHttpAuthUsernamePasswordHost(string host, string realm);
  254. public static void ClearHttpAuthUsernamePassword(string host, string realm) {
  255. CheckPlatform();
  256. uv_clearHttpAuthUsernamePasswordHost(host, realm);
  257. }
  258. [DllImport(DllLib)]
  259. private static extern void uv_setBackgroundColor(string name, float r, float g, float b, float a);
  260. public static void SetBackgroundColor(string name, float r, float g, float b, float a) {
  261. CheckPlatform();
  262. uv_setBackgroundColor(name, r, g, b, a);
  263. }
  264. [DllImport(DllLib)]
  265. private static extern void uv_setWebViewAlpha(string name, float alpha);
  266. public static void SetWebViewAlpha(string name, float alpha) {
  267. CheckPlatform();
  268. uv_setWebViewAlpha(name, alpha);
  269. }
  270. [DllImport(DllLib)]
  271. private static extern float uv_getWebViewAlpha(string name);
  272. public static float GetWebViewAlpha(string name) {
  273. CheckPlatform();
  274. return uv_getWebViewAlpha(name);
  275. }
  276. [DllImport(DllLib)]
  277. private static extern void uv_setShowSpinnerWhileLoading(string name, bool show);
  278. public static void SetShowSpinnerWhileLoading(string name, bool show) {
  279. CheckPlatform();
  280. uv_setShowSpinnerWhileLoading(name, show);
  281. }
  282. [DllImport(DllLib)]
  283. private static extern void uv_setSpinnerText(string name, string text);
  284. public static void SetSpinnerText(string name, string text) {
  285. CheckPlatform();
  286. uv_setSpinnerText(name, text);
  287. }
  288. [DllImport(DllLib)]
  289. private static extern bool uv_canGoBack(string name);
  290. public static bool CanGoBack(string name) {
  291. CheckPlatform();
  292. return uv_canGoBack(name);
  293. }
  294. [DllImport(DllLib)]
  295. private static extern bool uv_canGoForward(string name);
  296. public static bool CanGoForward(string name) {
  297. CheckPlatform();
  298. return uv_canGoForward(name);
  299. }
  300. [DllImport(DllLib)]
  301. private static extern void uv_goBack(string name);
  302. public static void GoBack(string name) {
  303. CheckPlatform();
  304. uv_goBack(name);
  305. }
  306. [DllImport(DllLib)]
  307. private static extern void uv_goForward(string name);
  308. public static void GoForward(string name) {
  309. CheckPlatform();
  310. uv_goForward(name);
  311. }
  312. [DllImport(DllLib)]
  313. private static extern void uv_setOpenLinksInExternalBrowser(string name, bool flag);
  314. public static void SetOpenLinksInExternalBrowser(string name, bool flag) {
  315. CheckPlatform();
  316. uv_setOpenLinksInExternalBrowser(name, flag);
  317. }
  318. [DllImport(DllLib)]
  319. private static extern void uv_setHorizontalScrollBarEnabled(string name, bool enabled);
  320. public static void SetHorizontalScrollBarEnabled(string name, bool enabled) {
  321. CheckPlatform();
  322. uv_setHorizontalScrollBarEnabled(name, enabled);
  323. }
  324. [DllImport(DllLib)]
  325. private static extern void uv_setVerticalScrollBarEnabled(string name, bool enabled);
  326. public static void SetVerticalScrollBarEnabled(string name, bool enabled) {
  327. CheckPlatform();
  328. uv_setVerticalScrollBarEnabled(name, enabled);
  329. }
  330. [DllImport(DllLib)]
  331. private static extern void uv_setBouncesEnabled(string name, bool enabled);
  332. public static void SetBouncesEnabled(string name, bool enabled) {
  333. CheckPlatform();
  334. uv_setBouncesEnabled(name, enabled);
  335. }
  336. [DllImport(DllLib)]
  337. private static extern void uv_setZoomEnabled(string name, bool enabled);
  338. public static void SetZoomEnabled(string name, bool enabled) {
  339. CheckPlatform();
  340. uv_setZoomEnabled(name, enabled);
  341. }
  342. [DllImport(DllLib)]
  343. private static extern void uv_setShowToolbar(string name, bool show, bool animated, bool onTop, bool adjustInset);
  344. public static void SetShowToolbar(string name, bool show, bool animated, bool onTop, bool adjustInset) {
  345. CheckPlatform();
  346. uv_setShowToolbar(name, show, animated, onTop, adjustInset);
  347. }
  348. [DllImport(DllLib)]
  349. private static extern void uv_setToolbarDoneButtonText(string name, string text);
  350. public static void SetToolbarDoneButtonText(string name, string text) {
  351. CheckPlatform();
  352. uv_setToolbarDoneButtonText(name, text);
  353. }
  354. [DllImport(DllLib)]
  355. private static extern void uv_setWindowUserResizeEnabled(string name, bool enabled);
  356. public static void SetWindowUserResizeEnabled(string name, bool enabled) {
  357. CheckPlatform();
  358. uv_setWindowUserResizeEnabled(name, enabled);
  359. }
  360. [DllImport(DllLib)]
  361. private static extern void uv_setWebContentsDebuggingEnabled(bool enabled);
  362. public static void SetWebContentsDebuggingEnabled(bool enabled) {
  363. CheckPlatform();
  364. uv_setWebContentsDebuggingEnabled(enabled);
  365. }
  366. [DllImport(DllLib)]
  367. private static extern void uv_setAllowFileAccessFromFileURLs(string name, bool flag);
  368. public static void SetAllowFileAccessFromFileURLs(string name, bool flag) {
  369. CheckPlatform();
  370. uv_setAllowFileAccessFromFileURLs(name, flag);
  371. }
  372. [DllImport(DllLib)]
  373. private static extern void uv_setAllowHTTPAuthPopUpWindow(string name, bool flag);
  374. public static void SetAllowHTTPAuthPopUpWindow(string name, bool flag) {
  375. CheckPlatform();
  376. uv_setAllowHTTPAuthPopUpWindow(name, flag);
  377. }
  378. [DllImport(DllLib)]
  379. private static extern void uv_print(string name);
  380. public static void Print(string name) {
  381. CheckPlatform();
  382. uv_print(name);
  383. }
  384. [DllImport(DllLib)]
  385. private static extern void uv_setCalloutEnabled(string name, bool flag);
  386. public static void SetCalloutEnabled(string name, bool flag) {
  387. CheckPlatform();
  388. uv_setCalloutEnabled(name, flag);
  389. }
  390. [DllImport(DllLib)]
  391. private static extern void uv_setDragInteractionEnabled(string name, bool flag);
  392. public static void SetDragInteractionEnabled(string name, bool flag) {
  393. CheckPlatform();
  394. uv_setDragInteractionEnabled(name, flag);
  395. }
  396. public static void CheckPlatform() {
  397. if (!correctPlatform) {
  398. throw new System.InvalidOperationException("Method can only be performed on correct platform. Current: " + Application.platform);
  399. }
  400. }
  401. }
  402. #endif