Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/labs/useragent/useragent.js
4085 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Defines for goog.labs.userAgent.
9
*/
10
11
goog.module('goog.labs.userAgent');
12
goog.module.declareLegacyNamespace();
13
14
const flags = goog.require('goog.flags');
15
16
/**
17
* @define {string} Optional runtime override for the USE_CLIENT_HINTS flag.
18
* If this is set (for example, to 'foo.bar') then any value of USE_CLIENT_HINTS
19
* will be overridden by `globalThis.foo.bar` if it is non-null.
20
* This flag will be removed in December 2021.
21
*/
22
const USE_CLIENT_HINTS_OVERRIDE =
23
goog.define('goog.labs.userAgent.USE_CLIENT_HINTS_OVERRIDE', '');
24
25
/**
26
* @define {boolean} If true, use navigator.userAgentData. Note: this overrides
27
* the `USE_USER_AGENT_CLIENT_HINTS` runtime flag. Please prefer the flag when
28
* possible.
29
*/
30
const USE_CLIENT_HINTS =
31
goog.define('goog.labs.userAgent.USE_CLIENT_HINTS', false);
32
33
let forceClientHintsInTests = false;
34
35
/**
36
* Sets whether to use client hints APIs in tests for codepaths that
37
* - were originally implemented as checks against the navigator.userAgent
38
* string.
39
* - have an alternative implementation that uses Client Hints APIs.
40
*
41
* See the jsdoc on useClientHints for cases where this flag will be
42
* ineffective, and the Client Hints APIs would be used regardless.
43
* DO NOT call this function in production code - it will cause de-optimization.
44
* @param {boolean} use Whether or not to use Client Hints API codepaths in
45
* goog.labs.useragent.* modules.
46
*/
47
exports.setUseClientHintsForTesting = (use) => {
48
forceClientHintsInTests = use;
49
};
50
51
/** @const {boolean} */
52
const useClientHintsRuntimeOverride = USE_CLIENT_HINTS_OVERRIDE ?
53
!!goog.getObjectByName(USE_CLIENT_HINTS_OVERRIDE) :
54
false;
55
56
/**
57
* Whether to use UserAgent-Client Hints API surfaces in parts of the
58
* labs.userAgent package that previously only relied on the navigator.userAgent
59
* string. Newer labs.userAgent API surfaces may ignore the result of this
60
* function as they are considered opt-in API surfaces.
61
* @const {function():boolean}
62
*/
63
exports.useClientHints = () => {
64
return flags.USE_USER_AGENT_CLIENT_HINTS || USE_CLIENT_HINTS ||
65
useClientHintsRuntimeOverride || forceClientHintsInTests;
66
};
67
68