UserAgent.js 6.0 KB

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