"use strict"; var CloudExperienceHost; (function (CloudExperienceHost) { var AppManager = (function () { function AppManager(view) { this._appView = null; this._description = null; this._correlationId = null; this._bridge = null; this._discovery = null; this._navigator = null; this._currentNode = null; this._hasNotifiedFirstVisible = false; this._resultsOperation = null; this._msaUIHandler = null; this._visibilityTimer = null; this._appResult = CloudExperienceHost.AppResult.fail; this._platform = null; this._failFromCxh = "CXHInternalFail"; this._appView = view; WinJS.Namespace.define("CloudExperienceHost", { getVersion: this._getVersion.bind(this), getContext: this._getContext.bind(this), getCurrentNode: function () { return this._currentNode; }.bind(this), fail: function () { this._appResult = CloudExperienceHost.AppResult.fail; this._close(); }.bind(this), }); } AppManager.prototype.start = function (args) { this._start(false, args); }; AppManager.prototype.resume = function (args) { this._start(true, args); }; AppManager.prototype.checkpoint = function () { }; AppManager.prototype.restart = function (scenario) { if (scenario) { this._description = CloudExperienceHost.ExperienceDescription.Create(scenario); } this._navigate(); }; AppManager.prototype.onUnhandledException = function (e) { return new WinJS.Promise(function (completeDispatch, errorDispatch ) { try { var data = e.detail; var logData = new Object; logData["exp"] = CloudExperienceHost.ExperienceDescription.getExperience(this._description); logData["cxid"] = this._currentNode && this._currentNode.cxid; logData["errorCode"] = data && (data.number || (data.exception && (data.exception.number || data.exception.code)) || (data.error && data.error.number) || data.errorCode || 0); logData["message"] = data && (data.message || data.errorMessage || (data.error && data.error.message) || (data.exception && data.exception.message) || null); logData["stack"] = data && (data.stack || (data.exception && (data.exception.stack || data.exception.message)) || (data.error && data.error.stack) || null); CloudExperienceHost.Telemetry.AppTelemetry.getInstance().logEvent("UnhandledException", JSON.stringify(logData)); if (this._platform === CloudExperienceHost.TargetPlatform.XBOX) { this._appResult = this._failFromCxh; this._close(); } else { var shouldShowAccountErrorPageOnFailurebyDefault = CloudExperienceHost.getContext() && (CloudExperienceHost.getContext().host.toLowerCase() === "frx") && (CloudExperienceHost.Environment.getPlatform() === CloudExperienceHost.TargetPlatform.DESKTOP); this._loadErrorPage(this._currentNode ? this._currentNode.showAccountErrorPageOnFailure : shouldShowAccountErrorPageOnFailurebyDefault).done(completeDispatch, errorDispatch); } } catch (error) { AppManager.prototype.onUnhandledException = function () { return null; }; throw error; } }.bind(this)); }; AppManager.prototype._start = function (resumed, args) { this._platform = CloudExperienceHost.Environment.getPlatform(); var scenario = this._getScenario(args); if (this._getDescription(scenario)) { this._appView.loadCss(); var stateManager = CloudExperienceHost.StateManager.getInstance(); var cxid = (resumed && stateManager.isValid(scenario)) ? stateManager.getNextCXID() : null; stateManager.setSource(scenario); this._navigate(cxid); } else { window.close(); } }; AppManager.prototype._getVersion = function () { return 1; }; AppManager.prototype._getScenario = function (args) { var scenario = null; switch (args.detail.kind) { case Windows.ApplicationModel.Activation.ActivationKind.launch: scenario = args.detail.arguments; this._resultsOperation = this._getResultOperationForXbox(); break; case Windows.ApplicationModel.Activation.ActivationKind.protocol: scenario = args.detail.uri.absoluteCanonicalUri; if (args.detail.uri.schemeName.toLowerCase() === "ms-device-enrollment") { if (args.detail.uri.absoluteCanonicalUri.toLowerCase() === "ms-device-enrollment:?mode=mdm") { scenario = "ms-cxh://mosetMDMconnecttowork/"; } else { scenario = "ms-cxh://"; } } this._resultsOperation = this._getResultOperationForXbox(); break; case Windows.ApplicationModel.Activation.ActivationKind.protocolForResults: scenario = args.detail.uri.absoluteUri; this._resultsOperation = args.detail.protocolForResultsOperation; break; default: throw new Error(CloudExperienceHost.ErrorNames.ActivationNotSupported); break; } return scenario; }; AppManager.prototype._getDescription = function (scenario) { if (scenario) { this._description = CloudExperienceHost.ExperienceDescription.Create(scenario); if (this._description) { this._correlationId = CloudExperienceHost.ExperienceDescription.GetCorrelationId(this._description); return true; } } return false; }; AppManager.prototype._getContext = function () { if (this._description) { var context = new CloudExperienceHost.Context(); context.source = this._description.source; context.protocol = this._description.protocol; context.host = this._description.host; context.platform = this._platform; context.capabilities = JSON.stringify({ "PrivatePropertyBag": 1 }); return context; } else { return null; } }; AppManager.prototype._navigate = function (cxid) { var descriptionAllowlist = [ 'experience', 'protocol', 'host', 'port', 'params', 'file', 'hash', 'path', 'segments' ]; CloudExperienceHost.Telemetry.AppTelemetry.getInstance().start(this._description.source.split("?")[0], JSON.stringify(this._description, descriptionAllowlist).toString(), this._correlationId); this._appView.showProgress().then(function () { this._create().then(function () { this._discovery.getNavMesh(this._description).then(function (navMesh) { this._navigator.navigate(navMesh, cxid).done(); }.bind(this)); }.bind(this)); }.bind(this)); }; AppManager.prototype._create = function () { return new WinJS.Promise(function (completeDispatch, errorDispatch ) { var webViewCtrl; webViewCtrl = this._appView.createWebView(); this._appView.cleanView(); this._appView.getView().appendChild(webViewCtrl); webViewCtrl.setAttribute("id", "x-ms-webview"); if (this._platform === CloudExperienceHost.TargetPlatform.XBOX) { webViewCtrl.focus(); } this._discovery = new CloudExperienceHost.Discovery(); this._discovery.getApiRules().then(function (rules) { var contractHandler = new CloudExperienceHost.ContractHandler(rules); this._bridge = new CloudExperienceHost.Bridge(webViewCtrl, contractHandler); this._bridge.addEventListener(CloudExperienceHost.Events.visible, this._onVisible.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.goBack, this._onGoBack.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.done, this._onDone.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.navigate, this._onNavigate.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.showEaseOfAccessControl, this._onShowEaseOfAccessControl.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.loadIdentityProvider, this._onLoadIdentityProvider.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.postTicketToReturnUrl, this._onPostTicketToReturnUrl.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.postDeviceTicketToUrl, this._onPostDeviceTicketToUrl.bind(this)); this._bridge.addEventListener(CloudExperienceHost.Events.registerNGCForUser, this._onRegisterNGCForUser.bind(this)); this._navigator = new CloudExperienceHost.Navigator(webViewCtrl, contractHandler); this._navigator.addEventListener("Error", this._onError.bind(this)); this._navigator.addEventListener("NavigationStarting", this._onNavigationStarting.bind(this)); this._navigator.addEventListener("NavigationCompleted", this._onNavigationCompleted.bind(this)); if (this._platform === CloudExperienceHost.TargetPlatform.XBOX) { window.addEventListener("focus", this._onFocus.bind(this), false); } completeDispatch(); }.bind(this), errorDispatch); }.bind(this)); }; AppManager.prototype._onFocus = function () { var webViewCtrl = document.getElementById("x-ms-webview"); webViewCtrl.focus(); webViewCtrl.focus(); }; AppManager.prototype._onNavigationStarting = function (node) { CloudExperienceHost.StateManager.getInstance().onNavigate(node); }; AppManager.prototype._startVisibilityTimer = function () { var timeout = 15000; if (this._currentNode.timeout) { timeout = this._currentNode.timeout; } this._visibilityTimer = WinJS.Promise.timeout(timeout).then(function () { CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("VisibilityTimeout", this._currentNode.cxid); this._onDone(CloudExperienceHost.AppResult.fail, true); }.bind(this)); }; AppManager.prototype._stopVisibilityTimer = function () { if (this._visibilityTimer) { this._visibilityTimer.cancel(); this._visibilityTimer = null; } }; AppManager.prototype._onNavigationCompleted = function (node) { if ((this._currentNode) && (this._currentNode.cxid === node.cxid)) { this._onVisible(true); } else { this._stopVisibilityTimer(); this._currentNode = node; if ((typeof (this._currentNode.visibility) === 'undefined') || (this._currentNode.visibility === true)) { this._onVisible(true); } else { this._startVisibilityTimer(); } } }; AppManager.prototype._notifyFirstWebAppVisibleIfNecessary = function () { if (!this._hasNotifiedFirstVisible) { this._hasNotifiedFirstVisible = true; CloudExperienceHost.Telemetry.AppTelemetry.getInstance().logEvent("FirstWebAppVisible", this._currentNode && this._currentNode.cxid); if (this._navigator.getNavMesh().getNotifyOnFirstVisible()) { CloudExperienceHostAPI.Synchronization.onFirstOOBEWebAppVisible(); } } }; AppManager.prototype._loadErrorPage = function (showAccountErrorPageOnFailure) { return new WinJS.Promise(function (completeDispatch, errorDispatch ) { this._appView.cleanView(); this._appView.showProgress().then(function () { WinJS.UI.Pages.render("views/errorHandler.html", this._appView.getView(), showAccountErrorPageOnFailure).done(function () { this._appView.showView().done(completeDispatch, errorDispatch); }.bind(this)); }.bind(this)); }.bind(this)); }; AppManager.prototype._onError = function (e) { if (e.node) { this._currentNode = e.node; } if (e.node && (e.node.showAccountErrorPageOnFailure || e.node.showErrorPageOnFailure)) { this._loadErrorPage(e.node.showAccountErrorPageOnFailure ? true : false).done(function () { this._notifyFirstWebAppVisibleIfNecessary(); }.bind(this)); } else { if (CloudExperienceHost.Environment.hasInternetAccess()) { this._onDone(CloudExperienceHost.AppResult.fail, true); } else { this._appResult = CloudExperienceHost.AppResult.success; this._close(); } } }; AppManager.prototype._onVisible = function (arg) { CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("Visible", arg); this._stopVisibilityTimer(); if (arg === true) { this._appView.showView().done(function () { this._notifyFirstWebAppVisibleIfNecessary(); }.bind(this)); } }; AppManager.prototype._onGoBack = function () { CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("GoBack"); this._appView.showProgress().then(function () { this._navigator.goBack(); }.bind(this)); }; AppManager.prototype._onDone = function (result, isInternalResult) { this._stopVisibilityTimer(); CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("Done", result); CloudExperienceHost.StateManager.getInstance().onDone(this._currentNode, result); if ((typeof (this._currentNode.ignoreResult) === 'undefined') || (this._currentNode.ignoreResult === false)) { this._appResult = result; } if ((this._platform === CloudExperienceHost.TargetPlatform.XBOX) && (this._appResult === CloudExperienceHost.AppResult.fail) && isInternalResult) { this._appResult = this._failFromCxh; } if (this._navigator.webAppDone(result)) { this._appView.showProgress().then(function () { this._navigator.goNext(); }.bind(this)); } else { this._close(); } }; AppManager.prototype._onNavigate = function (e) { var target = ((typeof e === "string") ? new CloudExperienceHost.RedirectEventArgs(e) : e); CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("Navigate", JSON.stringify({ httpMethod: target.httpMethod, url: CloudExperienceHost.UriHelper.RemovePIIFromUri(target.url) })); this._appView.showProgress().then(function () { this._navigator.redirect(target); }.bind(this)); }; AppManager.prototype._onShowEaseOfAccessControl = function (boundingRectOfEOAButton) { if (!boundingRectOfEOAButton) { var boundingRectangleOfWindow = this._appView.getBoundingClientRect(); boundingRectOfEOAButton = { left: boundingRectangleOfWindow.left, top: boundingRectangleOfWindow.bottom, right: boundingRectangleOfWindow.left, bottom: boundingRectangleOfWindow.bottom }; } CloudExperienceHost.showEaseOfAccessFlyout(boundingRectOfEOAButton).then(function () { }, function (e) { CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent(e); }); }; AppManager.prototype._onLoadIdentityProvider = function (signInIdentityProvider) { CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("LoadIdentityProvider", signInIdentityProvider); this._appView.showProgress().then(function () { this._navigator.loadIdentityProvider(signInIdentityProvider); }.bind(this)); }; AppManager.prototype._onTicketError = function (targetUrl, errorMsg, errorCode) { var logData = new Object; logData["cxid"] = this._currentNode && this._currentNode.cxid; logData["errorCode"] = errorCode || null; logData["message"] = errorMsg || null; logData["targetUrl"] = targetUrl; CloudExperienceHost.Telemetry.WebAppTelemetry.getInstance().logEvent("TicketRequestError", JSON.stringify(logData)); var error = null; switch (errorCode) { case -2147023665: error = Windows.Web.WebErrorStatus.serverUnreachable; break; } this._onError(new CloudExperienceHost.NavigationError(error, targetUrl, this._currentNode, errorMsg || errorCode.toString())); }; AppManager.prototype._onTicketRequestComplete = function (targetUrl, result) { if (targetUrl) { this._navigator.redirect(new CloudExperienceHost.RedirectEventArgs(targetUrl, null, result, result ? "POST" : "GET")); } else { this._onTicketError(targetUrl, result, -2147012891 ); } }; AppManager.prototype._onPostTicketToReturnUrl = function (data) { this._msaUIHandler = this._msaUIHandler || new CloudExperienceHost.MSAUIHandlerInternal(this._appView); var msaTicketContext = this._navigator.getNavMesh().getMsaTicketContext(); this._msaUIHandler.requestTicketForUrl(data, msaTicketContext, this._onNavigate.bind(this)).done(function (redirectArgs) { this._navigator.redirect(redirectArgs); }.bind(this), function (error) { this._onTicketError(data.targetUrl, "", error.number); }.bind(this)); }; AppManager.prototype._onRegisterNGCForUser = function (data) { this._msaUIHandler = this._msaUIHandler || new CloudExperienceHost.MSAUIHandlerInternal(this._appView); this._msaUIHandler.registerNGCForUser(data, this._onNavigate.bind(this)).done(this._onTicketRequestComplete.bind(this), function (error) { var destinationUrl = data.returnUrl; var hexString = "0x" + (error.number < 0 ? error.number + 0x100000000 : error.number).toString(16); destinationUrl += (destinationUrl.indexOf('?') > 0 ? '&' : '?') + "errorCode=" + encodeURIComponent(hexString); this._navigator.redirect(new CloudExperienceHost.RedirectEventArgs(destinationUrl)); }.bind(this)); }; AppManager.prototype._onPostDeviceTicketToUrl = function (data) { var targetUrl = data.targetUrl; var policy = data.policy; var guid = "49BB5C55-7CF8-49EA-BE52-9FEC226F728C"; this._appView.showProgress().then(function () { var msaExtension = new MicrosoftAccount.Extension.ExtensionWorker(); msaExtension.getDeviceTicketForWebFlowAsync(targetUrl, policy, guid).done(function (result) { this._onTicketRequestComplete(result.ResultUrl, result.Ticket); }.bind(this), function (error) { this._onTicketError(targetUrl, error.message, error.number); }.bind(this)); }.bind(this)); }; AppManager.prototype._close = function () { var cxhResult = (this._appResult !== CloudExperienceHost.AppResult.fail); CloudExperienceHost.Telemetry.AppTelemetry.getInstance().stop(this._appResult); CloudExperienceHost.StateManager.getInstance().clean(); if (this._resultsOperation != null) { var valueSet = new Windows.Foundation.Collections.ValueSet(); valueSet.insert("Result", this._appResult); this._resultsOperation.reportCompleted(valueSet); } else { CloudExperienceHostAPI.Synchronization.reportResult(cxhResult); } if (this._navigator && this._navigator.getNavMesh() && this._navigator.getNavMesh().getNotifyOnLastFinished()) { CloudExperienceHostAPI.Synchronization.onLastOOBEWebAppFinished(cxhResult); } else { window.close(); } }; AppManager.prototype._getResultOperationForXbox = function () { var resultOperation = null; if (CloudExperienceHost.Environment.getPlatform() === CloudExperienceHost.TargetPlatform.XBOX) { try { var tcuiContext = Windows.Xbox.UI.Internal.TCUIStateManager.getContext(); if (tcuiContext != null) { resultOperation = new CloudExperienceHost.XboxTcuiContext(tcuiContext); } } catch (e) { this.onUnhandledException(e); } } return resultOperation; }; return AppManager; })(); CloudExperienceHost.AppManager = AppManager; })(CloudExperienceHost || (CloudExperienceHost = {}));