home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / navigator / nsBrowserContentListener.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  170 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Alec Flett <alecf@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const mediatorContractId = "@mozilla.org/appshell/window-mediator;1";
  40. const nsIWebBrowserChrome = Components.interfaces.nsIWebBrowserChrome;
  41.  
  42. function nsBrowserContentListener(toplevelWindow, contentWindow)
  43. {
  44.     // this one is not as easy as you would hope.
  45.     // need to convert toplevelWindow to an XPConnected object, instead
  46.     // of a DOM-based object, to be able to QI() it to nsIXULWindow
  47.     
  48.     this.init(toplevelWindow, contentWindow);
  49. }
  50.  
  51. /* implements nsIURIContentListener */
  52.  
  53. nsBrowserContentListener.prototype =
  54. {
  55.     init: function(toplevelWindow, contentWindow)
  56.     {
  57.         this.toplevelWindow = toplevelWindow;
  58.         this.contentWindow = contentWindow;
  59.  
  60.         // hook up the whole parent chain thing
  61.         var windowDocShell = this.convertWindowToDocShell(toplevelWindow);
  62.         if (windowDocShell)
  63.             windowDocshell.parentURIContentListener = this;
  64.     
  65.         var registerWindow = false;
  66.         try {          
  67.           var treeItem = contentWindow.docShell.QueryInterface(Components.interfaces.nsIDocShellTreeItem);
  68.           var treeOwner = treeItem.treeOwner;
  69.           var interfaceRequestor = treeOwner.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  70.           var webBrowserChrome = interfaceRequestor.getInterface(nsIWebBrowserChrome);
  71.           if (webBrowserChrome)
  72.           {
  73.             var chromeFlags = webBrowserChrome.chromeFlags;
  74.             var res = chromeFlags & nsIWebBrowserChrome.CHROME_ALL;
  75.             var res2 = chromeFlags & nsIWebBrowserChrome.CHROME_DEFAULT;
  76.             if ( res == nsIWebBrowserChrome.CHROME_ALL || res2 == nsIWebBrowserChrome.CHROME_DEFAULT)
  77.             {             
  78.               registerWindow = true;
  79.             }
  80.          }
  81.        } catch (ex) {} 
  82.  
  83.         // register ourselves
  84.        if (registerWindow)
  85.        {
  86.         var uriLoader = Components.classes["@mozilla.org/uriloader;1"].getService(Components.interfaces.nsIURILoader);
  87.         uriLoader.registerContentListener(this);
  88.        }
  89.     },
  90.     close: function()
  91.     {
  92.         this.contentWindow = null;
  93.         var uriLoader = Components.classes["@mozilla.org/uriloader;1"].getService(Components.interfaces.nsIURILoader);
  94.  
  95.         uriLoader.unRegisterContentListener(this);
  96.     },
  97.     QueryInterface: function(iid)
  98.     {
  99.         if (iid.equals(Components.interfaces.nsIURIContentListener) ||
  100.             iid.equals(Components.interfaces.nsISupportsWeakReference) ||
  101.             iid.equals(Components.interfaces.nsISupports))
  102.             return this;
  103.         throw Components.results.NS_NOINTERFACE;
  104.  
  105.     },
  106.     onStartURIOpen: function(uri)
  107.     {
  108.         // ignore and don't abort
  109.         return false;
  110.     },
  111.  
  112.     doContent: function(contentType, isContentPreferred, request, contentHandler)
  113.     {
  114.         // forward the doContent to our content area webshell
  115.         var docShell = this.contentWindow.docShell;
  116.         var contentListener;
  117.         try {
  118.             contentListener =
  119.                 docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  120.                 .getInterface(Components.interfaces.nsIURIContentListener);
  121.         } catch (ex) {
  122.             dump(ex);
  123.         }
  124.         
  125.         if (!contentListener) return false;
  126.         
  127.         return contentListener.doContent(contentType, isContentPreferred, request, contentHandler);
  128.         
  129.     },
  130.  
  131.     isPreferred: function(contentType, desiredContentType)
  132.     {
  133.         // seems like we should be getting this from helper apps or something
  134.         switch(contentType) {
  135.             case "text/html":
  136.             case "text/xul":
  137.             case "text/rdf":
  138.             case "text/xml":
  139.             case "text/css":
  140.             case "image/gif":
  141.             case "image/jpeg":
  142.             case "image/png":
  143.             case "text/plain":
  144.             case "application/http-index-format":
  145.                 return true;
  146.         }
  147.         return false;
  148.     },
  149.     canHandleContent: function(contentType, isContentPreferred, desiredContentType)
  150.     {
  151.         var docShell = this.contentWindow.docShell;
  152.         var contentListener;
  153.         try {
  154.             contentListener =
  155.                 docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIURIContentListener);
  156.         } catch (ex) {
  157.             dump(ex);
  158.         }
  159.         if (!contentListener) return false;
  160.         
  161.         return contentListener.canHandleContent(contentType, isContentPreferred, desiredContentType);
  162.     },
  163.     convertWindowToDocShell: function(win) {
  164.         // don't know how to do this
  165.         return null;
  166.     },
  167.     loadCookie: null,
  168.     parentContentListener: null
  169. }
  170.