Bug 332774 Create API allowing extensions to hook into the session-restore service (r=mconnor)

git-svn-id: svn://10.0.0.236/trunk@201597 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
dietrich%mozilla.com
2006-07-05 20:56:05 +00:00
parent 2fc058199a
commit 0de0160110
3 changed files with 205 additions and 2 deletions

View File

@@ -39,6 +39,7 @@
#include "nsISupports.idl"
interface nsIDOMWindow;
interface nsIDOMNode;
/**
* nsISessionStore keeps track of the current browsing state - i.e.
@@ -46,7 +47,7 @@ interface nsIDOMWindow;
* - and allows to restore everything into one window.
*/
[scriptable, uuid(38bad250-0021-11db-92e3-0800200c9a66)]
[scriptable, uuid(320996f0-0c5e-11db-9cd8-0800200c9a66)]
interface nsISessionStore : nsISupports
{
/**
@@ -66,4 +67,51 @@ interface nsISessionStore : nsISupports
* Indicates the window to be restored (FIFO ordered).
*/
void undoCloseTab(in nsIDOMWindow aWindow, in unsigned long aIndex);
/**
* @param aWindow is the window to get the value for.
* @param aKey is the value's name.
*
* @return A string value or "" if none is set.
*/
string getWindowValue(in nsIDOMWindow aWindow, in string aKey);
/**
* @param aWindow is the window to set the value for.
* @param aKey is the value's name.
* @param aStringValue is the value itself (use toSource/eval before setting JS objects).
*/
void setWindowValue(in nsIDOMWindow aWindow, in string aKey, in string aStringValue);
/**
* @param aWindow is the window to get the value for.
* @param aKey is the value's name.
*/
void deleteWindowValue(in nsIDOMWindow aWindow, in string aKey);
/**
* @param aTab is the tab to get the value for.
* @param aKey is the value's name.
*
* @return A string value or "" if none is set.
*/
string getTabValue(in nsIDOMNode aTab, in string aKey);
/**
* @param aTab is the tab to set the value for.
* @param aKey is the value's name.
* @param aStringValue is the value itself (use toSource/eval before setting JS objects).
*/
void setTabValue(in nsIDOMNode aTab, in string aKey, in string aStringValue);
/**
* @param aTab is the tab to get the value for.
* @param aKey is the value's name.
*/
void deleteTabValue(in nsIDOMNode aTab, in string aKey);
/**
* @param aName is the name of the attribute to save/restore for all xul:tabs.
*/
void persistTabAttribute(in string aName);
};

View File

@@ -737,6 +737,13 @@ SessionStoreService.prototype = {
}
},
deleteWindowValue: function sss_deleteWindowValue(aWindow, aKey) {
if (this._windows[aWindow.__SSi].extData[aKey])
delete this._windows[aWindow.__SSi].extData[aKey];
else
Components.returnCode = -1; //zeniko: or should we rather fail silently?
},
getTabValue: function sss_getTabValue(aTab, aKey) {
var data = aTab.__SS_extdata || {};
return data[aKey] || "";
@@ -747,9 +754,17 @@ SessionStoreService.prototype = {
aTab.__SS_extdata = {};
}
aTab.__SS_extdata[aKey] = aStringValue;
this.saveStateDelayed(aTop.ownerDocument.defaultView);
this.saveStateDelayed(aTab.ownerDocument.defaultView);
},
deleteTabValue: function sss_deleteTabValue(aTab, aKey) {
if (aTab.__SS_extdata[aKey])
delete aTab.__SS_extdata[aKey];
else
Components.returnCode = -1; //zeniko: or should we rather fail silently?
},
persistTabAttribute: function sss_persistTabAttribute(aName) {
this.xulAttributes.push(aName);
this.saveStateDelayed();

View File

@@ -0,0 +1,140 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
title="nsSessionstore Tests">
<script type="text/javascript">
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
var ssTests = {
log: function sst_log(aMsg) {
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
},
run: function sst_run() {
var tests = ["componentExists", "getService", "windowValues", "tabValues"];
var results = {};
tests.forEach(function(aTest) {
try {
results[aTest] = ssTests[aTest]();
} catch(ex) {
results[aTest] = false;
}
});
return results;
},
componentExists: function sst_componentExists() {
return Cc["@mozilla.org/browser/sessionstore;1"] ? true : false;
},
getService: function sst_getService() {
return Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore) ? true : false;
},
// setWindowValue
windowValues: function sst_windowValues() {
var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
if (!ss)
return false;
var key = "key1";
var value = "value1";
// get current window, tabbrowser
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var windowEnumerator = wm.getEnumerator("navigator:browser");
var currentWindow = windowEnumerator.getNext();
var tabbrowser = currentWindow.getBrowser();
// create a new tab
var newTab = tabbrowser.addTab("http://www.mozilla.org");
// test adding
if (ss.setWindowValue(currentWindow, key, value) == -1) {
this.log("setWindowValue failed");
return false;
}
// test retrieving
var storedValue = ss.getWindowValue(currentWindow, key);
if (value != storedValue)
return false;
// test deleting
if (ss.deleteWindowValue(currentWindow, key) == -1) {
this.log("deleteWindowValue failed");
return false;
}
if (ss.getWindowValue(currentWindow, key)) {
this.log("deleteWindowValue failed: data was not deleted");
return false;
}
// clean up
tabbrowser.removeTab(newTab);
return true;
},
// tabValues
tabValues: function sst_tabValues() {
var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
if (!ss)
return false;
var key = "key1";
var value = "value1";
// get current window, tabbrowser
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var windowEnumerator = wm.getEnumerator("navigator:browser");
var currentWindow = windowEnumerator.getNext();
var tabbrowser = currentWindow.getBrowser();
// create a new tab
var newTab = tabbrowser.addTab("http://www.mozilla.org");
// test adding
if (ss.setTabValue(newTab, key, value) == -1) {
this.log("setTabValue failed");
return false;
}
// test retrieving
var storedValue = ss.getTabValue(newTab, key);
if (value != storedValue)
return false;
// test deleting
if (ss.deleteTabValue(newTab, key) == -1) {
this.log("deleteTabValue failed");
return false;
}
if (ss.getTabValue(newTab, key)) {
this.log("deleteTabValue failed: data was not deleted");
return false;
}
// clean up
tabbrowser.removeTab(newTab);
return true;
}
};
function debug(aMsg) {
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
}
var results = ssTests.run();
for (key in results) {
debug(key + ": " + results[key]);
}
</script>
</window>