1 /* See license.txt for terms of usage */
  2 
  3 define([], function() {
  4 
  5 // ********************************************************************************************* //
  6 // Constants
  7 
  8 var Ci = Components.interfaces;
  9 var Cc = Components.classes;
 10 var Cu = Components.utils;
 11 var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
 12 
 13 // ********************************************************************************************* //
 14 // Browser.xul dependent code
 15 
 16 function getBrowserDocument()
 17 {
 18     // TODO: this function is called very frequently, worth optimizing
 19     try
 20     {
 21         var chrome = Firebug.chrome;
 22         return chrome.inDetachedScope ? chrome.originalBrowser.ownerDocument : top.document;
 23     }
 24     catch (e)
 25     {
 26         if (FBTrace.DBG_ERRORS)
 27             FBTrace.sysout("firefox.getBrowserDocument; EXCEPTION " + e, e);
 28     }
 29 }
 30 
 31 // ********************************************************************************************* //
 32 // Browser.xul independent code
 33 
 34 var Firefox =
 35 {
 36     getElementById: function(id)
 37     {
 38         return getBrowserDocument().getElementById(id);
 39     },
 40 
 41     $: function(id)
 42     {
 43         return this.getElementById(id);
 44     },
 45 
 46     getTabBrowser: function()
 47     {
 48         if (window.gBrowser)
 49             return window.gBrowser;
 50 
 51         var tabBrowser = Firefox.getElementById("content");
 52         if (tabBrowser)
 53             return tabBrowser;
 54 
 55         if (FBTrace.DBG_WINDOWS)
 56             FBTrace.sysout("Firefox.getTabBrowser no window.gBrowser in "+window.location);
 57     },
 58 
 59     getCurrentBrowser: function()
 60     {
 61         var tabBrowser = Firefox.getTabBrowser();
 62         return tabBrowser ? tabBrowser.selectedBrowser : undefined;
 63     },
 64 
 65     getBrowsers: function()
 66     {
 67         var tabBrowser = Firefox.getTabBrowser();
 68         return tabBrowser ? tabBrowser.browsers : undefined;
 69     },
 70 
 71     selectTabByWindow: function(win)
 72     {
 73         var tabBrowser = Firefox.getTabBrowser();
 74         if (tabBrowser)
 75         {
 76             var index = tabBrowser.getBrowserIndexForDocument(win.document);
 77             tabBrowser.selectTabAtIndex(index);
 78         }
 79     },
 80 
 81     getCurrentURI: function()
 82     {
 83         try
 84         {
 85             return Firefox.getTabBrowser().currentURI;
 86         }
 87         catch (exc)
 88         {
 89             return null;
 90         }
 91     },
 92 
 93     /**
 94      * Returns <browser> element for specified content window.
 95      * @param {Object} win - Content window
 96      */
 97     getBrowserForWindow: function(win)
 98     {
 99         var tabBrowser = Firefox.getTabBrowser();
100         if (tabBrowser && win.document)
101             return tabBrowser.getBrowserForDocument(win.document);
102     },
103 
104     openWindow: function(windowType, url, features, params)
105     {
106         var win = windowType ? wm.getMostRecentWindow(windowType) : null;
107         if (win)
108         {
109             if ("initWithParams" in win)
110                 win.initWithParams(params);
111             win.focus();
112         }
113         else
114         {
115             var winFeatures = "resizable,dialog=no,centerscreen" +
116                 (features != "" ? ("," + features) : "");
117             var parentWindow = (this.instantApply || !window.opener || window.opener.closed) ?
118                 window : window.opener;
119 
120             win = parentWindow.openDialog(url, "_blank", winFeatures, params);
121         }
122         return win;
123     },
124 
125     viewSource: function(url, lineNo)
126     {
127         window.openDialog("chrome://global/content/viewSource.xul", "_blank",
128             "all,dialog=no", url, null, null, lineNo);
129     },
130 };
131 
132 // ********************************************************************************************* //
133 
134 //XXXjoe This horrible hack works around a focus bug in Firefox which is caused when
135 //the HTML Validator extension and Firebug are installed.  It causes the keyboard to
136 //behave erratically when typing, and the only solution I've found is to delay
137 //the initialization of HTML Validator by overriding this function with a timeout.
138 //XXXrobc Do we still need this? Does this extension even exist anymore?
139 //xxxHonza: The extension still exists, but I think we should remove this hack.
140 try
141 {
142     if (top.hasOwnProperty('TidyBrowser'))
143     {
144         var prev = TidyBrowser.prototype.updateStatusBar;
145         TidyBrowser.prototype.updateStatusBar = function()
146         {
147             var self = this, args = arguments;
148             setTimeout(function()
149             {
150                 prev.apply(self, args);
151             });
152         }
153     }
154 }
155 catch (err)
156 {
157     if (FBTrace.DBG_ERRORS)
158         FBTrace.sysout("firefox; EXCEPTION HTML Validator collision!", err);
159 }
160 
161 // ********************************************************************************************* //
162 // Registration
163 
164 return Firefox;
165 
166 // ********************************************************************************************* //
167 });
168