/*************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2015 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by all applicable intellectual property laws,
* including trade secret and or copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
import{analytics as e}from"../../common/analytics.js";import{dcLocalStorage as s}from"../../common/local-storage.js";import{loggingApi as t}from"../../common/loggingApi.js";import{common as a}from"../../sw_modules/common.js";import{PERF_MARKERS as r}from"../../sw_modules/perf-tracker.js";await s.init();const i=(e,s)=>{e&&chrome.runtime.sendMessage({main_op:"analytics",analytics:[e],tab:{id:s}})};class o{constructor(){this.urlParams=new URLSearchParams(window.location.search),this.env=this.urlParams.get("env")||"prod",this.locale=this.urlParams.get("locale"),this.tabId=Number(this.urlParams.get("tabId")),this.requestId=this.urlParams.get("requestId"),this.context=this.urlParams.get("context"),this.theme=this.urlParams.get("theme"),this.imsca=this.urlParams.get("imsca"),this.setupIframe(),this.setupMessageListeners()}mark(e,s){((e,s,t)=>{chrome.runtime.sendMessage({main_op:"kw-perf-mark",data:{requestId:s,name:e,metadata:t}})})(e,this.requestId,s)}static parentOrigin(){return window.location.ancestorOrigins?window.document.location.ancestorOrigins.length&&window.document.location.ancestorOrigins[0]:window.parent.location.origin}setupIframe(){a.reset(this.env),this.iframeElement=document.getElementById("addWebpageToProjectCdnHostedIframe");const e=a.getAddWebpageToProjectUrl(),t=new URL(e),i="false"!==s.getItem("logAnalytics"),n="false"!==s.getItem("ANALYTICS_OPT_IN_ADMIN");t.searchParams.set("ev",chrome.runtime.getManifest().version),t.searchParams.set("tabId",this.tabId),t.searchParams.set("fgContextVars",JSON.stringify(s.getItem("fgContextVars")||{})),t.searchParams.set("theme",this.theme),t.searchParams.set("extensionId",chrome.runtime.id),t.searchParams.set("requestId",this.requestId),t.searchParams.set("context",this.context),t.searchParams.set("la",i&&n),t.searchParams.set("imsca",this.imsca),t.searchParams.set("usedc","true"),this.locale&&t.searchParams.set("locale",this.locale),this.iframeUrl=t.toString(),this.origin=t.origin,this.pOrigin=o.parentOrigin(),this.setupIframeWithTimeout(),this.mark(r.AWPP_HOSTED_IFRAME_LOAD_STARTED),this.iframeElement.src=this.iframeUrl}setupIframeWithTimeout(){let s=!1;const t=setTimeout(()=>{s||(this.mark(r.AWPP_HOSTED_IFRAME_LOAD_TIMEOUT),this.sendMessageToParent({data:{main_op:"closeAllIframes"}}))},3e4);this.iframeElement.onload=()=>{this.mark(r.AWPP_HOSTED_IFRAME_LOADED),i(e.e.AWPP_HOSTED_IFRAME_LOADED,this.tabId),this.isCdnReady.then(()=>{s=!0}).catch(()=>{s=!1}).finally(()=>{clearTimeout(t)})},this.iframeElement.onerror=s=>{this.mark(r.AWPP_HOSTED_IFRAME_ERROR,{error:s?.toString()}),i(e.e.AWPP_HOSTED_IFRAME_ERROR,this.tabId),clearTimeout(t)}}isCdnReady=new Promise((e,s)=>{const a=setTimeout(()=>{t.error({message:"AddWebpageToProject cdn hosted iframe ready event timeout",requestId:this.requestId}),s(new Error("AddWebpageToProject cdn hosted iframe ready event timeout"))},6e4);this.isCdnReadyResolver=()=>{this.mark(r.AWPP_HOSTED_IFRAME_READY_EVENT_RESOLVED),clearTimeout(a),e()}});removeMessageListeners(){this.messageHandler&&(window.removeEventListener("message",this.messageHandler),this.messageHandler=null)}setupMessageListeners(){this.removeMessageListeners(),this.messageHandler=e=>{if(e.origin!==this.origin)return;const{main_op:a=""}=e.data;switch(a){case"closeAllIframes":case"openCollectionTab":case"kw-perf-mark":this.sendMessageToParent(e);break;case"authenticationComplete":this.handleAuthComplete();break;case"cdnReady":this.isCdnReadyResolver();break;case"getTabTitle":e.data.tabId?(async()=>{const s=await chrome.tabs.get(e.data.tabId);this.sendMessageToIframe({data:{messageId:e.data.messageId,tabTitle:s?.title||""}})})():this.sendMessageToIframe({data:{messageId:e.data.messageId,tabTitle:""}});break;case"getSignedInCachedffResponse_hostedExtension":{const t=s.getItem(`ffResponse_${e.data.userId}`)||"{}";this.sendMessageToIframe({data:{messageId:e.data.messageId,ffResponse:t,anonUserUUID:s.getItem("anonUserUUID")}});break}case"getAnonymousCachedffResponse_hostedExtension":{const t=s.getItem("ffResponse_anon")||"{}";this.sendMessageToIframe({data:{messageId:e.data.messageId,ffResponse:t,anonUserUUID:s.getItem("anonUserUUID")}});break}case"setFloodgateResponseFrom_hostedExtension":chrome.runtime.sendMessage({main_op:"handleViewerFloodgateResponse",fgResponse:e.data.fgResponse});break;case"getCachedData":{const t=s.getItem(e.data.key);this.sendMessageToIframe({data:{messageId:e.data.messageId,response:t}});break}case"setCachedData":s.setItem(e.data.key,e.data.value).then(()=>{this.sendMessageToIframe({data:{messageId:e.data.messageId,response:!0}})});break;case"check-ims-cookie":chrome.runtime.sendMessage({main_op:"check-ims-cookie"}).then(s=>{this.sendMessageToIframe({data:{messageId:e.data.messageId,isAvailable:s}})});break;default:t.info({message:"AddWebpageToProject cdn hosted iframe unknown event",event:JSON.stringify(e.data)})}},window.addEventListener("message",this.messageHandler)}static supportedOrigin(e){try{return["https://dev.acrobat.adobe.com","https://stage.acrobat.adobe.com","https://acrobat.adobe.com","https://documentcloud.adobe.com","https://local-test.acrobat.adobe.com:1900"].includes(e)}catch(e){return!1}}sendMessageToIframe(e){this.iframeElement&&o.supportedOrigin(this.origin)&&this.isCdnReady.then(()=>{this.iframeElement.contentWindow.postMessage({...e.data},this.origin)}).catch(e=>{t.error({message:"Error in forwarding message to AddWebpageToProject cdn hosted iframe",error:e.toString(),requestId:this.requestId})})}sendMessageToParent(e){window.parent.postMessage({...e.data},this.pOrigin)}handleAuthComplete(){t.info({message:"Sign in complete using susi light; reloading AddWebpageToProject cdn hosted iframe",requestId:this.requestId}),window.location.reload()}}const n=new o;export default n;