flash.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Flash detection.
  16. * @see ../demos/useragent.html
  17. */
  18. goog.provide('goog.userAgent.flash');
  19. goog.require('goog.string');
  20. /**
  21. * @define {boolean} Whether we know at compile-time that the browser doesn't
  22. * have flash.
  23. */
  24. goog.define('goog.userAgent.flash.ASSUME_NO_FLASH', false);
  25. /**
  26. * Whether we can detect that the browser has flash
  27. * @type {boolean}
  28. * @private
  29. */
  30. goog.userAgent.flash.detectedFlash_ = false;
  31. /**
  32. * Full version information of flash installed, in form 7.0.61
  33. * @type {string}
  34. * @private
  35. */
  36. goog.userAgent.flash.detectedFlashVersion_ = '';
  37. /**
  38. * Initializer for goog.userAgent.flash
  39. *
  40. * This is a named function so that it can be stripped via the jscompiler if
  41. * goog.userAgent.flash.ASSUME_NO_FLASH is true.
  42. * @private
  43. */
  44. goog.userAgent.flash.init_ = function() {
  45. if (navigator.plugins && navigator.plugins.length) {
  46. var plugin = navigator.plugins['Shockwave Flash'];
  47. if (plugin) {
  48. goog.userAgent.flash.detectedFlash_ = true;
  49. if (plugin.description) {
  50. goog.userAgent.flash.detectedFlashVersion_ =
  51. goog.userAgent.flash.getVersion_(plugin.description);
  52. return;
  53. }
  54. }
  55. if (navigator.plugins['Shockwave Flash 2.0']) {
  56. goog.userAgent.flash.detectedFlash_ = true;
  57. goog.userAgent.flash.detectedFlashVersion_ = '2.0.0.11';
  58. return;
  59. }
  60. }
  61. if (navigator.mimeTypes && navigator.mimeTypes.length) {
  62. var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];
  63. goog.userAgent.flash.detectedFlash_ =
  64. !!(mimeType && mimeType.enabledPlugin);
  65. if (goog.userAgent.flash.detectedFlash_) {
  66. goog.userAgent.flash.detectedFlashVersion_ =
  67. goog.userAgent.flash.getVersion_(mimeType.enabledPlugin.description);
  68. return;
  69. }
  70. }
  71. try {
  72. // Try 7 first, since we know we can use GetVariable with it
  73. var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
  74. goog.userAgent.flash.detectedFlash_ = true;
  75. goog.userAgent.flash.detectedFlashVersion_ =
  76. goog.userAgent.flash.getVersion_(ax.GetVariable('$version'));
  77. return;
  78. } catch (e) {
  79. /* Fall through */
  80. }
  81. // Try 6 next, some versions are known to crash with GetVariable calls
  82. try {
  83. var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
  84. goog.userAgent.flash.detectedFlash_ = true;
  85. // First public version of Flash 6
  86. goog.userAgent.flash.detectedFlashVersion_ = '6.0.21';
  87. return;
  88. } catch (e) {
  89. /* Fall through */
  90. }
  91. try {
  92. // Try the default activeX
  93. var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  94. goog.userAgent.flash.detectedFlash_ = true;
  95. goog.userAgent.flash.detectedFlashVersion_ =
  96. goog.userAgent.flash.getVersion_(ax.GetVariable('$version'));
  97. return;
  98. } catch (e) {
  99. // No flash
  100. }
  101. };
  102. /**
  103. * Derived from Apple's suggested sniffer.
  104. * @param {string} desc e.g. Shockwave Flash 7.0 r61.
  105. * @return {string} 7.0.61.
  106. * @private
  107. */
  108. goog.userAgent.flash.getVersion_ = function(desc) {
  109. var matches = desc.match(/[\d]+/g);
  110. if (!matches) {
  111. return '';
  112. }
  113. matches.length = 3; // To standardize IE vs FF
  114. return matches.join('.');
  115. };
  116. if (!goog.userAgent.flash.ASSUME_NO_FLASH) {
  117. goog.userAgent.flash.init_();
  118. }
  119. /**
  120. * Whether we can detect that the browser has flash
  121. * @type {boolean}
  122. */
  123. goog.userAgent.flash.HAS_FLASH = goog.userAgent.flash.detectedFlash_;
  124. /**
  125. * Full version information of flash installed, in form 7.0.61
  126. * @type {string}
  127. */
  128. goog.userAgent.flash.VERSION = goog.userAgent.flash.detectedFlashVersion_;
  129. /**
  130. * Whether the installed flash version is as new or newer than a given version.
  131. * @param {string} version The version to check.
  132. * @return {boolean} Whether the installed flash version is as new or newer
  133. * than a given version.
  134. */
  135. goog.userAgent.flash.isVersion = function(version) {
  136. return goog.string.compareVersions(goog.userAgent.flash.VERSION, version) >=
  137. 0;
  138. };