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