Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/nativeSdkHelpers.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface INativeSdkHelpers {
sendToNative: (path: string, value?: string) => void;
sendViaBridgeV1: (path: string, value: string) => void;
sendViaIframeToIOS: (path: string, value: string) => void;
sendViaBridgeV2: (path: string, value: string, requiredWebviewBridgeName: boolean) => void;
sendViaBridgeV2: (path: string, value: string, requiredWebviewBridgeName: string) => void;
}
111 changes: 60 additions & 51 deletions src/nativeSdkHelpers.js → src/nativeSdkHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import Constants from './constants';
import { IMParticleWebSDKInstance } from './mp-instance';
import { INativeSdkHelpers } from './nativeSdkHelpers.interfaces';
import { Dictionary } from './utils';

var Messages = Constants.Messages;
const Messages = Constants.Messages;

var androidBridgeNameBase = 'mParticleAndroid';
var iosBridgeNameBase = 'mParticle';
const androidBridgeNameBase = 'mParticleAndroid';
const iosBridgeNameBase = 'mParticle';

export default function NativeSdkHelpers(mpInstance) {
var self = this;
this.initializeSessionAttributes = function(apiKey) {
export default function NativeSdkHelpers(
this: INativeSdkHelpers,
mpInstance: IMParticleWebSDKInstance
): void {
const self = this;

Check failure on line 15 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not assign `this` to `self`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEq&open=AZ4eNB2DxSI5yJcCioEq&pullRequest=1262
this.initializeSessionAttributes = function(apiKey: string): void {
const { SetSessionAttribute } = Constants.NativeSdkPaths;
const env = JSON.stringify({
key: '$src_env',
Expand All @@ -25,56 +31,55 @@
}
};

this.isBridgeV2Available = function(bridgeName) {
this.isBridgeV2Available = function(bridgeName: string): boolean {
if (!bridgeName) {
return false;
}
var androidBridgeName =
const androidBridgeName =
androidBridgeNameBase + '_' + bridgeName + '_v2';
var iosBridgeName = iosBridgeNameBase + '_' + bridgeName + '_v2';
const iosBridgeName = iosBridgeNameBase + '_' + bridgeName + '_v2';

// iOS v2 bridge
if (
window.webkit &&
window.webkit.messageHandlers &&
window.webkit.messageHandlers.hasOwnProperty(iosBridgeName)
(window as Dictionary).webkit &&

Check warning on line 44 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEs&open=AZ4eNB2DxSI5yJcCioEs&pullRequest=1262
(window as Dictionary).webkit.messageHandlers &&

Check warning on line 45 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEt&open=AZ4eNB2DxSI5yJcCioEt&pullRequest=1262
(window as Dictionary).webkit.messageHandlers.hasOwnProperty(iosBridgeName)

Check warning on line 46 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using an optional chain expression instead, as it's more concise and easier to read.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEr&open=AZ4eNB2DxSI5yJcCioEr&pullRequest=1262

Check warning on line 46 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEu&open=AZ4eNB2DxSI5yJcCioEu&pullRequest=1262
) {
return true;
}
// other iOS v2 bridge
// TODO: what to do about people setting things on mParticle itself?
if (
window.mParticle &&

Check warning on line 52 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEv&open=AZ4eNB2DxSI5yJcCioEv&pullRequest=1262
window.mParticle.uiwebviewBridgeName &&
window.mParticle.uiwebviewBridgeName === iosBridgeName
(window.mParticle as Dictionary).uiwebviewBridgeName &&

Check warning on line 53 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEw&open=AZ4eNB2DxSI5yJcCioEw&pullRequest=1262
(window.mParticle as Dictionary).uiwebviewBridgeName === iosBridgeName

Check warning on line 54 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEx&open=AZ4eNB2DxSI5yJcCioEx&pullRequest=1262
) {
return true;
}
// android
if (window.hasOwnProperty(androidBridgeName)) {

Check warning on line 59 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEy&open=AZ4eNB2DxSI5yJcCioEy&pullRequest=1262
return true;
}
return false;
};

this.isWebviewEnabled = function(
requiredWebviewBridgeName,
minWebviewBridgeVersion
) {
mpInstance._Store.bridgeV2Available = self.isBridgeV2Available(
requiredWebviewBridgeName: string,
minWebviewBridgeVersion: number
): boolean {
(mpInstance._Store as Dictionary).bridgeV2Available = self.isBridgeV2Available(
requiredWebviewBridgeName
);
mpInstance._Store.bridgeV1Available = self.isBridgeV1Available();
(mpInstance._Store as Dictionary).bridgeV1Available = self.isBridgeV1Available();

if (minWebviewBridgeVersion === 2) {
return mpInstance._Store.bridgeV2Available;
return (mpInstance._Store as Dictionary).bridgeV2Available;
}

// iOS BridgeV1 can be available via mParticle.isIOS, but return false if uiwebviewBridgeName doesn't match requiredWebviewBridgeName
if (window.mParticle) {

Check warning on line 79 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioEz&open=AZ4eNB2DxSI5yJcCioEz&pullRequest=1262
if (
window.mParticle.uiwebviewBridgeName &&
window.mParticle.uiwebviewBridgeName !==
(window.mParticle as Dictionary).uiwebviewBridgeName &&

Check warning on line 81 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE0&open=AZ4eNB2DxSI5yJcCioE0&pullRequest=1262
(window.mParticle as Dictionary).uiwebviewBridgeName !==

Check warning on line 82 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE1&open=AZ4eNB2DxSI5yJcCioE1&pullRequest=1262
iosBridgeNameBase + '_' + requiredWebviewBridgeName + '_v2'
) {
return false;
Expand All @@ -84,18 +89,18 @@
if (minWebviewBridgeVersion < 2) {
// ios
return (
mpInstance._Store.bridgeV2Available ||
mpInstance._Store.bridgeV1Available
(mpInstance._Store as Dictionary).bridgeV2Available ||
(mpInstance._Store as Dictionary).bridgeV1Available
);
}

return false;
};

this.isBridgeV1Available = function() {
this.isBridgeV1Available = function(): boolean {
if (
mpInstance._Store.SDKConfig.useNativeSdk ||
window.mParticleAndroid ||
(window as Dictionary).mParticleAndroid ||

Check warning on line 103 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE2&open=AZ4eNB2DxSI5yJcCioE2&pullRequest=1262
mpInstance._Store.SDKConfig.isIOS
) {
return true;
Expand All @@ -104,9 +109,9 @@
return false;
};

this.sendToNative = function(path, value) {
this.sendToNative = function(path: string, value?: string): void {
if (
mpInstance._Store.bridgeV2Available &&
(mpInstance._Store as Dictionary).bridgeV2Available &&
mpInstance._Store.SDKConfig.minWebviewBridgeVersion === 2
) {
self.sendViaBridgeV2(
Expand All @@ -117,7 +122,7 @@
return;
}
if (
mpInstance._Store.bridgeV2Available &&
(mpInstance._Store as Dictionary).bridgeV2Available &&
mpInstance._Store.SDKConfig.minWebviewBridgeVersion < 2
) {
self.sendViaBridgeV2(
Expand All @@ -128,23 +133,23 @@
return;
}
if (
mpInstance._Store.bridgeV1Available &&
(mpInstance._Store as Dictionary).bridgeV1Available &&
mpInstance._Store.SDKConfig.minWebviewBridgeVersion < 2
) {
self.sendViaBridgeV1(path, value);
return;

Check warning on line 140 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant jump.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE3&open=AZ4eNB2DxSI5yJcCioE3&pullRequest=1262
}
};

this.sendViaBridgeV1 = function(path, value) {
this.sendViaBridgeV1 = function(path: string, value: string): void {
if (
window.mParticleAndroid &&
window.mParticleAndroid.hasOwnProperty(path)
(window as Dictionary).mParticleAndroid &&

Check warning on line 146 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE5&open=AZ4eNB2DxSI5yJcCioE5&pullRequest=1262
(window as Dictionary).mParticleAndroid.hasOwnProperty(path)

Check warning on line 147 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE6&open=AZ4eNB2DxSI5yJcCioE6&pullRequest=1262

Check warning on line 147 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using an optional chain expression instead, as it's more concise and easier to read.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE4&open=AZ4eNB2DxSI5yJcCioE4&pullRequest=1262
) {
mpInstance.Logger.verbose(
Messages.InformationMessages.SendAndroid + path
);
window.mParticleAndroid[path](value);
(window as Dictionary).mParticleAndroid[path](value);

Check warning on line 152 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE7&open=AZ4eNB2DxSI5yJcCioE7&pullRequest=1262
} else if (mpInstance._Store.SDKConfig.isIOS) {
mpInstance.Logger.verbose(
Messages.InformationMessages.SendIOS + path
Expand All @@ -153,48 +158,52 @@
}
};

this.sendViaIframeToIOS = function(path, value) {
var iframe = document.createElement('IFRAME');
this.sendViaIframeToIOS = function(path: string, value: string): void {
const iframe = document.createElement('IFRAME');
iframe.setAttribute(
'src',
'mp-sdk://' + path + '/' + encodeURIComponent(value)
);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);

Check warning on line 168 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `childNode.remove()` over `parentNode.removeChild(childNode)`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE8&open=AZ4eNB2DxSI5yJcCioE8&pullRequest=1262
};

this.sendViaBridgeV2 = function(path, value, requiredWebviewBridgeName) {
this.sendViaBridgeV2 = function(
path: string,
value: string,
requiredWebviewBridgeName: string
): void {
if (!requiredWebviewBridgeName) {
return;
}

var androidBridgeName =
androidBridgeNameBase + '_' + requiredWebviewBridgeName + '_v2',
androidBridge = window[androidBridgeName],
iosBridgeName =
iosBridgeNameBase + '_' + requiredWebviewBridgeName + '_v2',
iOSBridgeMessageHandler,
iOSBridgeNonMessageHandler;
const androidBridgeName =
androidBridgeNameBase + '_' + requiredWebviewBridgeName + '_v2';
const androidBridge = (window as Dictionary)[androidBridgeName];

Check warning on line 182 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE9&open=AZ4eNB2DxSI5yJcCioE9&pullRequest=1262
const iosBridgeName =
iosBridgeNameBase + '_' + requiredWebviewBridgeName + '_v2';
let iOSBridgeMessageHandler: Dictionary;
let iOSBridgeNonMessageHandler: Dictionary;

if (
window.webkit &&
window.webkit.messageHandlers &&
window.webkit.messageHandlers[iosBridgeName]
(window as Dictionary).webkit &&

Check warning on line 189 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE_&open=AZ4eNB2DxSI5yJcCioE_&pullRequest=1262
(window as Dictionary).webkit.messageHandlers &&

Check warning on line 190 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioFA&open=AZ4eNB2DxSI5yJcCioFA&pullRequest=1262
(window as Dictionary).webkit.messageHandlers[iosBridgeName]

Check warning on line 191 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using an optional chain expression instead, as it's more concise and easier to read.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioE-&open=AZ4eNB2DxSI5yJcCioE-&pullRequest=1262

Check warning on line 191 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioFB&open=AZ4eNB2DxSI5yJcCioFB&pullRequest=1262
) {
iOSBridgeMessageHandler =
window.webkit.messageHandlers[iosBridgeName];
(window as Dictionary).webkit.messageHandlers[iosBridgeName];

Check warning on line 194 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioFC&open=AZ4eNB2DxSI5yJcCioFC&pullRequest=1262
}

if (mpInstance.uiwebviewBridgeName === iosBridgeName) {
iOSBridgeNonMessageHandler = mpInstance[iosBridgeName];
if ((mpInstance as Dictionary).uiwebviewBridgeName === iosBridgeName) {
iOSBridgeNonMessageHandler = (mpInstance as Dictionary)[iosBridgeName];
}

if (androidBridge && androidBridge.hasOwnProperty(path)) {

Check warning on line 201 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using an optional chain expression instead, as it's more concise and easier to read.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioFD&open=AZ4eNB2DxSI5yJcCioFD&pullRequest=1262
mpInstance.Logger.verbose(
Messages.InformationMessages.SendAndroid + path
);
androidBridge[path](value);
return;

Check warning on line 206 in src/nativeSdkHelpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant jump.

See more on https://sonarcloud.io/project/issues?id=mParticle_mparticle-web-sdk&issues=AZ4eNB2DxSI5yJcCioFE&open=AZ4eNB2DxSI5yJcCioFE&pullRequest=1262
} else if (iOSBridgeMessageHandler) {
mpInstance.Logger.verbose(
Messages.InformationMessages.SendIOS + path
Expand Down
Loading