1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {ClientInfo, SegmentInfo} from './segmentation_internals.mojom-webui.js';
import {SegmentationInternalsBrowserProxy} from './segmentation_internals_browser_proxy.js';
function getProxy(): SegmentationInternalsBrowserProxy {
return SegmentationInternalsBrowserProxy.getInstance();
}
// Checks the given URL for expected pattern, and allowed list of param.
function getSafeURL(untrustedURL: URL|undefined): URL|undefined {
if (untrustedURL === undefined) {
return undefined;
}
const allowedList = [
'https://www.google.com/search',
'https://surveys.qualtrics.com/jfe/form/SV_cNMWDhaegCrsrsy',
'https://surveys.qualtrics.com/jfe/form/SV_40HhSVolVzTvBNI',
];
const allowedParams = ['ID', 'SG', 'Q_CHL', 'Q_DL', '_g_', 'QRID', 'CR', 'q'];
const withoutParams: string = untrustedURL.origin + untrustedURL.pathname;
if (!allowedList.includes(withoutParams)) {
return undefined;
}
const trustedURL = new URL(withoutParams);
untrustedURL.searchParams.forEach((value, key) => {
if (allowedParams.includes(key)) {
trustedURL.searchParams.append(key, value);
}
});
return trustedURL;
}
function openSurvey(result: string|undefined) {
try {
const untrustedParam =
new URL(window.location.href).searchParams.get('url');
if (untrustedParam) {
const untrustedURL = new URL(decodeURIComponent(untrustedParam));
const trustedURL = getSafeURL(untrustedURL);
if (trustedURL) {
if (result) {
trustedURL.searchParams.append('CR', encodeURIComponent(result));
}
window.location.href = trustedURL.toString();
return true;
}
}
} catch (error) {
}
return false;
}
function openError() {
window.location.href = 'chrome://network-error/-404';
}
function processPredictionResult(segmentInfo: SegmentInfo) {
if (!segmentInfo.predictionResult || segmentInfo.predictionResult === '' ||
segmentInfo.predictionTimestamp.internalValue === 0n ||
segmentInfo.segmentId !== 50 ||
!segmentInfo.segmentData.includes('model_version: 2')) {
openError();
return;
}
const result = String(segmentInfo.predictionResult) +
' Timestamp: ' + String(segmentInfo.predictionTimestamp.internalValue);
const encoded = window.btoa(result);
if (!openSurvey(encoded)) {
openError();
}
}
function processClientInfo(info: ClientInfo) {
for (let i = 0; i < info.segmentInfo.length; ++i) {
processPredictionResult(info.segmentInfo[i]!);
}
}
function initialize() {
// Timeout to wait for segmentation results.
const timeoutSec = 3000;
const timeoutId = setTimeout(() => {
openError();
}, timeoutSec);
getProxy().getCallbackRouter().onClientInfoAvailable.addListener(
(clientInfos: ClientInfo[]) => {
for (let i = 0; i < clientInfos.length; ++i) {
if (clientInfos[i]!.segmentationKey === 'metrics_clustering') {
clearTimeout(timeoutId);
processClientInfo(clientInfos[i]!);
}
}
});
// OPTIMIZATION_TARGET_SEGMENTATION_METRICS_CLUSTERING
getProxy().executeModel(50);
}
document.addEventListener('DOMContentLoaded', initialize);
|