From 41e7935bd56c0df423302a95695eab9f83cad586 Mon Sep 17 00:00:00 2001 From: Stephen Hicks Date: Fri, 24 Sep 2021 10:50:33 -0700 Subject: [PATCH] RELNOTES[NEW]: Provide goog.labs.userAgent.USE_CLIENT_HINTS_OVERRIDE as a runtime-override mechanism for overriding USE_CLIENT_HINTS in production builds. PiperOrigin-RevId: 398765743 Change-Id: I48aec9a4b0e70835867d6344813fcb4653cf8f95 --- closure/goog/labs/useragent/useragent.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/closure/goog/labs/useragent/useragent.js b/closure/goog/labs/useragent/useragent.js index 1c3330de32..d6ab484b50 100644 --- a/closure/goog/labs/useragent/useragent.js +++ b/closure/goog/labs/useragent/useragent.js @@ -10,10 +10,30 @@ goog.module('goog.labs.userAgent'); +/** + * @define {string} Optional runtime override for the USE_CLIENT_HINTS flag. + * If this is set (for example, to 'foo.bar') then any value of USE_CLIENT_HINTS + * will be overridden by `globalThis.foo.bar` if it is non-null. + * This flag will be removed in December 2021. + */ +const USE_CLIENT_HINTS_OVERRIDE = + goog.define('goog.labs.userAgent.USE_CLIENT_HINTS_OVERRIDE', ''); + /** * @define {boolean} If true, use navigator.userAgentData * TODO(user) Flip flag in 2021/12. */ const USE_CLIENT_HINTS = goog.define('goog.labs.userAgent.USE_CLIENT_HINTS', false); -exports.USE_CLIENT_HINTS = USE_CLIENT_HINTS; + +// TODO(user): Replace the IIFE with a simple null-coalescing operator. +// NOTE: This can't be done with a helper function, or else we risk an inlining +// back-off causing a huge code size regression if a non-inlined helper function +// prevents the optimizer from detecting the (possibly large) dead code paths. +/** @const {boolean} */ +exports.USE_CLIENT_HINTS = (() => { + const override = USE_CLIENT_HINTS_OVERRIDE ? + goog.getObjectByName(USE_CLIENT_HINTS_OVERRIDE) : + null; + return override != null ? override : USE_CLIENT_HINTS; +})();