Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 677970 - dashboard initialized twice for each new window. r=anant
  • Loading branch information
mixedpuppy committed Aug 16, 2011
2 parents 0d04a51 + efc6842 commit 328f4d6
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 149 deletions.
99 changes: 28 additions & 71 deletions addons/jetpack/lib/main.js
Expand Up @@ -48,18 +48,16 @@ Cu.import("resource://gre/modules/AddonManager.jsm", tmp);
Cu.import("resource://gre/modules/XPCOMUtils.jsm", tmp);
var {XPCOMUtils, AddonManager, Services} = tmp;

/**
* openwebapps
*
* per-window initialization for owa
*/
function openwebapps(win, getUrlCB)
{
this._getUrlCB = getUrlCB;
this._window = win;

Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService)
.addObserver( this, "openwebapp-installed", false);
Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService)
.addObserver( this, "openwebapp-uninstalled", false);

// Base initialization
let tmp = {};
tmp = require("./api");
Expand All @@ -72,16 +70,6 @@ function openwebapps(win, getUrlCB)

tmp = require("./services");
this._services = new tmp.serviceInvocationHandler(this._window);

tmp = {};
Cu.import("resource://services-sync/main.js", tmp);
if (tmp.Weave.Status.ready) {
this._registerSyncEngine();
} else {
tmp = {};
Cu.import("resource://services-sync/util.js", tmp);
tmp.Svc.Obs.add("weave:service:ready", this);
}

if (this.pendingRegistrations) {
for each (let reg in this.pendingRegistrations) {
Expand All @@ -91,10 +79,8 @@ function openwebapps(win, getUrlCB)
}

// Keep an eye out for LINK headers that contain manifests:
let obs = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
this._linkListenerAttached = false;
obs.addObserver(this, 'content-document-global-created', false);
Services.obs.addObserver( this, "content-document-global-created", false);

this._ui = new ui.openwebappsUI(win, getUrlCB, this._repo);

Expand All @@ -119,6 +105,8 @@ function openwebapps(win, getUrlCB)
if (e.target.pinned) return;

let browser = self._window.gBrowser.getBrowserForTab(e.target);
// empty tabs have no currentURI
if (!browser || !browser.currentURI) return;
let origin = url.URLParse(browser.currentURI.spec)
.originOnly().toString();

Expand Down Expand Up @@ -265,56 +253,10 @@ openwebapps.prototype = {
}
}
});
win.appinjector.registerAction(function() {
// Clear out the current page URL on every page load
let toolbarButton = win.document.getElementById("openwebapps-toolbar-button");
if (toolbarButton) {
toolbarButton.classList.remove("highlight");
}
repo.setCurrentPageAppURL(null);
});
},

_registerSyncEngine: function() {
/*
let tmp = {};
Cu.import("resource://services-sync/main.js", tmp);
tmp.AppsEngine = require("./sync").AppsEngine;
if (!tmp.Weave.Engines.get("apps")) {
tmp.Weave.Engines.register(tmp.AppsEngine);
unloaders.push(function() {
tmp.Weave.Engines.unregister("apps");
});
}
let prefname = "services.sync.engine.apps";
if (Services.prefs.getPrefType(prefname) ==
Ci.nsIPrefBranch.PREF_INVALID) {
Services.prefs.setBoolPref(prefname, true);
}
*/
},

observe: function(subject, topic, data) {
if (topic == "weave:service:ready") {
this._registerSyncEngine();
} else if (topic == "openwebapp-installed") {
// let installData = JSON.parse(data)
// this._ui._renderDockIcons(installData.origin);
// if (!installData.hidePostInstallPrompt) {
// this._ui._showDock();
// }
try{
this._ui._updateDashboard('yes');
} catch (e) {
console.log(e);
}

} else if (topic == "openwebapp-uninstalled") {
// this._ui._renderDockIcons();
this._ui._updateDashboard();
} else if (topic == "content-document-global-created") {
if (topic == "content-document-global-created") {

let mainWindow = subject
.QueryInterface(Ci.nsIInterfaceRequestor)
Expand Down Expand Up @@ -510,9 +452,25 @@ let AboutAppsHome = {
//----- end about:apps (but see ComponentRegistrar call in startup())

let unloaders = [];

/**
* startup
*
* all per-instance initialization should be started from here. The window
* watcher will create an instance of openwebapps per navigator window.
* addon-sdk widgets manage their own per-window initialization, don't replicate
* that.
*
* Notifications are made per-window after owa has finished it's per-window
* to allow other addons with owa as a dependency to have a reliable
* way to initialize per-window.
*/
function startup(getUrlCB) {
/* Initialize simple storage */
if (!simple.storage.links) simple.storage.links = {};
Services.obs.notifyObservers(null, "openwebapps-mediator-init", "");

ui.dashboard.init();

/* We use winWatcher to create an instance per window (current and future) */
let iter = Cc["@mozilla.org/appshell/window-mediator;1"]
Expand All @@ -521,6 +479,7 @@ function startup(getUrlCB) {
while (iter.hasMoreElements()) {
let aWindow = iter.getNext().QueryInterface(Ci.nsIDOMWindow);
aWindow.apps = new openwebapps(aWindow, getUrlCB);
Services.obs.notifyObservers(aWindow, "openwebapps-mediator-load", "");
}
function winWatcher(subject, topic) {
if (topic != "domwindowopened")
Expand All @@ -530,6 +489,7 @@ function startup(getUrlCB) {
let doc = subject.document.documentElement;
if (doc.getAttribute("windowtype") == "navigator:browser") {
subject.apps = new openwebapps(subject, getUrlCB);
Services.obs.notifyObservers(subject, "openwebapps-mediator-load", "");
}
}, false);
}
Expand All @@ -553,11 +513,8 @@ function startup(getUrlCB) {
});

// Broadcast that we're done, in case anybody is listening
let observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);

let tmp = require("api");
observerService.notifyObservers(tmp.FFRepoImplService, "openwebapps-startup-complete", "");
Services.obs.notifyObservers(tmp.FFRepoImplService, "openwebapps-startup-complete", "");
}

function shutdown(why)
Expand Down
186 changes: 108 additions & 78 deletions addons/jetpack/lib/ui.js
Expand Up @@ -42,6 +42,10 @@ const url = require("./urlmatch");
const HTML_NS = "http://www.w3.org/1999/xhtml";
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

let tmp = {};
Cu.import("resource://gre/modules/Services.jsm", tmp);
let {Services} = tmp;

/* l10n support. See https://github.com/Mardak/restartless/examples/l10nDialogs */
function getString(name, args, plural) {
let str;
Expand Down Expand Up @@ -93,6 +97,109 @@ getString.init = function(getUrlCB, getAlternate) {
getString.fallback = getBundle("en-US");
}


/**
* dashboard
*
* the dashboard widget is created once during the addon startup. addon sdk
* handles adding the widget to each new window
*/
var dashboard = {
init: function() {
let tmp = {};
tmp = require("./api");
this._repo = tmp.FFRepoImplService;

Services.obs.addObserver( this, "openwebapp-installed", false);
Services.obs.addObserver( this, "openwebapp-uninstalled", false);

let self = this;
let data = require("self").data;
let thePanel = require("panel").Panel({
height: 108,
width: 754,
position: "topcenter bottomright",
contentURL: data.url("panel.html"),
contentScriptFile: [data.url("base32.js"),
data.url("jquery-1.4.2.min.js"),
data.url("panel.js") ],

onShow: function() { self._repo.list(function(apps) {
thePanel.port.emit("theList", apps);
});
}
});

thePanel.port.on("getList", function(arg) {
self._repo.list(function(apps) {
thePanel.port.emit("theList", apps);
});
});

thePanel.port.on("launch", function(arg) {
self._repo.launch(arg);
thePanel.hide();
});

//load and save dash state, just using a constant string for now
thePanel.port.on("loadState", function(arg) {
self._repo.loadState("owadock", function(state) {
thePanel.port.emit("theState", state);
});
});

thePanel.port.on("saveState", function(arg) {
self._repo.saveState("owadock", arg);
});

this._panel = thePanel;

this._widget = widgets.Widget({
id: "openwebapps-toolbar-button",
label: "Web Apps",
width: 60,
contentURL: require("self").data.url("widget-label.html"),
panel: thePanel
});
},

/**
* update
*
* update the dashboard with any changes in the apps list
* XXX Dashboard should just have a listener built in
*/
update: function(show) {
let self = this;
self._repo.list(function(apps) {
self._panel.port.emit("theList", apps);
});

if (show != undefined) {
let WM = Cc['@mozilla.org/appshell/window-mediator;1']
.getService(Ci.nsIWindowMediator);
let currentDoc = WM.getMostRecentWindow("navigator:browser").document;
var widgetAnchor = currentDoc.getElementById("widget:" +
require("self").id + "-openwebapps-toolbar-button");

self._panel.show(widgetAnchor, "topcenter bottomright");
}

},
observe: function(subject, topic, data) {
if (topic == "openwebapp-installed") {
try{
dashboard.update('yes');
} catch (e) {
console.log(e);
}
} else if (topic == "openwebapp-uninstalled") {
dashboard.update();
}
}
}


function openwebappsUI(win, getUrlCB, repo)
{
this._repo = repo;
Expand All @@ -118,9 +225,6 @@ openwebappsUI.prototype = {
"\" type=\"text/css\""
);
doc.insertBefore(pi, doc.firstChild);

this._addToolbarButton();
// this._addDock();
},

_setupTabHandling: function() {
Expand Down Expand Up @@ -148,81 +252,6 @@ openwebappsUI.prototype = {
// false);
},


_addToolbarButton: function() {
let self = this;
let data = require("self").data;

console.log(data.url("panel.html"));
let thePanel = require("panel").Panel({
height: 108,
width: 754,
position: "topcenter bottomright",
contentURL: data.url("panel.html"),
contentScriptFile: [data.url("base32.js"),
data.url("jquery-1.4.2.min.js"),
data.url("panel.js") ],

onShow: function() { self._repo.list(function(apps) {
thePanel.port.emit("theList", apps);
}); },
position: "topcenter bottomright"
});

thePanel.port.on("getList", function(arg) {
self._repo.list(function(apps) {
thePanel.port.emit("theList", apps);
});
});

thePanel.port.on("launch", function(arg) {
self._repo.launch(arg);
thePanel.hide();
});

//load and save dash state, just using a constant string for now
thePanel.port.on("loadState", function(arg) {
self._repo.loadState("owadock", function(state) {
thePanel.port.emit("theState", state);
});
});

thePanel.port.on("saveState", function(arg) {
self._repo.saveState("owadock", arg);
});

widgets.Widget({
id: "openwebapps-toolbar-button",
label: "Web Apps",
width: 60,
contentURL: require("self").data.url("widget-label.html"),
panel: thePanel,
});

self._panel = thePanel;
},


_updateDashboard: function(show) {
let self = this;
self._repo.list(function(apps) {
self._panel.port.emit("theList", apps);
});


if (show != undefined) {
let WM = Cc['@mozilla.org/appshell/window-mediator;1']
.getService(Ci.nsIWindowMediator);
let currentDoc = WM.getMostRecentWindow("navigator:browser").document;
var widgetAnchor = currentDoc.getElementById("widget:" +
require("self").id + "-openwebapps-toolbar-button");

console.log("showing panel");
self._panel.show(widgetAnchor, "topcenter bottomright");
}

},

_hideOffer: function() {
if (this._offerAppPanel) {
this._offerAppPanel.destroy();
Expand Down Expand Up @@ -407,3 +436,4 @@ openwebappsUI.prototype = {
};

exports.openwebappsUI = openwebappsUI;
exports.dashboard = dashboard;

0 comments on commit 328f4d6

Please sign in to comment.