1 /* See license.txt for terms of usage */ 2 3 define([ 4 "firebug/lib/trace", 5 "firebug/lib/http", 6 "firebug/chrome/firefox" 7 ], 8 function(FBTrace, Http, Firefox) { 9 10 // ********************************************************************************************* // 11 // Constants 12 13 var Ci = Components.interfaces; 14 var Cc = Components.classes; 15 var Cu = Components.utils; 16 17 var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); 18 19 var Win = {}; 20 21 var window = {}; // these declarations exist to cause errors if we accidently 22 var document = {}; // reference these globals 23 24 // ********************************************************************************************* // 25 // Crossbrowser API 26 27 Win.getWindowProxyIdForWindow = function(win) 28 { 29 if (!win) 30 return null; 31 32 var id = Win.getWindowId(win).outerWindowID; 33 34 // xxxJJB, xxxHonza: the id is often null, what could be the problem? 35 // jjb: My guess: just another Mozilla bug 36 if (!id) 37 return Win.getTabIdForWindow(win); 38 39 return id; 40 }; 41 42 Win.getTabForWindow = function(aWindow) 43 { 44 aWindow = Win.getRootWindow(aWindow); 45 46 var tabBrowser = Firefox.getTabBrowser(); 47 if (!aWindow || !tabBrowser || !tabBrowser.getBrowserIndexForDocument) 48 { 49 if (FBTrace.DBG_WINDOWS) 50 FBTrace.sysout("getTabForWindow FAIL aWindow: "+aWindow+" tabBrowser: "+tabBrowser, tabBrowser); 51 return null; 52 } 53 54 try 55 { 56 var targetDoc = aWindow.document; 57 58 var tab = null; 59 var targetBrowserIndex = tabBrowser.getBrowserIndexForDocument(targetDoc); 60 if (targetBrowserIndex != -1) 61 { 62 tab = tabBrowser.tabContainer.childNodes[targetBrowserIndex]; 63 return tab; 64 } 65 } 66 catch (ex) 67 { 68 } 69 70 return null; 71 }; 72 73 Win.getTabIdForWindow = function(win) 74 { 75 var tab = Win.getTabForWindow(win); 76 return tab ? tab.linkedPanel : null; 77 }; 78 79 // ********************************************************************************************* // 80 // Window iteration 81 82 Win.iterateWindows = function(win, handler) 83 { 84 if (!win || !win.document) 85 return; 86 87 handler(win); 88 89 if (win == top || !win.frames) 90 return; // XXXjjb hack for chromeBug 91 92 for (var i = 0; i < win.frames.length; ++i) 93 { 94 var subWin = win.frames[i]; 95 if (subWin != win) 96 Win.iterateWindows(subWin, handler); 97 } 98 }; 99 100 Win.getRootWindow = function(win) 101 { 102 for (; win; win = win.parent) 103 { 104 if (!win.parent || win == win.parent) 105 return win; 106 107 // When checking the 'win.parent' type we need to use the target 108 // type from the same scope. i.e. from win.parent 109 // Iframes from different domains can use different Window type than 110 // the top level window. 111 if (!(win.parent instanceof win.parent.Window)) 112 return win; 113 } 114 115 return null; 116 }; 117 118 // ********************************************************************************************* // 119 // Firefox browsing 120 121 Win.openNewTab = function(url, postText) 122 { 123 if (!url) 124 return; 125 126 var postData = null; 127 if (postText) 128 { 129 var stringStream = Http.getInputStreamFromString(postText); 130 postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream); 131 postData.addHeader("Content-Type", "application/x-www-form-urlencoded"); 132 postData.addContentLength = true; 133 postData.setData(stringStream); 134 } 135 136 var tabBrowser = Firefox.getTabBrowser(); 137 return tabBrowser.selectedTab = tabBrowser.addTab(url, null, null, postData); 138 }; 139 140 // Iterate over all opened firefox windows of the given type. If the callback returns true 141 // the iteration is stopped. 142 Win.iterateBrowserWindows = function(windowType, callback) 143 { 144 var windowList = wm.getZOrderDOMWindowEnumerator(windowType, true); 145 if (!windowList.hasMoreElements()) 146 windowList = wm.getEnumerator(windowType); 147 148 while (windowList.hasMoreElements()) 149 { 150 if (callback(windowList.getNext())) 151 return true; 152 } 153 154 return false; 155 }; 156 157 Win.iterateBrowserTabs = function(browserWindow, callback) 158 { 159 var tabBrowser = browserWindow.getBrowser(); 160 var numTabs = tabBrowser.browsers.length; 161 162 for(var index=0; index<numTabs; index++) 163 { 164 var currentBrowser = tabBrowser.getBrowserAtIndex(index); 165 if (callback(tabBrowser.mTabs[index], currentBrowser)) 166 return true; 167 } 168 169 return false; 170 } 171 172 173 Win.getBrowserByWindow = function(win) 174 { 175 var browsers = Firefox.getBrowsers(); 176 for (var i = 0; i < browsers.length; ++i) 177 { 178 var browser = browsers[i]; 179 if (browser.contentWindow === win) 180 return browser; 181 } 182 183 return null; 184 } 185 186 // ********************************************************************************************* // 187 188 Win.getWindowId = function(win) 189 { 190 var util = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); 191 var innerWindowID = "(none)"; 192 193 try 194 { 195 var outerWindowID = util.outerWindowID; 196 innerWindowID = util.currentInnerWindowID; 197 } 198 catch(exc) 199 { 200 // no - op 201 } 202 203 return { 204 outer: outerWindowID, 205 inner: innerWindowID, 206 toString: function() { 207 return this.outer+"."+this.inner; 208 } 209 }; 210 }; 211 212 Win.safeGetWindowLocation = function(window) 213 { 214 try 215 { 216 if (window) 217 { 218 if (window.closed) 219 return "(window.closed)"; 220 if ("location" in window) 221 return window.location+""; 222 else 223 return "(no window.location)"; 224 } 225 else 226 return "(no context.window)"; 227 } 228 catch (exc) 229 { 230 if (FBTrace.DBG_WINDOWS || FBTrace.DBG_ERRORS) 231 { 232 FBTrace.sysout("TabContext.getWindowLocation failed "+exc, exc); 233 FBTrace.sysout("TabContext.getWindowLocation failed window:", window); 234 } 235 236 return "(getWindowLocation: "+exc+")"; 237 } 238 }; 239 240 // ********************************************************************************************* // 241 242 return Win; 243 244 // ********************************************************************************************* // 245 }); 246