| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using UnityEngine; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using Environment = DCL.Environment; |
| | 9 | |
|
| | 10 | | public class ExternalUrlPromptHUDController : IHUD |
| | 11 | | { |
| 0 | 12 | | internal ExternalUrlPromptView view { get; private set; } |
| | 13 | |
|
| 5 | 14 | | internal Dictionary<string, HashSet<string>> trustedDomains = new Dictionary<string, HashSet<string>>(); |
| | 15 | |
|
| 5 | 16 | | public ExternalUrlPromptHUDController() |
| | 17 | | { |
| 5 | 18 | | view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("ExternalUrlPromptHUD")).GetComponent<ExternalU |
| 5 | 19 | | view.name = "_ExternalUrlPromptHUD"; |
| 5 | 20 | | view.content.SetActive(false); |
| | 21 | |
|
| 5 | 22 | | if (Environment.i != null) |
| 5 | 23 | | Environment.i.world.sceneController.OnOpenExternalUrlRequest += ProcessOpenUrlRequest; |
| 5 | 24 | | } |
| | 25 | |
|
| | 26 | | public void SetVisibility(bool visible) |
| | 27 | | { |
| 10 | 28 | | view.gameObject.SetActive(visible); |
| | 29 | |
|
| 10 | 30 | | if (visible) |
| | 31 | | { |
| 6 | 32 | | view.content.SetActive(true); |
| 6 | 33 | | view.showHideAnimator.Show(); |
| | 34 | |
|
| 6 | 35 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 6 | 36 | | } |
| | 37 | | else |
| | 38 | | { |
| 4 | 39 | | view.showHideAnimator.Hide(); |
| | 40 | |
|
| 4 | 41 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 42 | | } |
| 4 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Dispose() |
| | 46 | | { |
| 5 | 47 | | if (Environment.i != null) |
| 5 | 48 | | Environment.i.world.sceneController.OnOpenExternalUrlRequest -= ProcessOpenUrlRequest; |
| | 49 | |
|
| 5 | 50 | | trustedDomains.Clear(); |
| | 51 | |
|
| 5 | 52 | | if (view != null) |
| 5 | 53 | | UnityEngine.Object.Destroy(view.gameObject); |
| 5 | 54 | | } |
| | 55 | |
|
| | 56 | | internal void ProcessOpenUrlRequest(IParcelScene scene, string url) |
| | 57 | | { |
| | 58 | | Uri uri; |
| 6 | 59 | | if (Uri.TryCreate(url, UriKind.Absolute, out uri)) |
| | 60 | | { |
| 6 | 61 | | if (trustedDomains.ContainsKey(scene.sceneData.id) && trustedDomains[scene.sceneData.id].Contains(uri.Host)) |
| | 62 | | { |
| 1 | 63 | | OpenUrl(url); |
| 1 | 64 | | return; |
| | 65 | | } |
| | 66 | |
|
| 5 | 67 | | SetVisibility(true); |
| 5 | 68 | | Utils.UnlockCursor(); |
| 5 | 69 | | view.RequestOpenUrl(uri, result => |
| | 70 | | { |
| | 71 | | switch (result) |
| | 72 | | { |
| | 73 | | case ExternalUrlPromptView.ResultType.APPROVED_TRUSTED: |
| 1 | 74 | | if (!trustedDomains.ContainsKey(scene.sceneData.id)) |
| | 75 | | { |
| 1 | 76 | | trustedDomains.Add(scene.sceneData.id, new HashSet<string>()); |
| | 77 | | } |
| | 78 | |
|
| 1 | 79 | | trustedDomains[scene.sceneData.id].Add(uri.Host); |
| 1 | 80 | | OpenUrl(url); |
| 1 | 81 | | break; |
| | 82 | | case ExternalUrlPromptView.ResultType.APPROVED: |
| 1 | 83 | | OpenUrl(url); |
| | 84 | | break; |
| | 85 | | } |
| | 86 | |
|
| 4 | 87 | | SetVisibility(false); |
| 4 | 88 | | }); |
| | 89 | | } |
| 5 | 90 | | } |
| | 91 | |
|
| | 92 | | private void OpenUrl(string url) |
| | 93 | | { |
| 3 | 94 | | WebInterface.OpenURL(url); |
| 3 | 95 | | AnalyticsHelper.SendExternalLinkAnalytic(url); |
| 3 | 96 | | } |
| | 97 | | } |