Fix telemetry from child windows of Data Explorer (#633)

* Fix telemetry from child windows of Data Explorer

* Address feedback
This commit is contained in:
Tanuj Mittal 2021-04-09 00:22:41 -07:00 committed by GitHub
parent 3ab6b2a05d
commit 37e0f50ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 24 deletions

View File

@ -48,32 +48,18 @@ export function sendCachedDataMessage<TResponseDataModel>(
} }
export function sendMessage(data: any): void { export function sendMessage(data: any): void {
if (canSendMessage()) { _sendMessage({
// We try to find data explorer window first, then fallback to current window
const portalChildWindow = getDataExplorerWindow(window) || window;
portalChildWindow.parent.postMessage(
{
signature: "pcIframe", signature: "pcIframe",
data: data, data: data,
}, });
portalChildWindow.document.referrer || "*"
);
}
} }
export function sendReadyMessage(): void { export function sendReadyMessage(): void {
if (canSendMessage()) { _sendMessage({
// We try to find data explorer window first, then fallback to current window
const portalChildWindow = getDataExplorerWindow(window) || window;
portalChildWindow.parent.postMessage(
{
signature: "pcIframe", signature: "pcIframe",
kind: "ready", kind: "ready",
data: "ready", data: "ready",
}, });
portalChildWindow.document.referrer || "*"
);
}
} }
export function canSendMessage(): boolean { export function canSendMessage(): boolean {
@ -89,3 +75,17 @@ export function runGarbageCollector() {
} }
}); });
} }
const _sendMessage = (message: any): void => {
if (canSendMessage()) {
// Portal window can receive messages from only child windows
const portalChildWindow = getDataExplorerWindow(window) || window;
if (portalChildWindow === window) {
// Current window is a child of portal, send message to portal window
portalChildWindow.parent.postMessage(message, portalChildWindow.document.referrer || "*");
} else {
// Current window is not a child of portal, send message to the child window instead (which is data explorer)
portalChildWindow.postMessage(message, portalChildWindow.location.origin || "*");
}
}
};

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 { sendReadyMessage } from "../Common/MessageHandler"; import { sendMessage, 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";
@ -266,6 +266,8 @@ async function configurePortal(explorerParams: ExplorerParams): Promise<Explorer
if (openAction) { if (openAction) {
handleOpenAction(openAction, explorer.databases(), explorer); handleOpenAction(openAction, explorer.databases(), explorer);
} }
} else if (shouldForwardMessage(message, event.origin)) {
sendMessage(message);
} }
}, },
false false
@ -275,6 +277,11 @@ async function configurePortal(explorerParams: ExplorerParams): Promise<Explorer
}); });
} }
function shouldForwardMessage(message: PortalMessage, messageOrigin: string) {
// Only allow forwarding messages from the same origin
return messageOrigin === window.document.location.origin && message.type === MessageTypes.TelemetryInfo;
}
function shouldProcessMessage(event: MessageEvent): boolean { function shouldProcessMessage(event: MessageEvent): boolean {
if (typeof event.data !== "object") { if (typeof event.data !== "object") {
return false; return false;