Guest User

unity_nacl.js

a guest
Mar 7th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright (c) 2011 The Native Client Authors. All rights reserved.
  3.  * Use of this source code is governed by a BSD-style license that can be
  4.  * found in the LICENSE file.
  5.  */
  6.  
  7. /**
  8.  * @fileoverview This file provides a BrowserChecker Javascript class.
  9.  * Users can create a BrowserChecker object, invoke checkBrowser(|version|),
  10.  * and then use getIsValidBrowser() and getBrowserSupportStatus()
  11.  * to determine if the browser version is greater than |version|
  12.  * and if the Native Client plugin is found.
  13.  */
  14.  
  15. // Create a namespace object
  16. var browser_version = browser_version || {};
  17.  
  18. /**
  19.  * Class to provide checking for version and NativeClient.
  20.  * @param {integer} arg1 An argument that indicates major version of Chrome we
  21.  *     require, such as 14.
  22.  */
  23.  
  24. /**
  25.  * Constructor for the BrowserChecker.  Sets the major version of
  26.  * Chrome that is required to |minChromeVersion|.
  27.  * @param minChromeVersion   The earliest major version of chrome that
  28.  *     is supported.  If the Chrome browser version is less than
  29.  *     |minChromeVersion| then |isValidBrowswer| will be set to false.
  30.  * @param opt_maxChromeVersion   Ignored.  Retained for backwards compatibility.
  31.  * @param appVersion  The application version string.
  32.  * @param plugins     The plugins that exist in the browser.
  33.  * @constructor
  34.  */
  35. browser_version.BrowserChecker = function(minChromeVersion,
  36.                                           appVersion, plugins,
  37.                                           opt_maxChromeVersion) {
  38.   /**
  39.    * Version specified by the user. This class looks to see if the browser
  40.    * version is >= |minChromeVersion_|.
  41.    * @type {integer}
  42.    * @private
  43.    */
  44.   this.minChromeVersion_ = minChromeVersion;
  45.  
  46.   /**
  47.    * List of Browser plugin objects.
  48.    * @type {Ojbect array}
  49.    * @private
  50.    */
  51.   this.plugins_ = plugins;
  52.  
  53.   /**
  54.    * Application version string from the Browser.
  55.    * @type {integer}
  56.    * @private
  57.    */
  58.   this.appVersion_ = appVersion;
  59.  
  60.   /**
  61.    * Flag used to indicate if the browser has Native Client and is if the
  62.    * browser version is recent enough.
  63.    * @type {boolean}
  64.    * @private
  65.    */
  66.   this.isValidBrowser_ = false;
  67.  
  68.   /**
  69.    * Actual major version of Chrome -- found by querying the browser.
  70.    * @type {integer}
  71.    * @private
  72.    */
  73.   this.chromeVersion_ = null;
  74.  
  75.   /**
  76.    * Browser support status. This allows the user to get a detailed status
  77.    * rather than using this.browserSupportMessage.
  78.    */
  79.   this.browserSupportStatus_ =
  80.       browser_version.BrowserChecker.StatusValues.UNKNOWN;
  81. }
  82.  
  83. /**
  84.  * The values used for BrowserChecker status to indicate success or
  85.  * a specific error.
  86.  * @enum {id}
  87.  */
  88. browser_version.BrowserChecker.StatusValues = {
  89.   UNKNOWN: 0,
  90.   NACL_ENABLED: 1,
  91.   UNKNOWN_BROWSER: 2,
  92.   CHROME_VERSION_TOO_OLD: 3,
  93.   NACL_NOT_ENABLED: 4,
  94.   NOT_USING_SERVER: 5
  95. };
  96.  
  97. /**
  98.  * Determines if the plugin with name |name| exists in the browser.
  99.  * @param {string} name The name of the plugin.
  100.  * @param {Object array} plugins The plugins in this browser.
  101.  * @return {bool} |true| if the plugin is found.
  102.  */
  103. browser_version.BrowserChecker.prototype.pluginExists = function(name,
  104.                                                                  plugins) {
  105.   for (var index=0; index < plugins.length; index++) {
  106.     var plugin = this.plugins_[index];
  107.     var plugin_name = plugin['name'];
  108.     // If the plugin is not found, you can use the Javascript console
  109.     // to see the names of the plugins that were found when debugging.
  110.     if (plugin_name.indexOf(name) != -1) {
  111.       return true;
  112.     }
  113.   }
  114.   return false;
  115. }
  116.  
  117. /**
  118.  * Returns browserSupportStatus_ which indicates if the browser supports
  119.  * Native Client.  Values are defined as literals in
  120.  * browser_version.BrowserChecker.StatusValues.
  121.  * @ return {int} Level of NaCl support.
  122.  */
  123. browser_version.BrowserChecker.prototype.getBrowserSupportStatus = function() {
  124.   return this.browserSupportStatus_;
  125. }
  126.  
  127. /**
  128.  * Returns isValidBrowser (true/false) to indicate if the browser supports
  129.  * Native Client.
  130.  * @ return {bool} If this browser has NativeClient and correct version.
  131.  */
  132. browser_version.BrowserChecker.prototype.getIsValidBrowser = function() {
  133.   return this.isValidBrowser_;
  134. }
  135.  
  136. /**
  137.  * Checks to see if this browser can support Native Client applications.
  138.  * For Chrome browsers, checks to see if the "Native Client" plugin is
  139.  * enabled.
  140.  */
  141. browser_version.BrowserChecker.prototype.checkBrowser = function() {
  142.   var versionPatt = /Chrome\/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  143.   var result = this.appVersion_.match(versionPatt);
  144.  
  145.   // |result| stores the Chrome version number.
  146.   if (!result) {
  147.     this.isValidBrowser_ = false;
  148.     this.browserSupportStatus_ =
  149.         browser_version.BrowserChecker.StatusValues.UNKNOWN_BROWSER;
  150.   } else {
  151.     this.chromeVersion_ = result[1];
  152.     // We know we have Chrome, check version and/or plugin named Native Client
  153.     if (this.chromeVersion_ >= this.minChromeVersion_) {
  154.       var found_nacl = this.pluginExists('Native Client', this.plugins_);
  155.       if (found_nacl) {
  156.         this.isValidBrowser_ = true;
  157.         this.browserSupportStatus_ =
  158.             browser_version.BrowserChecker.StatusValues.NACL_ENABLED;
  159.       } else {
  160.         this.isValidBrowser_ = false;
  161.         this.browserSupportStatus_ =
  162.             browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED;
  163.       }
  164.     } else {
  165.       // We are in a version that is less than |minChromeVersion_|
  166.       this.isValidBrowser_ = false;
  167.       this.browserSupportStatus_ =
  168.           browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD;
  169.     }
  170.   }
  171.   var my_protocol = window.location.protocol;
  172.   if (my_protocol.indexOf('file') == 0) {
  173.     this.isValidBrowser_ = false;
  174.     this.browserSupportStatus_ =
  175.         browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER;
  176.   }
  177. }
  178.  
  179. // Check for Native Client support in the browser before the DOM loads.
  180. var isValidBrowser = false;
  181. var browserSupportStatus = 0;
  182. var checker = new browser_version.BrowserChecker(
  183.     15,  // Minumum Chrome version.
  184.     navigator["appVersion"],
  185.     navigator["plugins"]);
  186. checker.checkBrowser();
  187.  
  188. loadProgressModule = null;
  189. logo = null;
  190. progress = null;
  191. progressFrame = null;
  192. statusField = null;
  193. errorMessage = null;
  194. centerX = 0;
  195. centerY = 0;
  196. isValidBrowser = checker.getIsValidBrowser();
  197. browserSupportStatus = checker.getBrowserSupportStatus();
  198.  
  199. var progressURLs = [];
  200. var progressBytes = [];
  201. var totalDownloadSize = 0;
  202.  
  203. function moduleLoadProgress(event) {       
  204.     if (progressURLs.indexOf(event.url) == -1)
  205.     {
  206.         progressURLs.push(event.url);
  207.         progressBytes.push(event.loaded);
  208.         totalDownloadSize += event.total;
  209.     }
  210.    
  211.     progressBytes[progressURLs.indexOf(event.url)] = event.loaded;
  212.     var loaded = 0;
  213.     for (var i = 0; i < progressBytes.length; i++)
  214.         loaded += progressBytes[i];
  215.    
  216.     var progressImg = new Image();
  217.     progressImg.src = progress.src;
  218.     var size = totalDownloadSize;
  219.     if (size < 10 * 1024 * 1024)
  220.         size = 10 * 1024 * 1024;
  221.     progress.width = progressImg.width * (loaded / size) * 0.5;
  222. }
  223.  
  224. function moduleMessage (message) {
  225.     if (message.data.indexOf("Unity.SetProgress(") == 0)
  226.     {
  227.         var p = parseFloat(message.data.substring(18));
  228.         var progressImg = new Image();
  229.         progressImg.src = progress.src;
  230.         progress.width = progressImg.width * (p * 0.5 + 0.5);
  231.     }
  232.     else if (message.data.indexOf("Unity.FinishedLoading(") == 0)
  233.     {
  234.         progress.style.display = "none";
  235.         progressFrame.style.display = "none";
  236.         logo.style.display = "none";
  237.     }
  238.     else
  239.         eval (message.data);
  240. }
  241.  
  242. function moduleLoadError() {
  243.     moduleDidEndLoad();
  244. }
  245.  
  246. function moduleDidLoad() {
  247.     loadProgressModule = document.getElementById('Unity');
  248.     if (loadProgressModule == null)
  249.         return;
  250.  
  251.     var element = loadProgressModule;
  252.     centerX = 0;
  253.     while( element != null ) {
  254.         centerX += element.offsetLeft;
  255.         element = element.offsetParent;
  256.     }
  257.     centerX += loadProgressModule.clientWidth * 0.5;
  258.     element = loadProgressModule;
  259.     centerY = 0;
  260.     while( element != null ) {
  261.         centerY += element.offsetTop;
  262.         element = element.offsetParent;
  263.     }
  264.     centerY += loadProgressModule.clientHeight * 0.5;
  265.    
  266.     logo = document.getElementById('Logo');
  267.     logo.style.position = "absolute";
  268.     logo.style.pixelTop = centerY - logo.height - 10;
  269.     logo.style.pixelLeft = centerX - logo.width * 0.5;
  270.    
  271.     progress = document.getElementById('Progress');
  272.     var progressImg = new Image();
  273.     progressImg.src = progress.src;
  274.     progress.height = progressImg.height;
  275.     progress.style.position = "absolute";
  276.     progress.style.pixelTop = centerY;
  277.     progress.style.pixelLeft = centerX - progressImg.width * 0.5;
  278.  
  279.     progressFrame = document.getElementById('ProgressFrame');
  280.     progressFrame.width = progressImg.width;
  281.     progressFrame.height = progressImg.height;
  282.     progressFrame.style.position = "absolute";
  283.     progressFrame.style.pixelTop = progress.style.pixelTop;
  284.     progressFrame.style.pixelLeft = progress.style.pixelLeft;
  285.    
  286.     statusField = document.getElementById('Status');
  287.     statusField.style.position = "absolute";
  288.     statusField.style.fontFamily="verdana";
  289.     statusField.style.fontSize="12px";
  290.     statusField.style.pixelTop = progress.style.pixelTop;
  291.    
  292.     if (errorMessage != null)
  293.         setError (errorMessage);
  294. }
  295.  
  296. function moduleDidEndLoad() {
  297.     var lastError = event.target.lastError;
  298.     if (lastError != undefined && lastError.length != 0)
  299.         setError (lastError);
  300. }
  301.  
  302. function setError(error) {
  303.     errorMessage = error;
  304.     if (loadProgressModule == null)
  305.         moduleDidLoad ();
  306.     if (loadProgressModule == null)
  307.         return;
  308.     statusField.innerHTML = error;
  309.     statusField.style.pixelLeft = centerX - statusField.clientWidth * 0.5;
  310.     progress.style.display = "none";
  311.     progressFrame.style.display = "none";
  312. }
  313.  
  314. window.onresize = moduleDidLoad;
Advertisement
Add Comment
Please, Sign In to add comment