openLocationLastURL.jsm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const LAST_URL_PREF = "general.open_location.last_url";
  5. const nsISupportsString = Components.interfaces.nsISupportsString;
  6. const Ci = Components.interfaces;
  7. Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
  8. this.EXPORTED_SYMBOLS = [ "OpenLocationLastURL" ];
  9. var prefSvc = Components.classes["@mozilla.org/preferences-service;1"]
  10. .getService(Components.interfaces.nsIPrefBranch);
  11. var gOpenLocationLastURLData = "";
  12. var observer = {
  13. QueryInterface: function(aIID) {
  14. if (aIID.equals(Components.interfaces.nsIObserver) ||
  15. aIID.equals(Components.interfaces.nsISupports) ||
  16. aIID.equals(Components.interfaces.nsISupportsWeakReference))
  17. return this;
  18. throw Components.results.NS_NOINTERFACE;
  19. },
  20. observe: function(aSubject, aTopic, aData) {
  21. switch (aTopic) {
  22. case "last-pb-context-exited":
  23. gOpenLocationLastURLData = "";
  24. break;
  25. case "browser:purge-session-history":
  26. prefSvc.clearUserPref(LAST_URL_PREF);
  27. gOpenLocationLastURLData = "";
  28. break;
  29. }
  30. }
  31. };
  32. var os = Components.classes["@mozilla.org/observer-service;1"]
  33. .getService(Components.interfaces.nsIObserverService);
  34. os.addObserver(observer, "last-pb-context-exited", true);
  35. os.addObserver(observer, "browser:purge-session-history", true);
  36. this.OpenLocationLastURL = function OpenLocationLastURL(aWindow) {
  37. this.window = aWindow;
  38. }
  39. OpenLocationLastURL.prototype = {
  40. isPrivate: function() {
  41. // Assume not in private browsing mode, unless the browser window is
  42. // in private mode.
  43. if (!this.window)
  44. return false;
  45. return PrivateBrowsingUtils.isWindowPrivate(this.window);
  46. },
  47. get value() {
  48. if (this.isPrivate())
  49. return gOpenLocationLastURLData;
  50. else {
  51. try {
  52. return prefSvc.getComplexValue(LAST_URL_PREF, nsISupportsString).data;
  53. }
  54. catch (e) {
  55. return "";
  56. }
  57. }
  58. },
  59. set value(val) {
  60. if (typeof val != "string")
  61. val = "";
  62. if (this.isPrivate())
  63. gOpenLocationLastURLData = val;
  64. else {
  65. let str = Components.classes["@mozilla.org/supports-string;1"]
  66. .createInstance(Components.interfaces.nsISupportsString);
  67. str.data = val;
  68. prefSvc.setComplexValue(LAST_URL_PREF, nsISupportsString, str);
  69. }
  70. },
  71. reset: function() {
  72. prefSvc.clearUserPref(LAST_URL_PREF);
  73. gOpenLocationLastURLData = "";
  74. }
  75. };