Fix ready message sent to parent frame

This commit is contained in:
artrejo 2021-03-18 20:41:43 -07:00
parent 3cccbdfe81
commit f929a638d6
6 changed files with 35 additions and 20 deletions

View File

@ -1,8 +1,8 @@
import { MessageTypes } from "../Contracts/ExplorerContracts";
import Q from "q"; import Q from "q";
import * as _ from "underscore"; import * as _ from "underscore";
import * as Constants from "./Constants"; import { MessageTypes } from "../Contracts/ExplorerContracts";
import { getDataExplorerWindow } from "../Utils/WindowUtils"; import { getDataExplorerWindow } from "../Utils/WindowUtils";
import * as Constants from "./Constants";
export interface CachedDataPromise<T> { export interface CachedDataPromise<T> {
deferred: Q.Deferred<T>; deferred: Q.Deferred<T>;
@ -61,6 +61,21 @@ export function sendMessage(data: any): void {
} }
} }
export function sendReadyMessage(): void {
if (canSendMessage()) {
// We try to find data explorer window first, then fallback to current window
const portalChildWindow = getDataExplorerWindow(window) || window;
portalChildWindow.parent.postMessage(
{
signature: "pcIframe",
kind: "ready",
},
portalChildWindow.document.referrer
);
}
}
export function canSendMessage(): boolean { export function canSendMessage(): boolean {
return window.parent !== window; return window.parent !== window;
} }

View File

@ -1,5 +1,10 @@
import * as Plotly from "plotly.js-cartesian-dist-min";
import dayjs from "dayjs"; import dayjs from "dayjs";
import * as Plotly from "plotly.js-cartesian-dist-min";
import { StyleConstants } from "../../Common/Constants";
import { sendCachedDataMessage, sendReadyMessage } from "../../Common/MessageHandler";
import { MessageTypes } from "../../Contracts/ExplorerContracts";
import { isInvalidParentFrameOrigin } from "../../Utils/MessageValidation";
import "./Heatmap.less";
import { import {
ChartSettings, ChartSettings,
DataPayload, DataPayload,
@ -9,13 +14,8 @@ import {
HeatmapData, HeatmapData,
LayoutSettings, LayoutSettings,
PartitionTimeStampToData, PartitionTimeStampToData,
PortalTheme, PortalTheme
} from "./HeatmapDatatypes"; } from "./HeatmapDatatypes";
import { isInvalidParentFrameOrigin } from "../../Utils/MessageValidation";
import { sendCachedDataMessage, sendMessage } from "../../Common/MessageHandler";
import { MessageTypes } from "../../Contracts/ExplorerContracts";
import { StyleConstants } from "../../Common/Constants";
import "./Heatmap.less";
export class Heatmap { export class Heatmap {
public static readonly elementId: string = "heatmap"; public static readonly elementId: string = "heatmap";
@ -266,4 +266,4 @@ export function handleMessage(event: MessageEvent) {
} }
window.addEventListener("message", handleMessage, false); window.addEventListener("message", handleMessage, false);
sendMessage("ready"); sendReadyMessage();

View File

@ -83,10 +83,10 @@ export default class MongoShellTab extends TabsBase {
} }
private handleReadyMessage(event: MessageEvent, shellIframe: HTMLIFrameElement) { private handleReadyMessage(event: MessageEvent, shellIframe: HTMLIFrameElement) {
if (typeof event.data["data"] !== "string") { if (typeof event.data["kind"] !== "string") {
return; return;
} }
if (event.data.data !== "ready") { if (event.data.kind !== "ready") {
return; return;
} }
@ -100,7 +100,7 @@ export default class MongoShellTab extends TabsBase {
documentEndpoint.substr( documentEndpoint.substr(
Constants.MongoDBAccounts.protocol.length + 3, Constants.MongoDBAccounts.protocol.length + 3,
documentEndpoint.length - documentEndpoint.length -
(Constants.MongoDBAccounts.protocol.length + 2 + Constants.MongoDBAccounts.defaultPort.length) (Constants.MongoDBAccounts.protocol.length + 2 + Constants.MongoDBAccounts.defaultPort.length)
) + Constants.MongoDBAccounts.defaultPort.toString(); ) + Constants.MongoDBAccounts.defaultPort.toString();
const databaseId = this.collection.databaseId; const databaseId = this.collection.databaseId;
const collectionId = this.collection.id(); const collectionId = this.collection.id();

View File

@ -3,7 +3,7 @@ import { initializeIcons } from "office-ui-fabric-react/lib/Icons";
import * as React from "react"; import * as React from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import { normalizeArmEndpoint } from "../Common/EnvironmentUtility"; import { normalizeArmEndpoint } from "../Common/EnvironmentUtility";
import { sendMessage } from "../Common/MessageHandler"; import { sendReadyMessage } from "../Common/MessageHandler";
import { configContext, updateConfigContext } from "../ConfigContext"; import { configContext, updateConfigContext } from "../ConfigContext";
import { SelfServeFrameInputs } from "../Contracts/ViewModels"; import { SelfServeFrameInputs } from "../Contracts/ViewModels";
import { updateUserContext } from "../UserContext"; import { updateUserContext } from "../UserContext";
@ -89,4 +89,4 @@ const handleMessage = async (event: MessageEvent): Promise<void> => {
ReactDOM.render(renderSpinner(), document.getElementById("selfServeContent")); ReactDOM.render(renderSpinner(), document.getElementById("selfServeContent"));
window.addEventListener("message", handleMessage, false); window.addEventListener("message", handleMessage, false);
sendMessage("ready"); sendReadyMessage();

View File

@ -3,7 +3,7 @@ import { applyExplorerBindings } from "../applyExplorerBindings";
import { AuthType } from "../AuthType"; import { AuthType } from "../AuthType";
import { AccountKind, DefaultAccountExperience } from "../Common/Constants"; import { AccountKind, DefaultAccountExperience } from "../Common/Constants";
import { normalizeArmEndpoint } from "../Common/EnvironmentUtility"; import { normalizeArmEndpoint } from "../Common/EnvironmentUtility";
import { sendMessage } from "../Common/MessageHandler"; import { sendReadyMessage } from "../Common/MessageHandler";
import { configContext, Platform, updateConfigContext } from "../ConfigContext"; import { configContext, Platform, updateConfigContext } from "../ConfigContext";
import { ActionType, DataExplorerAction } from "../Contracts/ActionContracts"; import { ActionType, DataExplorerAction } from "../Contracts/ActionContracts";
import { MessageTypes } from "../Contracts/ExplorerContracts"; import { MessageTypes } from "../Contracts/ExplorerContracts";
@ -15,14 +15,14 @@ import {
ConnectionString, ConnectionString,
EncryptedToken, EncryptedToken,
HostedExplorerChildFrame, HostedExplorerChildFrame,
ResourceToken, ResourceToken
} from "../HostedExplorerChildFrame"; } from "../HostedExplorerChildFrame";
import { emulatorAccount } from "../Platform/Emulator/emulatorAccount"; import { emulatorAccount } from "../Platform/Emulator/emulatorAccount";
import { extractFeatures } from "../Platform/Hosted/extractFeatures"; import { extractFeatures } from "../Platform/Hosted/extractFeatures";
import { parseResourceTokenConnectionString } from "../Platform/Hosted/Helpers/ResourceTokenUtils"; import { parseResourceTokenConnectionString } from "../Platform/Hosted/Helpers/ResourceTokenUtils";
import { import {
getDatabaseAccountKindFromExperience, getDatabaseAccountKindFromExperience,
getDatabaseAccountPropertiesFromMetadata, getDatabaseAccountPropertiesFromMetadata
} from "../Platform/Hosted/HostedUtils"; } from "../Platform/Hosted/HostedUtils";
import { DefaultExperienceUtility } from "../Shared/DefaultExperienceUtility"; import { DefaultExperienceUtility } from "../Shared/DefaultExperienceUtility";
import { PortalEnv, updateUserContext } from "../UserContext"; import { PortalEnv, updateUserContext } from "../UserContext";
@ -274,7 +274,7 @@ async function configurePortal(explorerParams: ExplorerParams): Promise<Explorer
false false
); );
sendMessage("ready"); sendReadyMessage();
}); });
} }

View File

@ -79,7 +79,7 @@ const initTestExplorer = async (): Promise<void> => {
// After we have received the "ready" message from the child iframe we can post configuration // After we have received the "ready" message from the child iframe we can post configuration
// This simulates the same action that happens in the portal // This simulates the same action that happens in the portal
console.dir(event.data); console.dir(event.data);
if (event.data?.data === "ready") { if (event.data?.kind === "ready") {
iframe.contentWindow.postMessage( iframe.contentWindow.postMessage(
{ {
signature: "pcIframe", signature: "pcIframe",