home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / navigator / nsBrowserStatusHandler.js < prev    next >
Encoding:
Text File  |  2001-08-01  |  10.4 KB  |  322 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Blake Ross <blakeross@telocity.com>
  22.  *   Peter Annema <disttsc@bart.nl>
  23.  */
  24.  
  25. const NS_ERROR_MODULE_NETWORK = 2152398848;
  26. const NS_NET_STATUS_READ_FROM = NS_ERROR_MODULE_NETWORK + 8;
  27. const NS_NET_STATUS_WROTE_TO  = NS_ERROR_MODULE_NETWORK + 9;
  28.  
  29.  
  30. function nsBrowserStatusHandler()
  31. {
  32.   this.init();
  33. }
  34.  
  35. nsBrowserStatusHandler.prototype =
  36. {
  37.   useRealProgressFlag : false,
  38.   totalRequests : 0,
  39.   finishedRequests : 0,
  40.  
  41.   // Stored Status, Link and Loading values
  42.   status : "",
  43.   defaultStatus : "",
  44.   jsStatus : "",
  45.   jsDefaultStatus : "",
  46.   overLink : "",
  47.   startTime : 0,
  48.  
  49.   statusTimeoutInEffect : false,
  50.  
  51.   hideAboutBlank : true,
  52.  
  53.   QueryInterface : function(aIID)
  54.   {
  55.     if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  56.         aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  57.         aIID.equals(Components.interfaces.nsIXULBrowserWindow) ||
  58.         aIID.equals(Components.interfaces.nsISupports))
  59.       return this;
  60.     throw Components.results.NS_NOINTERFACE;
  61.   },
  62.  
  63.   init : function()
  64.   {
  65.     // XXXjag is this still needed? It's currently just ""
  66.     this.defaultStatus = gNavigatorBundle.getString("defaultStatus");
  67.  
  68.     this.urlBar          = document.getElementById("urlbar");
  69.     this.throbberElement = document.getElementById("navigator-throbber");
  70.     this.statusMeter     = document.getElementById("statusbar-icon");
  71.     this.stopButton      = document.getElementById("stop-button");
  72.     this.stopMenu        = document.getElementById("menuitem-stop");
  73.     this.stopContext     = document.getElementById("context-stop");
  74.     this.statusTextField = document.getElementById("statusbar-display");
  75.  
  76.   },
  77.  
  78.   destroy : function()
  79.   {
  80.     // XXXjag to avoid leaks :-/, see bug 60729
  81.     this.urlBar          = null;
  82.     this.throbberElement = null;
  83.     this.statusMeter     = null;
  84.     this.stopButton      = null;
  85.     this.stopMenu        = null;
  86.     this.stopContext     = null;
  87.     this.statusTextField = null;
  88.   },
  89.  
  90.   setJSStatus : function(status)
  91.   {
  92.     this.jsStatus = status;
  93.     this.updateStatusField();
  94.     // set empty so defaults show up next change
  95.     this.jsStatus = "";
  96.   },
  97.  
  98.   setJSDefaultStatus : function(status)
  99.   {
  100.     this.jsDefaultStatus = status;
  101.     this.updateStatusField();
  102.   },
  103.  
  104.   setDefaultStatus : function(status)
  105.   {
  106.     this.defaultStatus = status;
  107.     this.updateStatusField();
  108.   },
  109.  
  110.   setOverLink : function(link, b)
  111.   {
  112.     this.overLink = link;
  113.     this.updateStatusField();
  114.     // set empty so defaults show up next change
  115.     this.overLink = "";
  116.   },
  117.  
  118.   updateStatusField : function()
  119.   {
  120.     var text = this.overLink || this.status || this.jsStatus || this.jsDefaultStatus || this.defaultStatus;
  121.  
  122.     // check the current value so we don't trigger an attribute change
  123.     // and cause needless (slow!) UI updates
  124.     if (this.statusTextField.label != text) {
  125.       this.statusTextField.label = text;
  126.     }
  127.   },
  128.  
  129.   onProgressChange : function (aWebProgress, aRequest,
  130.                                aCurSelfProgress, aMaxSelfProgress,
  131.                                aCurTotalProgress, aMaxTotalProgress)
  132.   {
  133.     if (!this.useRealProgressFlag && aRequest)
  134.       return;
  135.  
  136.     if (aMaxTotalProgress > 0) {
  137.       this.statusMeter.mode = "normal";
  138.  
  139.       // This is highly optimized.  Don't touch this code unless
  140.       // you are intimately familiar with the cost of setting
  141.       // attrs on XUL elements. -- hyatt
  142.       var percentage = (aCurTotalProgress * 100) / aMaxTotalProgress;
  143.       this.statusMeter.value = percentage;
  144.     } else {
  145.       this.statusMeter.mode = "undetermined";
  146.     }
  147.   },
  148.  
  149.   onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
  150.   {
  151.     if (!aRequest)
  152.       return;
  153.       
  154.     //ignore local/resource:/chrome: files
  155.     if (aStatus == NS_NET_STATUS_READ_FROM || aStatus == NS_NET_STATUS_WROTE_TO)
  156.       return;
  157.  
  158.     const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
  159.     const nsIChannel = Components.interfaces.nsIChannel;
  160.     var domWindow;
  161.     if (aStateFlags & nsIWebProgressListener.STATE_START) {
  162.       if (aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK) {
  163.         // Remember when loading commenced.
  164.         this.startTime = (new Date()).getTime();
  165.  
  166.         domWindow = aWebProgress.DOMWindow;
  167.         if (domWindow == _content)
  168.           this.startDocumentLoad(aRequest);
  169.  
  170.         // Turn progress meter on.
  171.         this.statusMeter.mode = "undetermined";
  172.         this.throbberElement.setAttribute("busy", true);
  173.  
  174.         // XXX: These need to be based on window activity...
  175.         this.stopButton.disabled = false;
  176.         this.stopMenu.removeAttribute('disabled');
  177.         this.stopContext.removeAttribute('disabled');
  178.  
  179.         // Initialize the progress stuff...
  180.         this.useRealProgressFlag = false;
  181.         this.totalRequests = 0;
  182.         this.finishedRequests = 0;
  183.       }
  184.  
  185.       if (aStateFlags & nsIWebProgressListener.STATE_IS_REQUEST) {
  186.         this.totalRequests += 1;
  187.       }
  188.     }
  189.     else if (aStateFlags & nsIWebProgressListener.STATE_STOP) {
  190.       if (aStateFlags & nsIWebProgressListener.STATE_IS_REQUEST) {
  191.         this.finishedRequests += 1;
  192.         if (!this.useRealProgressFlag)
  193.           this.onProgressChange(null, null, 0, 0, this.finishedRequests, this.totalRequests);
  194.       }
  195.       if (aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK) {
  196.         domWindow = aWebProgress.DOMWindow;
  197.         if (domWindow == domWindow.top)
  198.           this.endDocumentLoad(aRequest, aStatus);
  199.  
  200.         var location = aRequest.QueryInterface(nsIChannel).URI.spec;
  201.         var msg = "";
  202.         if (location != "about:blank") {
  203.           // Record page loading time.
  204.           var elapsed = ((new Date()).getTime() - this.startTime) / 1000;
  205.           msg = gNavigatorBundle.getString("nv_done");
  206.           msg = msg.replace(/%elapsed%/, elapsed);
  207.         }
  208.         this.status = "";
  209.         this.setDefaultStatus(msg);
  210.  
  211.         // Turn progress meter off.
  212.         this.statusMeter.mode = "normal";
  213.         this.statusMeter.value = 0;  // be sure to clear the progress bar
  214.         this.throbberElement.removeAttribute("busy");
  215.  
  216.         // XXX: These need to be based on window activity...
  217.         // XXXjag: <command id="cmd_stop"/> ?
  218.         this.stopButton.disabled = true;
  219.         this.stopMenu.setAttribute('disabled', 'true');
  220.         this.stopContext.setAttribute('disabled', 'true');
  221.  
  222.       }
  223.     }
  224.     else if (aStateFlags & nsIWebProgressListener.STATE_TRANSFERRING) {
  225.       if (aStateFlags & nsIWebProgressListener.STATE_IS_DOCUMENT) {
  226.         var ctype = aRequest.QueryInterface(nsIChannel).contentType;
  227.  
  228.         if (ctype != "text/html")
  229.           this.useRealProgressFlag = true;
  230.  
  231.         this.statusMeter.mode = "normal";
  232.       }
  233.  
  234.       if (aStateFlags & nsIWebProgressListener.STATE_IS_REQUEST) {
  235.         if (!this.useRealProgressFlag)
  236.           this.onProgressChange(null, null, 0, 0, this.finishedRequests, this.totalRequests);
  237.       }
  238.     }
  239.   },
  240.  
  241.   onLocationChange : function(aWebProgress, aRequest, aLocation)
  242.   {
  243.     var location = aLocation.spec;
  244.     domWindow = aWebProgress.DOMWindow;
  245.  
  246.     if (this.hideAboutBlank) {
  247.       this.hideAboutBlank = false;
  248.       if (location == "about:blank")
  249.         location = "";
  250.     }
  251.  
  252.     // We should probably not do this if the value has changed since the user
  253.     // searched
  254.     // Update urlbar only if a new page was loaded on the primary content area
  255.     // Do not update urlbar if there was a subframe navigation
  256.     if (domWindow == domWindow.top) {
  257.       this.urlBar.value = location;
  258.       SetPageProxyState("valid");
  259.     }
  260.     UpdateBackForwardButtons();
  261.   },
  262.  
  263.   onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage)
  264.   {
  265.     //ignore local/resource:/chrome: files
  266.     if (aStatus == NS_NET_STATUS_READ_FROM || aStatus == NS_NET_STATUS_WROTE_TO)
  267.       return;
  268.  
  269.     if (!this.statusTimeoutInEffect) {
  270.       this.statusTimeoutInEffect = true;
  271.       this.status = aMessage;
  272.       this.updateStatusField();
  273.       setTimeout(function(aClosure) { aClosure.statusTimeoutInEffect = false; }, 400, this);
  274.     }
  275.   },
  276.  
  277.   onSecurityChange : function(aWebProgress, aRequest, aState)
  278.   {
  279.   },
  280.  
  281.   startDocumentLoad : function(aRequest)
  282.   {
  283.     const nsIChannel = Components.interfaces.nsIChannel;
  284.     var urlStr = aRequest.QueryInterface(nsIChannel).URI.spec;
  285.     var observerService = Components.classes["@mozilla.org/observer-service;1"]
  286.                                     .getService(Components.interfaces.nsIObserverService);
  287.     try {
  288.       observerService.Notify(_content, "StartDocumentLoad", urlStr);
  289.     } catch (e) {
  290.     }
  291.   },
  292.  
  293.   endDocumentLoad : function(aRequest, aStatus)
  294.   {
  295.     const nsIChannel = Components.interfaces.nsIChannel;
  296.     var urlStr = aRequest.QueryInterface(nsIChannel).originalURI.spec;
  297.  
  298.     if (Components.isSuccessCode(aStatus))
  299.       dump("Document "+urlStr+" loaded successfully\n"); // per QA request
  300.     else {
  301.       // per QA request
  302.       var e = new Components.Exception("", aStatus);
  303.       var name = e.name;
  304.       dump("Error loading URL "+urlStr+" : "+
  305.            Number(aStatus).toString(16));
  306.       if (name)
  307.            dump(" ("+name+")");
  308.       dump('\n'); 
  309.     }
  310.  
  311.     var observerService = Components.classes["@mozilla.org/observer-service;1"]
  312.                                     .getService(Components.interfaces.nsIObserverService);
  313.  
  314.     var notification = Components.isSuccessCode(aStatus) ? "EndDocumentLoad" : "FailDocumentLoad";
  315.     try {
  316.       observerService.Notify(_content, notification, urlStr);
  317.     } catch (e) {
  318.     }
  319.   }
  320. }
  321.  
  322.