BrowserElementPromptService.jsm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. var Cu = Components.utils;
  6. var Ci = Components.interfaces;
  7. var Cc = Components.classes;
  8. var Cr = Components.results;
  9. var Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  10. this.EXPORTED_SYMBOLS = ["BrowserElementPromptService"];
  11. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  12. Cu.import("resource://gre/modules/Services.jsm");
  13. const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
  14. const BROWSER_FRAMES_ENABLED_PREF = "dom.mozBrowserFramesEnabled";
  15. function debug(msg) {
  16. //dump("BrowserElementPromptService - " + msg + "\n");
  17. }
  18. function BrowserElementPrompt(win, browserElementChild) {
  19. this._win = win;
  20. this._browserElementChild = browserElementChild;
  21. }
  22. BrowserElementPrompt.prototype = {
  23. QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt]),
  24. alert: function(title, text) {
  25. this._browserElementChild.showModalPrompt(
  26. this._win, {promptType: "alert", title: title, message: text, returnValue: undefined});
  27. },
  28. alertCheck: function(title, text, checkMsg, checkState) {
  29. // Treat this like a normal alert() call, ignoring the checkState. The
  30. // front-end can do its own suppression of the alert() if it wants.
  31. this.alert(title, text);
  32. },
  33. confirm: function(title, text) {
  34. return this._browserElementChild.showModalPrompt(
  35. this._win, {promptType: "confirm", title: title, message: text, returnValue: undefined});
  36. },
  37. confirmCheck: function(title, text, checkMsg, checkState) {
  38. return this.confirm(title, text);
  39. },
  40. // Each button is described by an object with the following schema
  41. // {
  42. // string messageType, // 'builtin' or 'custom'
  43. // string message, // 'ok', 'cancel', 'yes', 'no', 'save', 'dontsave',
  44. // // 'revert' or a string from caller if messageType was 'custom'.
  45. // }
  46. //
  47. // Expected result from embedder:
  48. // {
  49. // int button, // Index of the button that user pressed.
  50. // boolean checked, // True if the check box is checked.
  51. // }
  52. confirmEx: function(title, text, buttonFlags, button0Title, button1Title,
  53. button2Title, checkMsg, checkState) {
  54. let buttonProperties = this._buildConfirmExButtonProperties(buttonFlags,
  55. button0Title,
  56. button1Title,
  57. button2Title);
  58. let defaultReturnValue = { selectedButton: buttonProperties.defaultButton };
  59. if (checkMsg) {
  60. defaultReturnValue.checked = checkState.value;
  61. }
  62. let ret = this._browserElementChild.showModalPrompt(
  63. this._win,
  64. {
  65. promptType: "custom-prompt",
  66. title: title,
  67. message: text,
  68. defaultButton: buttonProperties.defaultButton,
  69. buttons: buttonProperties.buttons,
  70. showCheckbox: !!checkMsg,
  71. checkboxMessage: checkMsg,
  72. checkboxCheckedByDefault: !!checkState.value,
  73. returnValue: defaultReturnValue
  74. }
  75. );
  76. if (checkMsg) {
  77. checkState.value = ret.checked;
  78. }
  79. return buttonProperties.indexToButtonNumberMap[ret.selectedButton];
  80. },
  81. prompt: function(title, text, value, checkMsg, checkState) {
  82. let rv = this._browserElementChild.showModalPrompt(
  83. this._win,
  84. { promptType: "prompt",
  85. title: title,
  86. message: text,
  87. initialValue: value.value,
  88. returnValue: null });
  89. value.value = rv;
  90. // nsIPrompt::Prompt returns true if the user pressed "OK" at the prompt,
  91. // and false if the user pressed "Cancel".
  92. //
  93. // BrowserElementChild returns null for "Cancel" and returns the string the
  94. // user entered otherwise.
  95. return rv !== null;
  96. },
  97. promptUsernameAndPassword: function(title, text, username, password, checkMsg, checkState) {
  98. throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  99. },
  100. promptPassword: function(title, text, password, checkMsg, checkState) {
  101. throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  102. },
  103. select: function(title, text, aCount, aSelectList, aOutSelection) {
  104. throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  105. },
  106. _buildConfirmExButtonProperties: function(buttonFlags, button0Title,
  107. button1Title, button2Title) {
  108. let r = {
  109. defaultButton: -1,
  110. buttons: [],
  111. // This map is for translating array index to the button number that
  112. // is recognized by Gecko. This shouldn't be exposed to embedder.
  113. indexToButtonNumberMap: []
  114. };
  115. let defaultButton = 0; // Default to Button 0.
  116. if (buttonFlags & Ci.nsIPrompt.BUTTON_POS_1_DEFAULT) {
  117. defaultButton = 1;
  118. } else if (buttonFlags & Ci.nsIPrompt.BUTTON_POS_2_DEFAULT) {
  119. defaultButton = 2;
  120. }
  121. // Properties of each button.
  122. let buttonPositions = [
  123. Ci.nsIPrompt.BUTTON_POS_0,
  124. Ci.nsIPrompt.BUTTON_POS_1,
  125. Ci.nsIPrompt.BUTTON_POS_2
  126. ];
  127. function buildButton(buttonTitle, buttonNumber) {
  128. let ret = {};
  129. let buttonPosition = buttonPositions[buttonNumber];
  130. let mask = 0xff * buttonPosition; // 8 bit mask
  131. let titleType = (buttonFlags & mask) / buttonPosition;
  132. ret.messageType = 'builtin';
  133. switch(titleType) {
  134. case Ci.nsIPrompt.BUTTON_TITLE_OK:
  135. ret.message = 'ok';
  136. break;
  137. case Ci.nsIPrompt.BUTTON_TITLE_CANCEL:
  138. ret.message = 'cancel';
  139. break;
  140. case Ci.nsIPrompt.BUTTON_TITLE_YES:
  141. ret.message = 'yes';
  142. break;
  143. case Ci.nsIPrompt.BUTTON_TITLE_NO:
  144. ret.message = 'no';
  145. break;
  146. case Ci.nsIPrompt.BUTTON_TITLE_SAVE:
  147. ret.message = 'save';
  148. break;
  149. case Ci.nsIPrompt.BUTTON_TITLE_DONT_SAVE:
  150. ret.message = 'dontsave';
  151. break;
  152. case Ci.nsIPrompt.BUTTON_TITLE_REVERT:
  153. ret.message = 'revert';
  154. break;
  155. case Ci.nsIPrompt.BUTTON_TITLE_IS_STRING:
  156. ret.message = buttonTitle;
  157. ret.messageType = 'custom';
  158. break;
  159. default:
  160. // This button is not shown.
  161. return;
  162. }
  163. // If this is the default button, set r.defaultButton to
  164. // the index of this button in the array. This value is going to be
  165. // exposed to the embedder.
  166. if (defaultButton === buttonNumber) {
  167. r.defaultButton = r.buttons.length;
  168. }
  169. r.buttons.push(ret);
  170. r.indexToButtonNumberMap.push(buttonNumber);
  171. }
  172. buildButton(button0Title, 0);
  173. buildButton(button1Title, 1);
  174. buildButton(button2Title, 2);
  175. // If defaultButton is still -1 here, it means the default button won't
  176. // be shown.
  177. if (r.defaultButton === -1) {
  178. throw new Components.Exception("Default button won't be shown",
  179. Cr.NS_ERROR_FAILURE);
  180. }
  181. return r;
  182. },
  183. };
  184. function BrowserElementAuthPrompt() {
  185. }
  186. BrowserElementAuthPrompt.prototype = {
  187. QueryInterface: XPCOMUtils.generateQI([Ci.nsIAuthPrompt2]),
  188. promptAuth: function promptAuth(channel, level, authInfo) {
  189. throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  190. },
  191. asyncPromptAuth: function asyncPromptAuth(channel, callback, context, level, authInfo) {
  192. debug("asyncPromptAuth");
  193. // The cases that we don't support now.
  194. if ((authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) &&
  195. (authInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD)) {
  196. throw Cr.NS_ERROR_FAILURE;
  197. }
  198. let frame = this._getFrameFromChannel(channel);
  199. if (!frame) {
  200. debug("Cannot get frame, asyncPromptAuth fail");
  201. throw Cr.NS_ERROR_FAILURE;
  202. }
  203. let browserElementParent =
  204. BrowserElementPromptService.getBrowserElementParentForFrame(frame);
  205. if (!browserElementParent) {
  206. debug("Failed to load browser element parent.");
  207. throw Cr.NS_ERROR_FAILURE;
  208. }
  209. let consumer = {
  210. QueryInterface: XPCOMUtils.generateQI([Ci.nsICancelable]),
  211. callback: callback,
  212. context: context,
  213. cancel: function() {
  214. this.callback.onAuthCancelled(this.context, false);
  215. this.callback = null;
  216. this.context = null;
  217. }
  218. };
  219. let [hostname, httpRealm] = this._getAuthTarget(channel, authInfo);
  220. let hashKey = level + "|" + hostname + "|" + httpRealm;
  221. let asyncPrompt = this._asyncPrompts[hashKey];
  222. if (asyncPrompt) {
  223. asyncPrompt.consumers.push(consumer);
  224. return consumer;
  225. }
  226. asyncPrompt = {
  227. consumers: [consumer],
  228. channel: channel,
  229. authInfo: authInfo,
  230. level: level,
  231. inProgress: false,
  232. browserElementParent: browserElementParent
  233. };
  234. this._asyncPrompts[hashKey] = asyncPrompt;
  235. this._doAsyncPrompt();
  236. return consumer;
  237. },
  238. // Utilities for nsIAuthPrompt2 ----------------
  239. _asyncPrompts: {},
  240. _asyncPromptInProgress: new WeakMap(),
  241. _doAsyncPrompt: function() {
  242. // Find the key of a prompt whose browser element parent does not have
  243. // async prompt in progress.
  244. let hashKey = null;
  245. for (let key in this._asyncPrompts) {
  246. let prompt = this._asyncPrompts[key];
  247. if (!this._asyncPromptInProgress.get(prompt.browserElementParent)) {
  248. hashKey = key;
  249. break;
  250. }
  251. }
  252. // Didn't find an available prompt, so just return.
  253. if (!hashKey)
  254. return;
  255. let prompt = this._asyncPrompts[hashKey];
  256. let [hostname, httpRealm] = this._getAuthTarget(prompt.channel,
  257. prompt.authInfo);
  258. this._asyncPromptInProgress.set(prompt.browserElementParent, true);
  259. prompt.inProgress = true;
  260. let self = this;
  261. let callback = function(ok, username, password) {
  262. debug("Async auth callback is called, ok = " +
  263. ok + ", username = " + username);
  264. // Here we got the username and password provided by embedder, or
  265. // ok = false if the prompt was cancelled by embedder.
  266. delete self._asyncPrompts[hashKey];
  267. prompt.inProgress = false;
  268. self._asyncPromptInProgress.delete(prompt.browserElementParent);
  269. // Fill authentication information with username and password provided
  270. // by user.
  271. let flags = prompt.authInfo.flags;
  272. if (username) {
  273. if (flags & Ci.nsIAuthInformation.NEED_DOMAIN) {
  274. // Domain is separated from username by a backslash
  275. let idx = username.indexOf("\\");
  276. if (idx == -1) {
  277. prompt.authInfo.username = username;
  278. } else {
  279. prompt.authInfo.domain = username.substring(0, idx);
  280. prompt.authInfo.username = username.substring(idx + 1);
  281. }
  282. } else {
  283. prompt.authInfo.username = username;
  284. }
  285. }
  286. if (password) {
  287. prompt.authInfo.password = password;
  288. }
  289. for (let consumer of prompt.consumers) {
  290. if (!consumer.callback) {
  291. // Not having a callback means that consumer didn't provide it
  292. // or canceled the notification.
  293. continue;
  294. }
  295. try {
  296. if (ok) {
  297. debug("Ok, calling onAuthAvailable to finish auth");
  298. consumer.callback.onAuthAvailable(consumer.context, prompt.authInfo);
  299. } else {
  300. debug("Cancelled, calling onAuthCancelled to finish auth.");
  301. consumer.callback.onAuthCancelled(consumer.context, true);
  302. }
  303. } catch (e) { /* Throw away exceptions caused by callback */ }
  304. }
  305. // Process the next prompt, if one is pending.
  306. self._doAsyncPrompt();
  307. };
  308. let runnable = {
  309. run: function() {
  310. // Call promptAuth of browserElementParent, to show the prompt.
  311. prompt.browserElementParent.promptAuth(
  312. self._createAuthDetail(prompt.channel, prompt.authInfo),
  313. callback);
  314. }
  315. }
  316. Services.tm.currentThread.dispatch(runnable, Ci.nsIThread.DISPATCH_NORMAL);
  317. },
  318. _getFrameFromChannel: function(channel) {
  319. let loadContext = channel.notificationCallbacks.getInterface(Ci.nsILoadContext);
  320. return loadContext.topFrameElement;
  321. },
  322. _createAuthDetail: function(channel, authInfo) {
  323. let [hostname, httpRealm] = this._getAuthTarget(channel, authInfo);
  324. return {
  325. host: hostname,
  326. path: channel.URI.path,
  327. realm: httpRealm,
  328. username: authInfo.username,
  329. isProxy: !!(authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY),
  330. isOnlyPassword: !!(authInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD)
  331. };
  332. },
  333. // The code is taken from nsLoginManagerPrompter.js, with slight
  334. // modification for parameter name consistency here.
  335. _getAuthTarget : function (channel, authInfo) {
  336. let hostname, realm;
  337. // If our proxy is demanding authentication, don't use the
  338. // channel's actual destination.
  339. if (authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) {
  340. if (!(channel instanceof Ci.nsIProxiedChannel))
  341. throw new Error("proxy auth needs nsIProxiedChannel");
  342. let info = channel.proxyInfo;
  343. if (!info)
  344. throw new Error("proxy auth needs nsIProxyInfo");
  345. // Proxies don't have a scheme, but we'll use "moz-proxy://"
  346. // so that it's more obvious what the login is for.
  347. var idnService = Cc["@mozilla.org/network/idn-service;1"].
  348. getService(Ci.nsIIDNService);
  349. hostname = "moz-proxy://" +
  350. idnService.convertUTF8toACE(info.host) +
  351. ":" + info.port;
  352. realm = authInfo.realm;
  353. if (!realm)
  354. realm = hostname;
  355. return [hostname, realm];
  356. }
  357. hostname = this._getFormattedHostname(channel.URI);
  358. // If a HTTP WWW-Authenticate header specified a realm, that value
  359. // will be available here. If it wasn't set or wasn't HTTP, we'll use
  360. // the formatted hostname instead.
  361. realm = authInfo.realm;
  362. if (!realm)
  363. realm = hostname;
  364. return [hostname, realm];
  365. },
  366. /**
  367. * Strip out things like userPass and path for display.
  368. */
  369. _getFormattedHostname : function(uri) {
  370. return uri.scheme + "://" + uri.hostPort;
  371. },
  372. };
  373. function AuthPromptWrapper(oldImpl, browserElementImpl) {
  374. this._oldImpl = oldImpl;
  375. this._browserElementImpl = browserElementImpl;
  376. }
  377. AuthPromptWrapper.prototype = {
  378. QueryInterface: XPCOMUtils.generateQI([Ci.nsIAuthPrompt2]),
  379. promptAuth: function(channel, level, authInfo) {
  380. if (this._canGetParentElement(channel)) {
  381. return this._browserElementImpl.promptAuth(channel, level, authInfo);
  382. } else {
  383. return this._oldImpl.promptAuth(channel, level, authInfo);
  384. }
  385. },
  386. asyncPromptAuth: function(channel, callback, context, level, authInfo) {
  387. if (this._canGetParentElement(channel)) {
  388. return this._browserElementImpl.asyncPromptAuth(channel, callback, context, level, authInfo);
  389. } else {
  390. return this._oldImpl.asyncPromptAuth(channel, callback, context, level, authInfo);
  391. }
  392. },
  393. _canGetParentElement: function(channel) {
  394. try {
  395. let context = channel.notificationCallbacks.getInterface(Ci.nsILoadContext);
  396. let frame = context.topFrameElement;
  397. if (!frame) {
  398. // This function returns a boolean value
  399. return !!context.nestedFrameId;
  400. }
  401. if (!BrowserElementPromptService.getBrowserElementParentForFrame(frame))
  402. return false;
  403. return true;
  404. } catch (e) {
  405. return false;
  406. }
  407. }
  408. };
  409. function BrowserElementPromptFactory(toWrap) {
  410. this._wrapped = toWrap;
  411. }
  412. BrowserElementPromptFactory.prototype = {
  413. classID: Components.ID("{24f3d0cf-e417-4b85-9017-c9ecf8bb1299}"),
  414. QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptFactory]),
  415. _mayUseNativePrompt: function() {
  416. try {
  417. return Services.prefs.getBoolPref("browser.prompt.allowNative");
  418. } catch (e) {
  419. // This properity is default to true.
  420. return true;
  421. }
  422. },
  423. _getNativePromptIfAllowed: function(win, iid, err) {
  424. if (this._mayUseNativePrompt())
  425. return this._wrapped.getPrompt(win, iid);
  426. else {
  427. // Not allowed, throw an exception.
  428. throw err;
  429. }
  430. },
  431. getPrompt: function(win, iid) {
  432. // It is possible for some object to get a prompt without passing
  433. // valid reference of window, like nsNSSComponent. In such case, we
  434. // should just fall back to the native prompt service
  435. if (!win)
  436. return this._getNativePromptIfAllowed(win, iid, Cr.NS_ERROR_INVALID_ARG);
  437. if (iid.number != Ci.nsIPrompt.number &&
  438. iid.number != Ci.nsIAuthPrompt2.number) {
  439. debug("We don't recognize the requested IID (" + iid + ", " +
  440. "allowed IID: " +
  441. "nsIPrompt=" + Ci.nsIPrompt + ", " +
  442. "nsIAuthPrompt2=" + Ci.nsIAuthPrompt2 + ")");
  443. return this._getNativePromptIfAllowed(win, iid, Cr.NS_ERROR_INVALID_ARG);
  444. }
  445. // Try to find a BrowserElementChild for the window.
  446. let browserElementChild =
  447. BrowserElementPromptService.getBrowserElementChildForWindow(win);
  448. if (iid.number === Ci.nsIAuthPrompt2.number) {
  449. debug("Caller requests an instance of nsIAuthPrompt2.");
  450. if (browserElementChild) {
  451. // If we are able to get a BrowserElementChild, it means that
  452. // the auth prompt is for a mozbrowser. Therefore we don't need to
  453. // fall back.
  454. return new BrowserElementAuthPrompt().QueryInterface(iid);
  455. }
  456. // Because nsIAuthPrompt2 is called in parent process. If caller
  457. // wants nsIAuthPrompt2 and we cannot get BrowserElementchild,
  458. // it doesn't mean that we should fallback. It is possible that we can
  459. // get the BrowserElementParent from nsIChannel that passed to
  460. // functions of nsIAuthPrompt2.
  461. if (this._mayUseNativePrompt()) {
  462. return new AuthPromptWrapper(
  463. this._wrapped.getPrompt(win, iid),
  464. new BrowserElementAuthPrompt().QueryInterface(iid))
  465. .QueryInterface(iid);
  466. } else {
  467. // Falling back is not allowed, so we don't need wrap the
  468. // BrowserElementPrompt.
  469. return new BrowserElementAuthPrompt().QueryInterface(iid);
  470. }
  471. }
  472. if (!browserElementChild) {
  473. debug("We can't find a browserElementChild for " +
  474. win + ", " + win.location);
  475. return this._getNativePromptIfAllowed(win, iid, Cr.NS_ERROR_FAILURE);
  476. }
  477. debug("Returning wrapped getPrompt for " + win);
  478. return new BrowserElementPrompt(win, browserElementChild)
  479. .QueryInterface(iid);
  480. }
  481. };
  482. this.BrowserElementPromptService = {
  483. QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
  484. Ci.nsISupportsWeakReference]),
  485. _initialized: false,
  486. _init: function() {
  487. if (this._initialized) {
  488. return;
  489. }
  490. // If the pref is disabled, do nothing except wait for the pref to change.
  491. if (!this._browserFramesPrefEnabled()) {
  492. var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
  493. prefs.addObserver(BROWSER_FRAMES_ENABLED_PREF, this, /* ownsWeak = */ true);
  494. return;
  495. }
  496. this._initialized = true;
  497. this._browserElementParentMap = new WeakMap();
  498. var os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  499. os.addObserver(this, "outer-window-destroyed", /* ownsWeak = */ true);
  500. // Wrap the existing @mozilla.org/prompter;1 implementation.
  501. var contractID = "@mozilla.org/prompter;1";
  502. var oldCID = Cm.contractIDToCID(contractID);
  503. var newCID = BrowserElementPromptFactory.prototype.classID;
  504. var oldFactory = Cm.getClassObject(Cc[contractID], Ci.nsIFactory);
  505. if (oldCID == newCID) {
  506. debug("WARNING: Wrapped prompt factory is already installed!");
  507. return;
  508. }
  509. Cm.unregisterFactory(oldCID, oldFactory);
  510. var oldInstance = oldFactory.createInstance(null, Ci.nsIPromptFactory);
  511. var newInstance = new BrowserElementPromptFactory(oldInstance);
  512. var newFactory = {
  513. createInstance: function(outer, iid) {
  514. if (outer != null) {
  515. throw Cr.NS_ERROR_NO_AGGREGATION;
  516. }
  517. return newInstance.QueryInterface(iid);
  518. }
  519. };
  520. Cm.registerFactory(newCID,
  521. "BrowserElementPromptService's prompter;1 wrapper",
  522. contractID, newFactory);
  523. debug("Done installing new prompt factory.");
  524. },
  525. _getOuterWindowID: function(win) {
  526. return win.QueryInterface(Ci.nsIInterfaceRequestor)
  527. .getInterface(Ci.nsIDOMWindowUtils)
  528. .outerWindowID;
  529. },
  530. _browserElementChildMap: {},
  531. mapWindowToBrowserElementChild: function(win, browserElementChild) {
  532. this._browserElementChildMap[this._getOuterWindowID(win)] = browserElementChild;
  533. },
  534. unmapWindowToBrowserElementChild: function(win) {
  535. delete this._browserElementChildMap[this._getOuterWindowID(win)];
  536. },
  537. getBrowserElementChildForWindow: function(win) {
  538. // We only have a mapping for <iframe mozbrowser>s, not their inner
  539. // <iframes>, so we look up win.top below. window.top (when called from
  540. // script) respects <iframe mozbrowser> boundaries.
  541. return this._browserElementChildMap[this._getOuterWindowID(win.top)];
  542. },
  543. mapFrameToBrowserElementParent: function(frame, browserElementParent) {
  544. this._browserElementParentMap.set(frame, browserElementParent);
  545. },
  546. getBrowserElementParentForFrame: function(frame) {
  547. return this._browserElementParentMap.get(frame);
  548. },
  549. _observeOuterWindowDestroyed: function(outerWindowID) {
  550. let id = outerWindowID.QueryInterface(Ci.nsISupportsPRUint64).data;
  551. debug("observeOuterWindowDestroyed " + id);
  552. delete this._browserElementChildMap[outerWindowID.data];
  553. },
  554. _browserFramesPrefEnabled: function() {
  555. var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
  556. try {
  557. return prefs.getBoolPref(BROWSER_FRAMES_ENABLED_PREF);
  558. }
  559. catch(e) {
  560. return false;
  561. }
  562. },
  563. observe: function(subject, topic, data) {
  564. switch(topic) {
  565. case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
  566. if (data == BROWSER_FRAMES_ENABLED_PREF) {
  567. this._init();
  568. }
  569. break;
  570. case "outer-window-destroyed":
  571. this._observeOuterWindowDestroyed(subject);
  572. break;
  573. default:
  574. debug("Observed unexpected topic " + topic);
  575. }
  576. }
  577. };
  578. BrowserElementPromptService._init();