Skip to content
Merged
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
36 changes: 34 additions & 2 deletions scripts/patch-linux-window-ui.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2820,7 +2820,7 @@ test("auto-installs the current Chrome plugin gate shape", () => {

assert.match(
patched,
/\{forceReload:!0,installWhenMissing:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:\(\{buildFlavor:e,features:t\}\)=>t\.externalBrowserUseAllowed&&\$n\(e\)\}/,
/\{forceReload:!0,installWhenMissing:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:\(\{buildFlavor:e,features:t\}\)=>process\.platform===`linux`\|\|\(t\.externalBrowserUseAllowed&&\$n\(e\)\)\}/,
);
assert.match(patched, /name:xt,syncInstallStateWithChromeExtension:!0,isAvailable:\(\{buildFlavor:e,env:t,features:n\}\)=>Ar\(e,t\)&&n\.externalBrowserUseAllowed/);
assert.match(patched, /name:dt,syncInstallStateWithChromeExtension:!0,isAvailable:\(\{buildFlavor:e,env:t,features:n\}\)=>jr\(e,t\)&&n\.externalBrowserUseAllowed/);
Expand Down Expand Up @@ -2904,22 +2904,54 @@ test("reports drifted Chrome native host runtime resolver as required upstream f
}
});

test("keeps an already auto-installed Chrome plugin gate unchanged", () => {
test("adds Linux availability to an already auto-installed Chrome plugin gate", () => {
const source = currentPluginGateBundleFixture().replace(
"{forceReload:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:",
"{forceReload:!0,installWhenMissing:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:",
);

const patched = applyPatchTwice(applyLinuxChromePluginAutoInstallPatch, source);

assert.match(
patched,
/installWhenMissing:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:\(\{buildFlavor:e,features:t\}\)=>process\.platform===`linux`\|\|\(t\.externalBrowserUseAllowed&&\$n\(e\)\)/,
);
});

test("keeps a fully Linux-enabled Chrome plugin gate unchanged", () => {
const source = currentPluginGateBundleFixture().replace(
"{forceReload:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:({buildFlavor:e,features:t})=>t.externalBrowserUseAllowed&&$n(e)}",
"{forceReload:!0,installWhenMissing:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:({buildFlavor:e,features:t})=>process.platform===`linux`||(t.externalBrowserUseAllowed&&$n(e))}",
);

assert.equal(applyPatchTwice(applyLinuxChromePluginAutoInstallPatch, source), source);
});

test("does not treat unrelated Linux platform checks as Chrome plugin availability", () => {
const source = currentPluginGateBundleFixture()
.replace(
"{forceReload:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:",
"{forceReload:!0,installWhenMissing:!0,name:ut,syncInstallStateWithChromeExtension:!0,isAvailable:",
)
.replace(
"({buildFlavor:e,features:t})=>t.externalBrowserUseAllowed&&$n(e)}",
"function({features:t}){return t.externalBrowserUseAllowed}}",
) + "var __codexOtherLinuxPatch=process.platform===`linux`;";

assert.throws(
() => applyLinuxChromePluginAutoInstallPatch(source),
/Required Linux Chrome plugin auto-install patch failed/,
);
});

test("handles literal Chrome plugin gate names", () => {
const source =
"var Kr=[{forceReload:!0,name:'chrome',isEnabled:({features:t})=>t.externalBrowserUseAllowed},{forceReload:!0,name:'chrome-internal',isEnabled:({features:t})=>t.externalBrowserUseAllowed}];";

const patched = applyPatchTwice(applyLinuxChromePluginAutoInstallPatch, source);

assert.match(patched, /installWhenMissing:!0,name:'chrome'/);
assert.match(patched, /process\.platform===`linux`\|\|\(t\.externalBrowserUseAllowed\)/);
assert.doesNotMatch(patched, /installWhenMissing:!0,name:'chrome-internal'/);
});

Expand Down
28 changes: 23 additions & 5 deletions scripts/patches/chrome-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ function isChromeNameExpr(nameExpr, chromeNameVar) {
nameExpr === chromeNameVar;
}

function hasChromeAutoInstall(source, chromeNameVar) {
function chromeNamePatterns(chromeNameVar) {
const namePatterns = [String.raw`\`chrome\``, "\"chrome\"", "'chrome'"];
if (chromeNameVar != null) {
namePatterns.push(chromeNameVar);
}
return new RegExp(String.raw`installWhenMissing:!0,name:(?:${namePatterns.join("|")})`).test(source);
return namePatterns;
}

function hasLinuxChromeAvailability(source) {
return source.includes("process.platform===`linux`");
}

function hasChromeAutoInstallWithLinuxAvailability(source, chromeNameVar) {
const namePatterns = chromeNamePatterns(chromeNameVar);
return new RegExp(
String.raw`\{(?=[^{}]*installWhenMissing:!0)(?=[^{}]*name:(?:${namePatterns.join("|")}))(?=[^{}]*process\.platform===\`linux\`)[^{}]*(?:isEnabled|isAvailable):[^{}]*\}`,
).test(source);
}

function applyLinuxChromePluginAutoInstallPatch(currentSource) {
Expand Down Expand Up @@ -62,20 +73,27 @@ function applyLinuxChromePluginAutoInstallPatch(currentSource) {
}

sawChromeGate = true;
if (installWhenMissing != null || prefix.includes("installWhenMissing:!0")) {
const hasInstallWhenMissing = installWhenMissing != null ||
prefix.includes("installWhenMissing:!0");
const hasLinuxAvailability = hasLinuxChromeAvailability(expression);
if (hasInstallWhenMissing && hasLinuxAvailability) {
sawAlreadyInstalledGate = true;
return gateSource;
}

return `{${prefix}installWhenMissing:!0,name:${nameExpr},${middleFields}${availabilityProp}:({${paramsText}})=>${expression}${migrateSuffix}}`;
const installWhenMissingField = hasInstallWhenMissing ? (installWhenMissing ?? "") : "installWhenMissing:!0,";
const availabilityExpression = hasLinuxAvailability
? expression
: `process.platform===\`linux\`||(${expression})`;
return `{${prefix}${installWhenMissingField}name:${nameExpr},${middleFields}${availabilityProp}:({${paramsText}})=>${availabilityExpression}${migrateSuffix}}`;
},
);

if (patched !== currentSource || (sawChromeGate && sawAlreadyInstalledGate)) {
return patched;
}

if (hasChromeAutoInstall(currentSource, chromeNameVar)) {
if (hasChromeAutoInstallWithLinuxAvailability(currentSource, chromeNameVar)) {
return currentSource;
}

Expand Down
Loading