Path: blob/trunk/third_party/closure/goog/labs/useragent/useragent.js
4085 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Defines for goog.labs.userAgent.8*/910goog.module('goog.labs.userAgent');11goog.module.declareLegacyNamespace();1213const flags = goog.require('goog.flags');1415/**16* @define {string} Optional runtime override for the USE_CLIENT_HINTS flag.17* If this is set (for example, to 'foo.bar') then any value of USE_CLIENT_HINTS18* will be overridden by `globalThis.foo.bar` if it is non-null.19* This flag will be removed in December 2021.20*/21const USE_CLIENT_HINTS_OVERRIDE =22goog.define('goog.labs.userAgent.USE_CLIENT_HINTS_OVERRIDE', '');2324/**25* @define {boolean} If true, use navigator.userAgentData. Note: this overrides26* the `USE_USER_AGENT_CLIENT_HINTS` runtime flag. Please prefer the flag when27* possible.28*/29const USE_CLIENT_HINTS =30goog.define('goog.labs.userAgent.USE_CLIENT_HINTS', false);3132let forceClientHintsInTests = false;3334/**35* Sets whether to use client hints APIs in tests for codepaths that36* - were originally implemented as checks against the navigator.userAgent37* string.38* - have an alternative implementation that uses Client Hints APIs.39*40* See the jsdoc on useClientHints for cases where this flag will be41* ineffective, and the Client Hints APIs would be used regardless.42* DO NOT call this function in production code - it will cause de-optimization.43* @param {boolean} use Whether or not to use Client Hints API codepaths in44* goog.labs.useragent.* modules.45*/46exports.setUseClientHintsForTesting = (use) => {47forceClientHintsInTests = use;48};4950/** @const {boolean} */51const useClientHintsRuntimeOverride = USE_CLIENT_HINTS_OVERRIDE ?52!!goog.getObjectByName(USE_CLIENT_HINTS_OVERRIDE) :53false;5455/**56* Whether to use UserAgent-Client Hints API surfaces in parts of the57* labs.userAgent package that previously only relied on the navigator.userAgent58* string. Newer labs.userAgent API surfaces may ignore the result of this59* function as they are considered opt-in API surfaces.60* @const {function():boolean}61*/62exports.useClientHints = () => {63return flags.USE_USER_AGENT_CLIENT_HINTS || USE_CLIENT_HINTS ||64useClientHintsRuntimeOverride || forceClientHintsInTests;65};666768