Bug 531170 - Tabs don't sync from fennec

Add listeners for Fennec tab events the same way we do it for Firefox and share listener logic for various event types.

--HG--
extra : rebase_source : 685e45c16052db3d285c25c68db47680b7d15f72
This commit is contained in:
Edward Lee 2009-11-25 15:14:56 -08:00
Родитель cb27e2d2e6
Коммит 84acd63006
1 изменённых файлов: 23 добавлений и 78 удалений

Просмотреть файл

@ -41,8 +41,6 @@ const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cu = Components.utils; const Cu = Components.utils;
const TAB_TIME_ATTR = "weave.tabEngine.lastUsed.timeStamp";
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
Cu.import("resource://weave/engines.js"); Cu.import("resource://weave/engines.js");
@ -215,7 +213,7 @@ TabStore.prototype = {
continue; continue;
// Get the time the tab was last used // Get the time the tab was last used
let lastUsedTimestamp = tab.getAttribute(TAB_TIME_ATTR); let lastUsedTimestamp = tab.lastUsed;
// Get title of current page // Get title of current page
let currentPage = tabState.entries[tabState.entries.length - 1]; let currentPage = tabState.entries[tabState.entries.length - 1];
@ -357,101 +355,48 @@ TabTracker.prototype = {
this.resetChanged(); this.resetChanged();
// Make sure "this" pointer is always set correctly for event listeners // Make sure "this" pointer is always set correctly for event listeners
this.onTabOpened = Utils.bind2(this, this.onTabOpened); this.onTab = Utils.bind2(this, this.onTab);
this.onTabClosed = Utils.bind2(this, this.onTabClosed);
this.onTabSelected = Utils.bind2(this, this.onTabSelected);
// TODO Figure out how this will work on Fennec.
// Register as an observer so we can catch windows opening and closing: // Register as an observer so we can catch windows opening and closing:
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"] Svc.WinWatcher.registerNotification(this);
.getService(Ci.nsIWindowWatcher);
ww.registerNotification(this);
/* Also directly register the listeners for any browser window alread // Also register listeners on already open windows
* open: */ let wins = Svc.WinMediator.getEnumerator("navigator:browser");
let wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] while (wins.hasMoreElements())
.getService(Components.interfaces.nsIWindowMediator); this._registerListenersForWindow(wins.getNext());
let enumerator = wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
this._registerListenersForWindow(enumerator.getNext());
}
},
_getBrowser: function TabTracker__getBrowser(window) {
// Make sure the window is browser-like
if (typeof window.getBrowser != "function")
return null;
// Make sure it's a tabbrowser-like window
let browser = window.getBrowser();
if (browser == null || typeof browser.tabContainer != "object")
return null;
return browser;
}, },
_registerListenersForWindow: function TabTracker__registerListen(window) { _registerListenersForWindow: function TabTracker__registerListen(window) {
let browser = this._getBrowser(window); this._log.trace("Registering tab listeners in new window");
if (browser == null)
return;
//this._log.trace("Registering tab listeners in new window.\n"); // For each topic, add or remove onTab as the listener
//dump("Tab listeners registered!\n"); let topics = ["TabOpen", "TabClose", "TabSelect"];
let container = browser.tabContainer; let onTab = this.onTab;
container.addEventListener("TabOpen", this.onTabOpened, false); let addRem = function(add) topics.forEach(function(topic) {
container.addEventListener("TabClose", this.onTabClosed, false); window[(add ? "add" : "remove") + "EventListener"](topic, onTab, false);
container.addEventListener("TabSelect", this.onTabSelected, false); });
},
_unRegisterListenersForWindow: function TabTracker__unregister(window) { // Add the listeners now and remove them on unload
let browser = this._getBrowser(window); addRem(true);
if (browser == null) window.addEventListener("unload", function() addRem(false), false);
return;
let container = browser.tabContainer;
container.removeEventListener("TabOpen", this.onTabOpened, false);
container.removeEventListener("TabClose", this.onTabClosed, false);
container.removeEventListener("TabSelect", this.onTabSelected, false);
}, },
observe: function TabTracker_observe(aSubject, aTopic, aData) { observe: function TabTracker_observe(aSubject, aTopic, aData) {
/* Called when a window opens or closes. Make sure that every // Add tab listeners now that a window has opened
* window has the appropriate listeners registered. */
let window = aSubject.QueryInterface(Ci.nsIDOMWindow); let window = aSubject.QueryInterface(Ci.nsIDOMWindow);
// TODO figure out how this will work in Fennec. if (aTopic == "domwindowopened")
if (aTopic == "domwindowopened") {
this._registerListenersForWindow(window); this._registerListenersForWindow(window);
} else if (aTopic == "domwindowclosed") {
this._unRegisterListenersForWindow(window);
}
}, },
_upScore: function _upScore(amount) { onTab: function onTab(event) {
this.score += amount; this._log.trace(event.type);
this.score += 1;
this._changedIDs[Clients.clientID] = true; this._changedIDs[Clients.clientID] = true;
},
onTabOpened: function TabTracker_onTabOpened(event) {
// Store a timestamp in the tab to track when it was last used // Store a timestamp in the tab to track when it was last used
this._log.trace("Tab opened."); event.originalTarget.lastUsed = Math.floor(Date.now() / 1000);
event.target.setAttribute(TAB_TIME_ATTR, event.timeStamp);
this._upScore(1);
}, },
onTabClosed: function TabTracker_onTabSelected(event) {
this._log.trace("Tab closed.");
this._upScore(1);
},
onTabSelected: function TabTracker_onTabSelected(event) {
// Update the tab's timestamp
this._log.trace("Tab selected.");
event.target.setAttribute(TAB_TIME_ATTR, event.timeStamp);
this._upScore(1);
},
// TODO: Also listen for tabs loading new content?
get changedIDs() this._changedIDs, get changedIDs() this._changedIDs,
// Provide a way to empty out the changed ids // Provide a way to empty out the changed ids