| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
const EXPORTED_SYMBOLS = [ |
|---|
| 38 |
'getMostRecent', |
|---|
| 39 |
'getMostRecentByUrl', |
|---|
| 40 |
'getAllByType', |
|---|
| 41 |
'openExternal', |
|---|
| 42 |
'openUrl', |
|---|
| 43 |
'showNotice', |
|---|
| 44 |
]; |
|---|
| 45 |
|
|---|
| 46 |
const Cc = Components.classes; |
|---|
| 47 |
const Ci = Components.interfaces; |
|---|
| 48 |
const Cr = Components.results; |
|---|
| 49 |
const Cu = Components.utils; |
|---|
| 50 |
const Exception = Components.Exception; |
|---|
| 51 |
|
|---|
| 52 |
const mediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator); |
|---|
| 53 |
const ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService); |
|---|
| 54 |
const protoservice = Cc['@mozilla.org/uriloader/external-protocol-service;1'].getService(Ci.nsIExternalProtocolService); |
|---|
| 55 |
const logger = Cc['@downthemall.net/debug-service;1'].getService(Ci.dtaIDebugService); |
|---|
| 56 |
const windowwatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher); |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
function objToString(obj) { |
|---|
| 60 |
if (obj == null || obj == undefined) { |
|---|
| 61 |
return obj; |
|---|
| 62 |
} |
|---|
| 63 |
if ( |
|---|
| 64 |
typeof obj == 'string' |
|---|
| 65 |
|| obj instanceof String |
|---|
| 66 |
) { |
|---|
| 67 |
return obj.toString(); |
|---|
| 68 |
} |
|---|
| 69 |
if ( |
|---|
| 70 |
obj instanceof Ci.nsIURL |
|---|
| 71 |
|| obj instanceof Ci.nsIURI |
|---|
| 72 |
) { |
|---|
| 73 |
return obj.spec; |
|---|
| 74 |
} |
|---|
| 75 |
if (obj.url) { |
|---|
| 76 |
return objToString(obj.url); |
|---|
| 77 |
} |
|---|
| 78 |
throw new Exception("Not a valid type"); |
|---|
| 79 |
} |
|---|
| 80 |
function objToUri(obj) { |
|---|
| 81 |
if (obj == null || obj == undefined) { |
|---|
| 82 |
return null; |
|---|
| 83 |
} |
|---|
| 84 |
if (obj instanceof Ci.nsIURL || obj instanceof Ci.nsIURI) { |
|---|
| 85 |
return obj; |
|---|
| 86 |
} |
|---|
| 87 |
if (typeof obj == 'string' || obj instanceof String) { |
|---|
| 88 |
return ioservice.newURI(obj.toString(), null, null); |
|---|
| 89 |
} |
|---|
| 90 |
if (obj.url) { |
|---|
| 91 |
return objToUri(obj.url); |
|---|
| 92 |
} |
|---|
| 93 |
throw new Exception("Not a valid type"); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
function getMostRecent(type) { |
|---|
| 101 |
if (type instanceof Array) { |
|---|
| 102 |
for each (t in type) { |
|---|
| 103 |
let rv = getMostRecent(t); |
|---|
| 104 |
if (rv) { |
|---|
| 105 |
return rv; |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
return mediator.getMostRecentWindow(type.toString()); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
function getMostRecentByUrl(url) { |
|---|
| 116 |
if (!url) { |
|---|
| 117 |
return null; |
|---|
| 118 |
} |
|---|
| 119 |
url = objToString(url); |
|---|
| 120 |
|
|---|
| 121 |
let enumerator = mediator.getEnumerator(null); |
|---|
| 122 |
while (enumerator.hasMoreElements()) { |
|---|
| 123 |
var win = enumerator.getNext(); |
|---|
| 124 |
if (win.location == url) { |
|---|
| 125 |
return win; |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
return null; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
function getAllByType(type) { |
|---|
| 132 |
let rv = []; |
|---|
| 133 |
let enumerator = mediator.getEnumerator(type); |
|---|
| 134 |
while (enumerator.hasMoreElements()) { |
|---|
| 135 |
rv.push(enumerator.getNext()); |
|---|
| 136 |
} |
|---|
| 137 |
return rv; |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
function openExternal(link) { |
|---|
| 141 |
logger.logString("Mediator: Using external handler for " + link); |
|---|
| 142 |
protoservice.loadUrl(objToUri(link)); |
|---|
| 143 |
} |
|---|
| 144 |
function openUrl(window, link, ref) { |
|---|
| 145 |
logger.logString("Mediator: Request to open " + link); |
|---|
| 146 |
try { |
|---|
| 147 |
let win = getMostRecent('navigator:browser'); |
|---|
| 148 |
if (win) { |
|---|
| 149 |
|
|---|
| 150 |
if ('delayedOpenTab' in win) { |
|---|
| 151 |
win.delayedOpenTab(objToString(link), objToUri(ref)); |
|---|
| 152 |
return; |
|---|
| 153 |
} |
|---|
| 154 |
win.getBrowser().addTab(objToString(link), objToString(ref)); |
|---|
| 155 |
return; |
|---|
| 156 |
} |
|---|
| 157 |
win = getMostRecent('Songbird:Main'); |
|---|
| 158 |
if (win) { |
|---|
| 159 |
|
|---|
| 160 |
let tb = win.document.getElementById('content'); |
|---|
| 161 |
if (tb) { |
|---|
| 162 |
tb.loadOneTab(objToString(link), objToUri(ref), null, null, null); |
|---|
| 163 |
return; |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
catch (ex) { |
|---|
| 168 |
logger.log('Mediator: Failed to open tab', ex); |
|---|
| 169 |
} |
|---|
| 170 |
try { |
|---|
| 171 |
window.open(objToString(link)); |
|---|
| 172 |
} |
|---|
| 173 |
catch (ex) { |
|---|
| 174 |
logger.log('Mediator: Failed to open window', ex); |
|---|
| 175 |
openExternal(link); |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
function showNotice(window, params) { |
|---|
| 180 |
windowwatcher.openWindow( |
|---|
| 181 |
window, |
|---|
| 182 |
'chrome://dta/content/about/notice.xul', |
|---|
| 183 |
'_blank', |
|---|
| 184 |
'chrome,centerscreen,all,dialog,modal', |
|---|
| 185 |
params |
|---|
| 186 |
); |
|---|
| 187 |
} |
|---|