Source: lib/util/platform.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.Platform');
  18. /**
  19. * A wrapper for platform-specific functions.
  20. *
  21. * @final
  22. */
  23. shaka.util.Platform = class {
  24. /**
  25. * Check if the current platform is MS Edge.
  26. *
  27. * @return {boolean}
  28. */
  29. static isEdge() {
  30. return shaka.util.Platform.userAgentContains_('Edge/');
  31. }
  32. /**
  33. * Check if the current platform is MS IE.
  34. *
  35. * @return {boolean}
  36. */
  37. static isIE() {
  38. return shaka.util.Platform.userAgentContains_('Trident/');
  39. }
  40. /**
  41. * Check if the current platform is a Tizen TV.
  42. *
  43. * @return {boolean}
  44. */
  45. static isTizen() {
  46. return shaka.util.Platform.userAgentContains_('Tizen');
  47. }
  48. /**
  49. * Check if the current platform is a Tizen 3 TV.
  50. *
  51. * @return {boolean}
  52. */
  53. static isTizen3() {
  54. return shaka.util.Platform.userAgentContains_('Tizen 3');
  55. }
  56. /**
  57. * Check if the current platform is a Google Chromecast.
  58. *
  59. * @return {boolean}
  60. */
  61. static isChromecast() {
  62. return shaka.util.Platform.userAgentContains_('CrKey');
  63. }
  64. /**
  65. * Check if the current platform is Google Chrome.
  66. *
  67. * @return {boolean}
  68. */
  69. static isChrome() {
  70. // The Edge user agent will also contain the "Chrome" keyword, so we need
  71. // to make sure this is not Edge.
  72. return shaka.util.Platform.userAgentContains_('Chrome') &&
  73. !shaka.util.Platform.isEdge();
  74. }
  75. /**
  76. * Check if the user agent contains a key. This is the best way we know of
  77. * right now to detect platforms. If there is a better way, please send a
  78. * PR.
  79. *
  80. * @param {string} key
  81. * @return {boolean}
  82. * @private
  83. */
  84. static userAgentContains_(key) {
  85. const userAgent = navigator.userAgent || '';
  86. return userAgent.includes(key);
  87. }
  88. };