UserAgent.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. */
  8. 'use strict';
  9. var UserAgentData = require("./UserAgentData");
  10. var VersionRange = require("./VersionRange");
  11. var mapObject = require("./mapObject");
  12. var memoizeStringOnly = require("./memoizeStringOnly");
  13. /**
  14. * Checks to see whether `name` and `version` satisfy `query`.
  15. *
  16. * @param {string} name Name of the browser, device, engine or platform
  17. * @param {?string} version Version of the browser, engine or platform
  18. * @param {string} query Query of form "Name [range expression]"
  19. * @param {?function} normalizer Optional pre-processor for range expression
  20. * @return {boolean}
  21. */
  22. function compare(name, version, query, normalizer) {
  23. // check for exact match with no version
  24. if (name === query) {
  25. return true;
  26. } // check for non-matching names
  27. if (!query.startsWith(name)) {
  28. return false;
  29. } // full comparison with version
  30. var range = query.slice(name.length);
  31. if (version) {
  32. range = normalizer ? normalizer(range) : range;
  33. return VersionRange.contains(range, version);
  34. }
  35. return false;
  36. }
  37. /**
  38. * Normalizes `version` by stripping any "NT" prefix, but only on the Windows
  39. * platform.
  40. *
  41. * Mimics the stripping performed by the `UserAgentWindowsPlatform` PHP class.
  42. *
  43. * @param {string} version
  44. * @return {string}
  45. */
  46. function normalizePlatformVersion(version) {
  47. if (UserAgentData.platformName === 'Windows') {
  48. return version.replace(/^\s*NT/, '');
  49. }
  50. return version;
  51. }
  52. /**
  53. * Provides client-side access to the authoritative PHP-generated User Agent
  54. * information supplied by the server.
  55. */
  56. var UserAgent = {
  57. /**
  58. * Check if the User Agent browser matches `query`.
  59. *
  60. * `query` should be a string like "Chrome" or "Chrome > 33".
  61. *
  62. * Valid browser names include:
  63. *
  64. * - ACCESS NetFront
  65. * - AOL
  66. * - Amazon Silk
  67. * - Android
  68. * - BlackBerry
  69. * - BlackBerry PlayBook
  70. * - Chrome
  71. * - Chrome for iOS
  72. * - Chrome frame
  73. * - Facebook PHP SDK
  74. * - Facebook for iOS
  75. * - Firefox
  76. * - IE
  77. * - IE Mobile
  78. * - Mobile Safari
  79. * - Motorola Internet Browser
  80. * - Nokia
  81. * - Openwave Mobile Browser
  82. * - Opera
  83. * - Opera Mini
  84. * - Opera Mobile
  85. * - Safari
  86. * - UIWebView
  87. * - Unknown
  88. * - webOS
  89. * - etc...
  90. *
  91. * An authoritative list can be found in the PHP `BrowserDetector` class and
  92. * related classes in the same file (see calls to `new UserAgentBrowser` here:
  93. * https://fburl.com/50728104).
  94. *
  95. * @note Function results are memoized
  96. *
  97. * @param {string} query Query of the form "Name [range expression]"
  98. * @return {boolean}
  99. */
  100. isBrowser: function isBrowser(query) {
  101. return compare(UserAgentData.browserName, UserAgentData.browserFullVersion, query);
  102. },
  103. /**
  104. * Check if the User Agent browser uses a 32 or 64 bit architecture.
  105. *
  106. * @note Function results are memoized
  107. *
  108. * @param {string} query Query of the form "32" or "64".
  109. * @return {boolean}
  110. */
  111. isBrowserArchitecture: function isBrowserArchitecture(query) {
  112. return compare(UserAgentData.browserArchitecture, null, query);
  113. },
  114. /**
  115. * Check if the User Agent device matches `query`.
  116. *
  117. * `query` should be a string like "iPhone" or "iPad".
  118. *
  119. * Valid device names include:
  120. *
  121. * - Kindle
  122. * - Kindle Fire
  123. * - Unknown
  124. * - iPad
  125. * - iPhone
  126. * - iPod
  127. * - etc...
  128. *
  129. * An authoritative list can be found in the PHP `DeviceDetector` class and
  130. * related classes in the same file (see calls to `new UserAgentDevice` here:
  131. * https://fburl.com/50728332).
  132. *
  133. * @note Function results are memoized
  134. *
  135. * @param {string} query Query of the form "Name"
  136. * @return {boolean}
  137. */
  138. isDevice: function isDevice(query) {
  139. return compare(UserAgentData.deviceName, null, query);
  140. },
  141. /**
  142. * Check if the User Agent rendering engine matches `query`.
  143. *
  144. * `query` should be a string like "WebKit" or "WebKit >= 537".
  145. *
  146. * Valid engine names include:
  147. *
  148. * - Gecko
  149. * - Presto
  150. * - Trident
  151. * - WebKit
  152. * - etc...
  153. *
  154. * An authoritative list can be found in the PHP `RenderingEngineDetector`
  155. * class related classes in the same file (see calls to `new
  156. * UserAgentRenderingEngine` here: https://fburl.com/50728617).
  157. *
  158. * @note Function results are memoized
  159. *
  160. * @param {string} query Query of the form "Name [range expression]"
  161. * @return {boolean}
  162. */
  163. isEngine: function isEngine(query) {
  164. return compare(UserAgentData.engineName, UserAgentData.engineVersion, query);
  165. },
  166. /**
  167. * Check if the User Agent platform matches `query`.
  168. *
  169. * `query` should be a string like "Windows" or "iOS 5 - 6".
  170. *
  171. * Valid platform names include:
  172. *
  173. * - Android
  174. * - BlackBerry OS
  175. * - Java ME
  176. * - Linux
  177. * - Mac OS X
  178. * - Mac OS X Calendar
  179. * - Mac OS X Internet Account
  180. * - Symbian
  181. * - SymbianOS
  182. * - Windows
  183. * - Windows Mobile
  184. * - Windows Phone
  185. * - iOS
  186. * - iOS Facebook Integration Account
  187. * - iOS Facebook Social Sharing UI
  188. * - webOS
  189. * - Chrome OS
  190. * - etc...
  191. *
  192. * An authoritative list can be found in the PHP `PlatformDetector` class and
  193. * related classes in the same file (see calls to `new UserAgentPlatform`
  194. * here: https://fburl.com/50729226).
  195. *
  196. * @note Function results are memoized
  197. *
  198. * @param {string} query Query of the form "Name [range expression]"
  199. * @return {boolean}
  200. */
  201. isPlatform: function isPlatform(query) {
  202. return compare(UserAgentData.platformName, UserAgentData.platformFullVersion, query, normalizePlatformVersion);
  203. },
  204. /**
  205. * Check if the User Agent platform is a 32 or 64 bit architecture.
  206. *
  207. * @note Function results are memoized
  208. *
  209. * @param {string} query Query of the form "32" or "64".
  210. * @return {boolean}
  211. */
  212. isPlatformArchitecture: function isPlatformArchitecture(query) {
  213. return compare(UserAgentData.platformArchitecture, null, query);
  214. }
  215. };
  216. module.exports = mapObject(UserAgent, memoizeStringOnly);