UniWebViewAndroid.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. #if UNITY_ANDROID && !UNITY_EDITOR
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. class UniWebViewMethodChannel: AndroidJavaProxy {
  7. private const string GlobalChannelIdentifier = "__UniWebViewGlobalChannelIdentifier";
  8. public UniWebViewMethodChannel() : base("com.onevcat.uniwebview.UniWebViewNativeChannel") { }
  9. string invokeChannelMethod(string name, string method, string parameters) {
  10. if (name == GlobalChannelIdentifier) {
  11. UniWebViewLogger.Instance.Verbose(
  12. "Global channel method invoked. Method: " + method + " Params: " + parameters
  13. );
  14. return UniWebViewStaticListener.InvokeStaticMethod(method, parameters);
  15. } else {
  16. UniWebViewLogger.Instance.Verbose("invokeChannelMethod invoked by native side. Name: " + name + " Method: "
  17. + method + " Params: " + parameters);
  18. return UniWebViewChannelMethodManager.Instance.InvokeMethod(name, method, parameters);
  19. }
  20. }
  21. }
  22. public class UniWebViewInterface {
  23. private const int SnapshotTextureStreamStopRenderEventCount = 3;
  24. private static readonly AndroidJavaClass plugin;
  25. private static bool correctPlatform = Application.platform == RuntimePlatform.Android;
  26. static UniWebViewInterface() {
  27. var go = new GameObject("UniWebViewAndroidStaticListener");
  28. go.AddComponent<UniWebViewAndroidStaticListener>();
  29. plugin = new AndroidJavaClass("com.onevcat.uniwebview.UniWebViewInterface");
  30. // Prepare dispatcher instance. Some callbacks may come from non-UI threads. Use this dispatcher to
  31. // send any action to the Unity main thread.
  32. _ = UniWebViewMainThreadDispatcher.Instance;
  33. CheckPlatform();
  34. plugin.CallStatic("prepare");
  35. UniWebViewLogger.Instance.Info("Connecting to native side method channel.");
  36. plugin.CallStatic("registerChannel", new UniWebViewMethodChannel());
  37. }
  38. public static void SetLogLevel(int level) {
  39. CheckPlatform();
  40. plugin.CallStatic("setLogLevel", level);
  41. }
  42. public static bool IsWebViewSupported() {
  43. CheckPlatform();
  44. return plugin.CallStatic<bool>("isWebViewSupported");
  45. }
  46. public static void Init(string name, int x, int y, int width, int height) {
  47. CheckPlatform();
  48. plugin.CallStatic("init", name, x, y, width, height);
  49. }
  50. public static void Destroy(string name) {
  51. CheckPlatform();
  52. plugin.CallStatic("destroy", name);
  53. }
  54. public static void Load(string name, string url, bool skipEncoding, string readAccessURL) {
  55. CheckPlatform();
  56. plugin.CallStatic("load", name, url);
  57. }
  58. public static void LoadHTMLString(string name, string html, string baseUrl, bool skipEncoding) {
  59. CheckPlatform();
  60. plugin.CallStatic("loadHTMLString", name, html, baseUrl);
  61. }
  62. public static void Reload(string name) {
  63. CheckPlatform();
  64. plugin.CallStatic("reload", name);
  65. }
  66. public static void Stop(string name) {
  67. CheckPlatform();
  68. plugin.CallStatic("stop", name);
  69. }
  70. public static string GetUrl(string name) {
  71. CheckPlatform();
  72. return plugin.CallStatic<string>("getUrl", name);
  73. }
  74. public static void SetFrame(string name, int x, int y, int width, int height) {
  75. CheckPlatform();
  76. plugin.CallStatic("setFrame", name, x, y, width, height);
  77. }
  78. public static void SetPosition(string name, int x, int y) {
  79. CheckPlatform();
  80. plugin.CallStatic("setPosition", name, x, y);
  81. }
  82. public static void SetSize(string name, int width, int height) {
  83. CheckPlatform();
  84. plugin.CallStatic("setSize", name, width, height);
  85. }
  86. public static void SetTransform(string name, float rotation, float scaleX, float scaleY) {
  87. CheckPlatform();
  88. plugin.CallStatic("setTransform", name, rotation, scaleX, scaleY);
  89. }
  90. public static void SetRoundCornerRadius(string name, float topLeft, float topRight, float bottomLeft, float bottomRight) {
  91. CheckPlatform();
  92. plugin.CallStatic("setCornerRadius", name, topLeft, topRight, bottomLeft, bottomRight);
  93. }
  94. public static void SetShadow(
  95. string name,
  96. float red,
  97. float green,
  98. float blue,
  99. float alpha,
  100. float opacity,
  101. float radius,
  102. float offsetX,
  103. float offsetY,
  104. float spread
  105. ) {
  106. CheckPlatform();
  107. plugin.CallStatic("setShadow", name, red, green, blue, alpha, opacity, radius, offsetX, offsetY, spread);
  108. }
  109. public static bool Show(string name, bool fade, int edge, float duration, bool useAsync, string identifier) {
  110. CheckPlatform();
  111. if (useAsync) {
  112. plugin.CallStatic("showAsync", name, fade, edge, duration, identifier);
  113. return true;
  114. } else {
  115. return plugin.CallStatic<bool>("show", name, fade, edge, duration, identifier);
  116. }
  117. }
  118. public static bool Hide(string name, bool fade, int edge, float duration, bool useAsync, string identifier) {
  119. CheckPlatform();
  120. if (useAsync) {
  121. plugin.CallStatic("hideAsync", name, fade, edge, duration, identifier);
  122. return true;
  123. } else {
  124. return plugin.CallStatic<bool>("hide", name, fade, edge, duration, identifier);
  125. }
  126. }
  127. public static bool AnimateTo(string name, int x, int y, int width, int height, float duration, float delay, string identifier) {
  128. CheckPlatform();
  129. return plugin.CallStatic<bool>("animateTo", name, x, y, width, height, duration, delay, identifier);
  130. }
  131. public static void AddJavaScript(string name, string jsString, string identifier) {
  132. CheckPlatform();
  133. plugin.CallStatic("addJavaScript", name, jsString, identifier);
  134. }
  135. public static void EvaluateJavaScript(string name, string jsString, string identifier) {
  136. CheckPlatform();
  137. plugin.CallStatic("evaluateJavaScript", name, jsString, identifier);
  138. }
  139. public static void ClosePopupWindow(string name, string popupId) {
  140. CheckPlatform();
  141. plugin.CallStatic("closePopupWindow", name, popupId);
  142. }
  143. public static void GoBackPopupWindow(string name, string popupId) {
  144. CheckPlatform();
  145. plugin.CallStatic("goBackPopupWindow", name, popupId);
  146. }
  147. public static void GoForwardPopupWindow(string name, string popupId) {
  148. CheckPlatform();
  149. plugin.CallStatic("goForwardPopupWindow", name, popupId);
  150. }
  151. public static void EvaluateJavaScriptInPopupWindow(
  152. string name,
  153. string popupId,
  154. string jsString,
  155. string identifier
  156. ) {
  157. CheckPlatform();
  158. plugin.CallStatic("evaluateJavaScriptInPopupWindow", name, popupId, jsString, identifier);
  159. }
  160. public static void CloseAllPopupWindows(string name) {
  161. CheckPlatform();
  162. plugin.CallStatic("closeAllPopupWindows", name);
  163. }
  164. public static void SetPopupPageEventEnabled(string name, bool enabled) {
  165. CheckPlatform();
  166. plugin.CallStatic("setPopupPageEventEnabled", name, enabled);
  167. }
  168. public static void AddUrlScheme(string name, string scheme) {
  169. CheckPlatform();
  170. plugin.CallStatic("addUrlScheme", name, scheme);
  171. }
  172. public static void RemoveUrlScheme(string name, string scheme) {
  173. CheckPlatform();
  174. plugin.CallStatic("removeUrlScheme", name, scheme);
  175. }
  176. [Obsolete("AddSslExceptionDomain is deprecated. Use AddSslPinnedFingerprint instead.")]
  177. public static void AddSslExceptionDomain(string name, string domain) {
  178. CheckPlatform();
  179. plugin.CallStatic("addSslExceptionDomain", name, domain);
  180. }
  181. [Obsolete("RemoveSslExceptionDomain is deprecated. Use RemoveSslPinnedFingerprint instead.")]
  182. public static void RemoveSslExceptionDomain(string name, string domain) {
  183. CheckPlatform();
  184. plugin.CallStatic("removeSslExceptionDomain", name, domain);
  185. }
  186. public static void AddSslPinnedFingerprint(string name, string domain, string fingerprint) {
  187. CheckPlatform();
  188. plugin.CallStatic("addSslPinnedFingerprint", name, domain, fingerprint);
  189. }
  190. public static void RemoveSslPinnedFingerprint(string name, string domain, string fingerprint) {
  191. CheckPlatform();
  192. plugin.CallStatic("removeSslPinnedFingerprint", name, domain, fingerprint);
  193. }
  194. public static void AddPermissionTrustDomain(string name, string domain) {
  195. CheckPlatform();
  196. plugin.CallStatic("addPermissionTrustDomain", name, domain);
  197. }
  198. public static void RemovePermissionTrustDomain(string name, string domain) {
  199. CheckPlatform();
  200. plugin.CallStatic("removePermissionTrustDomain", name, domain);
  201. }
  202. public static void SetHeaderField(string name, string key, string value) {
  203. CheckPlatform();
  204. plugin.CallStatic("setHeaderField", name, key, value);
  205. }
  206. public static void SetUserAgent(string name, string userAgent) {
  207. CheckPlatform();
  208. plugin.CallStatic("setUserAgent", name, userAgent);
  209. }
  210. public static string GetUserAgent(string name) {
  211. CheckPlatform();
  212. return plugin.CallStatic<string>("getUserAgent", name);
  213. }
  214. public static void SetAllowAutoPlay(bool flag) {
  215. CheckPlatform();
  216. plugin.CallStatic("setAllowAutoPlay", flag);
  217. }
  218. public static void SetAllowJavaScriptOpenWindow(bool flag) {
  219. CheckPlatform();
  220. plugin.CallStatic("setAllowJavaScriptOpenWindow", flag);
  221. }
  222. public static void SetAllowFileAccess(string name, bool flag) {
  223. CheckPlatform();
  224. plugin.CallStatic("setAllowFileAccess", name, flag);
  225. }
  226. public static void SetAcceptThirdPartyCookies(string name, bool flag) {
  227. CheckPlatform();
  228. plugin.CallStatic("setAcceptThirdPartyCookies", name, flag);
  229. }
  230. public static void SetAllowFileAccessFromFileURLs(string name, bool flag) {
  231. CheckPlatform();
  232. plugin.CallStatic("setAllowFileAccessFromFileURLs", name, flag);
  233. }
  234. public static void SetAllowUniversalAccessFromFileURLs(bool flag) {
  235. CheckPlatform();
  236. plugin.CallStatic("setAllowUniversalAccessFromFileURLs", flag);
  237. }
  238. public static void BringContentToFront(string name) {
  239. CheckPlatform();
  240. plugin.CallStatic("bringContentToFront", name);
  241. }
  242. public static void SetForwardWebConsoleToNativeOutput(bool flag) {
  243. CheckPlatform();
  244. plugin.CallStatic("setForwardWebConsoleToNativeOutput", flag);
  245. }
  246. public static void SetEnableKeyboardAvoidance(bool flag) {
  247. CheckPlatform();
  248. plugin.CallStatic("setEnableKeyboardAvoidance", flag);
  249. }
  250. public static void SetJavaScriptEnabled(bool enabled) {
  251. CheckPlatform();
  252. plugin.CallStatic("setJavaScriptEnabled", enabled);
  253. }
  254. public static void CleanCache(string name, bool includeStorage, string identifier) {
  255. CheckPlatform();
  256. plugin.CallStatic("cleanCache", name, includeStorage, identifier);
  257. }
  258. public static void SetCacheMode(string name, int mode) {
  259. CheckPlatform();
  260. plugin.CallStatic("setCacheMode", name, mode);
  261. }
  262. public static void ClearCookies() {
  263. CheckPlatform();
  264. plugin.CallStatic("clearCookies");
  265. }
  266. public static void ClearCookies(string identifier) {
  267. CheckPlatform();
  268. plugin.CallStatic("clearCookiesAsync", identifier);
  269. }
  270. public static void SetCookie(string url, string cookie, bool skipEncoding) {
  271. CheckPlatform();
  272. plugin.CallStatic("setCookie", url, cookie);
  273. }
  274. public static void SetCookie(string url, string cookie, bool skipEncoding, string identifier) {
  275. CheckPlatform();
  276. plugin.CallStatic("setCookieAsync", url, cookie, identifier);
  277. }
  278. public static string GetCookie(string url, string key, bool skipEncoding) {
  279. CheckPlatform();
  280. return plugin.CallStatic<string>("getCookie", url, key);
  281. }
  282. public static void GetCookie(string url, string key, bool skipEncoding, string identifier) {
  283. CheckPlatform();
  284. plugin.CallStatic("getCookieAsync", url, key, identifier);
  285. }
  286. public static void RemoveCookies(string url, bool skipEncoding) {
  287. CheckPlatform();
  288. plugin.CallStatic("removeCookies", url);
  289. }
  290. public static void RemoveCookies(string url, bool skipEncoding, string identifier) {
  291. CheckPlatform();
  292. plugin.CallStatic("removeCookiesAsync", url, identifier);
  293. }
  294. public static void RemoveCookie(string url, string key, bool skipEncoding) {
  295. CheckPlatform();
  296. plugin.CallStatic("removeCookie", url, key);
  297. }
  298. public static void RemoveCookie(string url, string key, bool skipEncoding, string identifier) {
  299. CheckPlatform();
  300. plugin.CallStatic("removeCookieAsync", url, key, identifier);
  301. }
  302. public static void ClearHttpAuthUsernamePassword(string host, string realm) {
  303. CheckPlatform();
  304. plugin.CallStatic("clearHttpAuthUsernamePassword", host, realm);
  305. }
  306. public static void SetBackgroundColor(string name, float r, float g, float b, float a) {
  307. CheckPlatform();
  308. plugin.CallStatic("setBackgroundColor", name, r, g, b, a);
  309. }
  310. public static void SetWebViewAlpha(string name, float alpha) {
  311. CheckPlatform();
  312. plugin.CallStatic("setWebViewAlpha", name, alpha);
  313. }
  314. public static float GetWebViewAlpha(string name) {
  315. CheckPlatform();
  316. return plugin.CallStatic<float>("getWebViewAlpha", name);
  317. }
  318. public static void SetShowSpinnerWhileLoading(string name, bool show) {
  319. CheckPlatform();
  320. plugin.CallStatic("setShowSpinnerWhileLoading", name, show);
  321. }
  322. public static void SetSpinnerText(string name, string text) {
  323. CheckPlatform();
  324. plugin.CallStatic("setSpinnerText", name, text);
  325. }
  326. public static void SetAllowUserDismissSpinnerByGesture(string name, bool flag) {
  327. CheckPlatform();
  328. plugin.CallStatic("setAllowUserDismissSpinnerByGesture", name, flag);
  329. }
  330. public static void ShowSpinner(string name) {
  331. CheckPlatform();
  332. plugin.CallStatic("showSpinner", name);
  333. }
  334. public static void HideSpinner(string name) {
  335. CheckPlatform();
  336. plugin.CallStatic("hideSpinner", name);
  337. }
  338. public static bool CanGoBack(string name) {
  339. CheckPlatform();
  340. return plugin.CallStatic<bool>("canGoBack", name);
  341. }
  342. public static bool CanGoForward(string name) {
  343. CheckPlatform();
  344. return plugin.CallStatic<bool>("canGoForward", name);
  345. }
  346. public static void GoBack(string name) {
  347. CheckPlatform();
  348. plugin.CallStatic("goBack", name);
  349. }
  350. public static void GoForward(string name) {
  351. CheckPlatform();
  352. plugin.CallStatic("goForward", name);
  353. }
  354. public static void SetOpenLinksInExternalBrowser(string name, bool flag) {
  355. CheckPlatform();
  356. plugin.CallStatic("setOpenLinksInExternalBrowser", name, flag);
  357. }
  358. public static void SetHorizontalScrollBarEnabled(string name, bool enabled) {
  359. CheckPlatform();
  360. plugin.CallStatic("setHorizontalScrollBarEnabled", name, enabled);
  361. }
  362. public static void SetVerticalScrollBarEnabled(string name, bool enabled) {
  363. CheckPlatform();
  364. plugin.CallStatic("setVerticalScrollBarEnabled", name, enabled);
  365. }
  366. public static void SetBouncesEnabled(string name, bool enabled) {
  367. CheckPlatform();
  368. plugin.CallStatic("setBouncesEnabled", name, enabled);
  369. }
  370. public static void SetZoomEnabled(string name, bool enabled) {
  371. CheckPlatform();
  372. plugin.CallStatic("setZoomEnabled", name, enabled);
  373. }
  374. public static void SetUseWideViewPort(string name, bool use) {
  375. CheckPlatform();
  376. plugin.CallStatic("setUseWideViewPort", name, use);
  377. }
  378. public static void SetLoadWithOverviewMode(string name, bool overview) {
  379. CheckPlatform();
  380. plugin.CallStatic("setLoadWithOverviewMode", name, overview);
  381. }
  382. public static void SetImmersiveModeEnabled(string name, bool enabled) {
  383. CheckPlatform();
  384. plugin.CallStatic("setImmersiveModeEnabled", name, enabled);
  385. }
  386. public static void SetUserInteractionEnabled(string name, bool enabled) {
  387. CheckPlatform();
  388. plugin.CallStatic("setUserInteractionEnabled", name, enabled);
  389. }
  390. public static void SetTransparencyClickingThroughEnabled(string name, bool enabled) {
  391. CheckPlatform();
  392. plugin.CallStatic("setTransparencyClickingThroughEnabled", name, enabled);
  393. }
  394. public static void RefreshTransparencyClickingThroughLayout(string name) {
  395. CheckPlatform();
  396. plugin.CallStatic("refreshTransparencyClickingThroughLayout", name);
  397. }
  398. public static void SetWebContentsDebuggingEnabled(bool enabled) {
  399. CheckPlatform();
  400. plugin.CallStatic("setWebContentsDebuggingEnabled", enabled);
  401. }
  402. public static void SetAllowHTTPAuthPopUpWindow(string name, bool flag) {
  403. CheckPlatform();
  404. plugin.CallStatic("setAllowHTTPAuthPopUpWindow", name, flag);
  405. }
  406. public static void Print(string name) {
  407. CheckPlatform();
  408. plugin.CallStatic("print", name);
  409. }
  410. public static void CaptureSnapshot(string name, string filename) {
  411. CheckPlatform();
  412. plugin.CallStatic("captureSnapshot", name, filename);
  413. }
  414. public static void ScrollTo(string name, int x, int y, bool animated) {
  415. CheckPlatform();
  416. plugin.CallStatic("scrollTo", name, x, y, animated);
  417. }
  418. public static void SetCalloutEnabled(string name, bool flag) {
  419. CheckPlatform();
  420. plugin.CallStatic("setCalloutEnabled", name, flag);
  421. }
  422. public static void SetSupportMultipleWindows(string name, bool enabled, bool allowJavaScriptOpening) {
  423. CheckPlatform();
  424. plugin.CallStatic("setSupportMultipleWindows", name, enabled, allowJavaScriptOpening);
  425. }
  426. public static void SetDragInteractionEnabled(string name, bool flag) {
  427. CheckPlatform();
  428. plugin.CallStatic("setDragInteractionEnabled", name, flag);
  429. }
  430. public static void SetDefaultFontSize(string name, int size) {
  431. CheckPlatform();
  432. plugin.CallStatic("setDefaultFontSize", name, size);
  433. }
  434. public static void SetTextZoom(string name, int textZoom) {
  435. CheckPlatform();
  436. plugin.CallStatic("setTextZoom", name, textZoom);
  437. }
  438. public static float NativeScreenWidth() {
  439. CheckPlatform();
  440. return plugin.CallStatic<float>("screenWidth");
  441. }
  442. public static float NativeScreenHeight() {
  443. CheckPlatform();
  444. return plugin.CallStatic<float>("screenHeight");
  445. }
  446. public static int GetStatusBarHeight() {
  447. CheckPlatform();
  448. return plugin.CallStatic<int>("getStatusBarHeight");
  449. }
  450. // Returns the padding of the native content view as [left, top, right, bottom] in pixels.
  451. // Returns all zeros if the value cannot be retrieved, so callers fall back to the
  452. // status-bar-based frame mapping.
  453. public static float[] GetContentViewPadding() {
  454. CheckPlatform();
  455. try {
  456. var result = plugin.CallStatic<string>("getContentViewPadding");
  457. var parts = result.Split(',');
  458. if (parts.Length == 4) {
  459. return new float[] {
  460. int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), int.Parse(parts[3])
  461. };
  462. }
  463. UniWebViewLogger.Instance.Warning("Unexpected content view padding value: " + result);
  464. } catch (Exception e) {
  465. UniWebViewLogger.Instance.Warning("Failed to get content view padding: " + e.Message);
  466. }
  467. return new float[] { 0, 0, 0, 0 };
  468. }
  469. public static void SetDownloadEventForContextMenuEnabled(string name, bool enabled) {
  470. CheckPlatform();
  471. plugin.CallStatic("setDownloadEventForContextMenuEnabled", name, enabled);
  472. }
  473. public static void SetAutoDownloadEnabled(string name, bool enabled) {
  474. CheckPlatform();
  475. plugin.CallStatic("setAutoDownloadEnabled", name, enabled);
  476. }
  477. public static void SetAllowUserEditFileNameBeforeDownloading(string name, bool allowed) {
  478. CheckPlatform();
  479. plugin.CallStatic("setAllowUserEditFileNameBeforeDownloading", name, allowed);
  480. }
  481. // Safe Browsing
  482. public static bool IsSafeBrowsingSupported() {
  483. CheckPlatform();
  484. return plugin.CallStatic<bool>("isSafeBrowsingSupported");
  485. }
  486. public static string GetSafeBrowsingCustomTabsProviderPackageName() {
  487. CheckPlatform();
  488. return plugin.CallStatic<string>("getSafeBrowsingCustomTabsProviderPackageName");
  489. }
  490. public static void SafeBrowsingInit(string name, string url) {
  491. CheckPlatform();
  492. plugin.CallStatic("safeBrowsingInit", name, url);
  493. }
  494. public static void SafeBrowsingChangeUrl(string name, string url) {
  495. CheckPlatform();
  496. plugin.CallStatic("safeBrowsingChangeUrl", name, url);
  497. }
  498. public static void SafeBrowsingSetToolbarColor(string name, float r, float g, float b) {
  499. CheckPlatform();
  500. plugin.CallStatic("safeBrowsingSetToolbarColor", name, r, g, b);
  501. }
  502. public static void SafeBrowsingSetColorScheme(string name, int colorScheme) {
  503. CheckPlatform();
  504. plugin.CallStatic("safeBrowsingSetColorScheme", name, colorScheme);
  505. }
  506. public static void SafeBrowsingShow(string name) {
  507. CheckPlatform();
  508. plugin.CallStatic("safeBrowsingShow", name);
  509. }
  510. public static void SafeBrowsingInvalidate(string name) {
  511. CheckPlatform();
  512. plugin.CallStatic("safeBrowsingInvalidate", name);
  513. }
  514. public static void SetPreferredCustomTabsBrowsers(string[] packages) {
  515. CheckPlatform();
  516. plugin.CallStatic("setPreferredCustomTabsBrowsers", (object)packages);
  517. }
  518. public static void SafeBrowsingSetSecondaryToolbarColor(string name, float r, float g, float b) {
  519. CheckPlatform();
  520. plugin.CallStatic("safeBrowsingSetSecondaryToolbarColor", name, r, g, b);
  521. }
  522. public static void SafeBrowsingSetNavigationBarColor(string name, float r, float g, float b) {
  523. CheckPlatform();
  524. plugin.CallStatic("safeBrowsingSetNavigationBarColor", name, r, g, b);
  525. }
  526. public static void SafeBrowsingSetNavigationBarDividerColor(string name, float r, float g, float b) {
  527. CheckPlatform();
  528. plugin.CallStatic("safeBrowsingSetNavigationBarDividerColor", name, r, g, b);
  529. }
  530. public static void SafeBrowsingSetToolbarCornerRadiusDp(string name, int cornerRadiusDp) {
  531. CheckPlatform();
  532. plugin.CallStatic("safeBrowsingSetToolbarCornerRadiusDp", name, cornerRadiusDp);
  533. }
  534. public static void SafeBrowsingSetInitialHeightPx(string name, int initialHeightPx, int resizeBehavior = (int)UniWebViewSafeBrowsing.ActivityHeightResizeBehavior.Fixed) {
  535. CheckPlatform();
  536. plugin.CallStatic("safeBrowsingSetInitialHeightPx", name, initialHeightPx, resizeBehavior);
  537. }
  538. public static void SafeBrowsingSetInitialWidthPx(string name, int initialWidthPx) {
  539. CheckPlatform();
  540. plugin.CallStatic("safeBrowsingSetInitialWidthPx", name, initialWidthPx);
  541. }
  542. public static void SafeBrowsingSetShareMenuItemEnabled(string name, bool enabled) {
  543. CheckPlatform();
  544. plugin.CallStatic("safeBrowsingSetShareMenuItemEnabled", name, enabled);
  545. }
  546. public static void SafeBrowsingSetUrlBarHidingEnabled(string name, bool enabled) {
  547. CheckPlatform();
  548. plugin.CallStatic("safeBrowsingSetUrlBarHidingEnabled", name, enabled);
  549. }
  550. public static void SafeBrowsingSetSendToExternalDefaultHandlerEnabled(string name, bool enabled) {
  551. CheckPlatform();
  552. plugin.CallStatic("safeBrowsingSetSendToExternalDefaultHandlerEnabled", name, enabled);
  553. }
  554. public static void SafeBrowsingSetMaximizationEnabled(string name, bool enabled) {
  555. CheckPlatform();
  556. plugin.CallStatic("safeBrowsingSetMaximizationEnabled", name, enabled);
  557. }
  558. public static void SafeBrowsingSetDownloadButtonEnabled(string name, bool enabled) {
  559. CheckPlatform();
  560. plugin.CallStatic("safeBrowsingSetDownloadButtonEnabled", name, enabled);
  561. }
  562. public static void SafeBrowsingSetBookmarksButtonEnabled(string name, bool enabled) {
  563. CheckPlatform();
  564. plugin.CallStatic("safeBrowsingSetBookmarksButtonEnabled", name, enabled);
  565. }
  566. public static void SafeBrowsingSetBackgroundInteractionEnabled(string name, bool enabled) {
  567. CheckPlatform();
  568. plugin.CallStatic("safeBrowsingSetBackgroundInteractionEnabled", name, enabled);
  569. }
  570. public static void SafeBrowsingSetWarmup(string name, bool enabled) {
  571. CheckPlatform();
  572. plugin.CallStatic("safeBrowsingSetWarmup", name, enabled);
  573. }
  574. public static void SafeBrowsingSetPrefetch(string name, bool enabled, string optionalAlternativeUrl = null) {
  575. CheckPlatform();
  576. plugin.CallStatic("safeBrowsingSetPrefetch", name, enabled, optionalAlternativeUrl);
  577. }
  578. // Authentication
  579. public static bool IsAuthenticationIsSupported() {
  580. CheckPlatform();
  581. return plugin.CallStatic<bool>("isAuthenticationIsSupported");
  582. }
  583. public static void AuthenticationInit(string name, string url, string scheme) {
  584. CheckPlatform();
  585. plugin.CallStatic("authenticationInit", name, url, scheme);
  586. }
  587. public static void AuthenticationStart(string name) {
  588. CheckPlatform();
  589. plugin.CallStatic("authenticationStart", name);
  590. }
  591. public static void AuthenticationSetPrivateMode(string name, bool enabled) {
  592. CheckPlatform();
  593. plugin.CallStatic("authenticationSetPrivateMode", name, enabled);
  594. }
  595. public static bool SetEmbeddedToolbarConfig(string name, string json) {
  596. CheckPlatform();
  597. return plugin.CallStatic<bool>("setEmbeddedToolbarConfig", name, json);
  598. }
  599. public static void SetShowEmbeddedToolbar(string name, bool show) {
  600. CheckPlatform();
  601. plugin.CallStatic("setShowEmbeddedToolbar", name, show);
  602. }
  603. public static void SetEmbeddedToolbarOnTop(string name, bool top) {
  604. CheckPlatform();
  605. plugin.CallStatic("setEmbeddedToolbarOnTop", name, top);
  606. }
  607. public static void SetEmbeddedToolbarDoneButtonText(string name, string text) {
  608. CheckPlatform();
  609. plugin.CallStatic("setEmbeddedToolbarDoneButtonText", name, text);
  610. }
  611. public static void SetEmbeddedToolbarGoBackButtonText(string name, string text) {
  612. CheckPlatform();
  613. plugin.CallStatic("setEmbeddedToolbarGoBackButtonText", name, text);
  614. }
  615. public static void SetEmbeddedToolbarGoForwardButtonText(string name, string text) {
  616. CheckPlatform();
  617. plugin.CallStatic("setEmbeddedToolbarGoForwardButtonText", name, text);
  618. }
  619. public static void SetEmbeddedToolbarTitleText(string name, string text) {
  620. CheckPlatform();
  621. plugin.CallStatic("setEmbeddedToolbarTitleText", name, text);
  622. }
  623. public static void SetEmbeddedToolbarBackgroundColor(string name, Color color) {
  624. CheckPlatform();
  625. plugin.CallStatic("setEmbeddedToolbarBackgroundColor", name, color.r, color.g, color.b, color.a);
  626. }
  627. public static void SetEmbeddedToolbarButtonTextColor(string name, Color color) {
  628. CheckPlatform();
  629. plugin.CallStatic("setEmbeddedToolbarButtonTextColor", name, color.r, color.g, color.b, color.a);
  630. }
  631. public static void SetEmbeddedToolbarTitleTextColor(string name, Color color) {
  632. CheckPlatform();
  633. plugin.CallStatic("setEmbeddedToolbarTitleTextColor", name, color.r, color.g, color.b, color.a);
  634. }
  635. public static void SetEmeddedToolbarNavigationButtonsShow(string name, bool show) {
  636. CheckPlatform();
  637. plugin.CallStatic("setEmbeddedToolbarNavigationButtonsShow", name, show);
  638. }
  639. public static void SetEmbeddedToolbarMaxHeight(string name, float height) {
  640. CheckPlatform();
  641. plugin.CallStatic("setEmbeddedToolbarMaxHeight", name, height);
  642. }
  643. public static void StartSnapshotForRendering(string name, string identifier) {
  644. CheckPlatform();
  645. plugin.CallStatic("startSnapshotForRendering", name, identifier);
  646. }
  647. public static void StopSnapshotForRendering(string name) {
  648. CheckPlatform();
  649. plugin.CallStatic("stopSnapshotForRendering", name);
  650. }
  651. public static byte[] GetRenderedData(string name, int x, int y, int width, int height) {
  652. CheckPlatform();
  653. var sbyteArray = plugin.CallStatic<sbyte[]>("getRenderedData", name, x, y, width, height);
  654. if (sbyteArray == null) {
  655. return null;
  656. }
  657. byte[] byteArray = new byte[sbyteArray.Length];
  658. // sbyte and byte share the same bit layout; a bulk copy avoids a per-element loop over a
  659. // multi-megabyte image buffer.
  660. Buffer.BlockCopy(sbyteArray, 0, byteArray, 0, sbyteArray.Length);
  661. return byteArray;
  662. }
  663. public static bool StartSnapshotTextureStream(string name, long streamId, int x, int y, int width, int height, float resolutionScale) {
  664. CheckPlatform();
  665. LogSnapshotTextureStreamCpuFallbackIfNeeded();
  666. if (!Mathf.Approximately(resolutionScale, 1.0f)) {
  667. UniWebViewLogger.Instance.Info("Snapshot texture stream resolutionScale is not supported on Android yet. Capturing at full resolution.");
  668. }
  669. return plugin.CallStatic<bool>("startSnapshotTextureStream", name, streamId, x, y, width, height);
  670. }
  671. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  672. [return: MarshalAs(UnmanagedType.I1)]
  673. private static extern bool uv_startSnapshotTextureStreamSynthetic(string name, long streamId, int width, int height, int framePattern);
  674. public static bool StartSnapshotTextureStreamWithSyntheticFrames(
  675. string name, long streamId, int width, int height, int framePattern
  676. ) {
  677. CheckPlatform();
  678. LogSnapshotTextureStreamCpuFallbackIfNeeded();
  679. return uv_startSnapshotTextureStreamSynthetic(name, streamId, width, height, framePattern);
  680. }
  681. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  682. private static extern void uv_stopSnapshotTextureStream(string name, long streamId);
  683. public static void StopSnapshotTextureStream(string name, long streamId) {
  684. CheckPlatform();
  685. uv_stopSnapshotTextureStream(name, streamId);
  686. plugin.CallStatic("stopSnapshotTextureStream", name, streamId);
  687. if (!SnapshotTextureStreamUsesCpuFallback()) {
  688. IssueSnapshotTextureStreamRenderEvents(SnapshotTextureStreamStopRenderEventCount);
  689. }
  690. }
  691. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  692. private static extern void uv_tickSnapshotTextureStream(string name, long streamId);
  693. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  694. private static extern IntPtr uv_getSnapshotTextureStreamRenderEventFunc();
  695. public static void TickSnapshotTextureStream(string name, long streamId) {
  696. CheckPlatform();
  697. plugin.CallStatic("tickSnapshotTextureStream", name, streamId);
  698. uv_tickSnapshotTextureStream(name, streamId);
  699. }
  700. public static void PumpSnapshotTextureStream(string name, long streamId) {
  701. CheckPlatform();
  702. if (SnapshotTextureStreamUsesCpuFallback()) {
  703. return;
  704. }
  705. IssueSnapshotTextureStreamRenderEvent();
  706. }
  707. private static void IssueSnapshotTextureStreamRenderEvent() {
  708. GL.IssuePluginEvent(uv_getSnapshotTextureStreamRenderEventFunc(), 0);
  709. }
  710. private static void IssueSnapshotTextureStreamRenderEvents(int count) {
  711. for (var i = 0; i < count; i++) {
  712. IssueSnapshotTextureStreamRenderEvent();
  713. }
  714. }
  715. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  716. [return: MarshalAs(UnmanagedType.I1)]
  717. private static extern bool uv_isSnapshotTextureStreamReady(string name, long streamId);
  718. public static bool IsSnapshotTextureStreamReady(string name, long streamId) {
  719. CheckPlatform();
  720. return uv_isSnapshotTextureStreamReady(name, streamId);
  721. }
  722. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  723. private static extern int uv_getSnapshotTextureStreamWidth(string name, long streamId);
  724. public static int GetSnapshotTextureStreamWidth(string name, long streamId) {
  725. CheckPlatform();
  726. return uv_getSnapshotTextureStreamWidth(name, streamId);
  727. }
  728. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  729. private static extern int uv_getSnapshotTextureStreamHeight(string name, long streamId);
  730. public static int GetSnapshotTextureStreamHeight(string name, long streamId) {
  731. CheckPlatform();
  732. return uv_getSnapshotTextureStreamHeight(name, streamId);
  733. }
  734. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  735. private static extern long uv_getSnapshotTextureStreamFrameIndex(string name, long streamId);
  736. public static long GetSnapshotTextureStreamFrameIndex(string name, long streamId) {
  737. CheckPlatform();
  738. return uv_getSnapshotTextureStreamFrameIndex(name, streamId);
  739. }
  740. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  741. private static extern IntPtr uv_getSnapshotTextureStreamTexturePointer(string name, long streamId);
  742. public static IntPtr GetSnapshotTextureStreamTexturePointer(string name, long streamId) {
  743. CheckPlatform();
  744. return uv_getSnapshotTextureStreamTexturePointer(name, streamId);
  745. }
  746. public static TextureFormat GetSnapshotTextureStreamTextureFormat() {
  747. CheckPlatform();
  748. return TextureFormat.RGBA32;
  749. }
  750. [System.Runtime.InteropServices.DllImport("UniWebViewNativeTexture")]
  751. private static extern long uv_consumeSnapshotTextureStreamCpuFrame(string name, long streamId, IntPtr destination, int capacity);
  752. public static long ConsumeSnapshotTextureStreamCpuFrame(string name, long streamId, IntPtr destination, int capacity) {
  753. CheckPlatform();
  754. return uv_consumeSnapshotTextureStreamCpuFrame(name, streamId, destination, capacity);
  755. }
  756. // The optimized native texture path renders through OpenGL ES. On other graphics backends
  757. // (Vulkan), the stream falls back to a CPU readback that updates a stream-owned Texture2D.
  758. public static bool SnapshotTextureStreamUsesCpuFallback() {
  759. CheckPlatform();
  760. return !IsSnapshotTextureStreamGLBackend();
  761. }
  762. private static void LogSnapshotTextureStreamCpuFallbackIfNeeded() {
  763. if (SnapshotTextureStreamUsesCpuFallback()) {
  764. UniWebViewLogger.Instance.Info(
  765. "Snapshot texture stream is using the CPU readback fallback since the current " +
  766. "graphics backend is not OpenGL ES: " + SystemInfo.graphicsDeviceType
  767. );
  768. }
  769. }
  770. private static bool IsSnapshotTextureStreamGLBackend() {
  771. #if UNITY_2023_1_OR_NEWER
  772. // OpenGL ES 2.0 is no longer a selectable graphics API since Unity 2023.1.
  773. return SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES3;
  774. #else
  775. return SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES2 ||
  776. SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES3;
  777. #endif
  778. }
  779. public static string CopyBackForwardList(string name) {
  780. CheckPlatform();
  781. return plugin.CallStatic<string>("copyBackForwardList", name);
  782. }
  783. public static void GoToIndexInBackForwardList(string listenerName, int index) {
  784. CheckPlatform();
  785. plugin.CallStatic("goToIndexInBackForwardList", listenerName, index);
  786. }
  787. // Platform
  788. public static void CheckPlatform() {
  789. if (!correctPlatform) {
  790. throw new System.InvalidOperationException("Method can only be performed on Android.");
  791. }
  792. }
  793. }
  794. #endif