add back the major items missing from the old overlay.js

This commit is contained in:
Shane Caraveo 2011-03-15 18:07:29 -07:00
Родитель c4a2f9c94e
Коммит b0a8782bd3
4 изменённых файлов: 282 добавлений и 35 удалений

2
extensions/firefox-share/src/bootstrap.js поставляемый
Просмотреть файл

@ -46,7 +46,7 @@ function loadIntoWindow(win) {
try {
dump("install addon\n");
unloaders = installOverlay(win);
startAddon(win);
unloaders.push.apply(startAddon(win));
} catch(e) {
dump("load error "+e+"\n");
}

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

@ -15,6 +15,12 @@ var EXPORTED_SYMBOLS = ["startAddon"];
function startAddon(win) {
win.gBrowser.f1 = new f1(win);
let unloaders = [];
unloaders.push(function () {
win.gBrowser.f1.unload();
win.gBrowser.f1 = null;
});
return unloaders;
}
function log(msg) {
@ -27,6 +33,95 @@ function error(msg) {
Cu.reportError('.' + msg); // avoid clearing on empty log
}
function sendJustInstalledEvent(win, url) {
var buttonNode = win.document.getElementById(buttonId);
//Button may not be there if customized and removed from toolbar.
if (buttonNode) {
var tab = win.gBrowser.loadOneTab(url, { referrerURI: null,
charset: null,
postData: null,
inBackground: false,
allowThirdPartyFixup: null });
// select here and there in case the load was quick
win.gBrowser.selectedTab = tab;
tab.addEventListener("load", function tabevent() {
tab.removeEventListener("load", tabevent, true);
win.gBrowser.selectedTab = tab;
}, true);
buttonNode.setAttribute("firstRun", "true");
}
}
function makeInstalledLoadHandler(win, url) {
var handler = function () {
win.removeEventListener("load", handler, true);
sendJustInstalledEvent(win, url);
};
return handler;
}
//Taken from https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
function openAndReuseOneTabPerURL(url) {
var browserEnumerator = Services.wm.getEnumerator("navigator:browser"),
rect, browser, buttonNode;
// Check each browser instance for our URL
var found = false;
try {
while (!found && browserEnumerator.hasMoreElements()) {
var browserWin = browserEnumerator.getNext();
var tabbrowser = browserWin.gBrowser;
// Sometimes we don't get a tabbrowser element
if (tabbrowser) {
// Check each tab of this browser instance
var numTabs = tabbrowser.browsers.length;
for (var index = 0; index < numTabs; index++) {
var currentBrowser = tabbrowser.getBrowserAtIndex(index);
if (currentBrowser.currentURI &&
url === currentBrowser.currentURI.spec) {
// The URL is already opened. Select this tab.
tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
// Focus *this* browser-window
browserWin.focus();
buttonNode = browserWin.document.getElementById(buttonId);
//Button may not be there if customized and removed from toolbar.
if (buttonNode) {
buttonNode.setAttribute("firstRun", "true");
}
found = true;
break;
}
}
}
}
// Minefield likes to error out in this loop sometimes
} catch (ignore) { }
// Our URL isn't open. Open it now.
if (!found) {
var recentWindow = Services.wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
// If our window is opened and ready just open the tab
// possible values: (loading, complete, or uninitialized)
if (recentWindow.document.readyState === "complete") {
sendJustInstalledEvent(recentWindow, url);
} else {
// Otherwise the window, while existing, might not be ready yet so we wait to open our tab
recentWindow.addEventListener("load", makeInstalledLoadHandler(recentWindow, url), true);
}
}
else {
// No browser windows are open, so open a new one.
this.window.open(url);
}
}
}
function f1(win)
{
this.window = win;
@ -45,6 +140,9 @@ function f1(win)
checkWindow();
}
f1.prototype = {
keycodeId: "key_ffshare",
keycode : "VK_F1",
oldKeycodeId: "key_old_ffshare",
togglePanel: function(options) {
let popup = this.window.document.getElementById('share-popup');
@ -155,20 +253,53 @@ f1.prototype = {
// an upgrade vs. fresh install.
firstRun: Application.prefs.getValue(
"extensions." + FFSHARE_EXT_ID + ".first-install",
""
true
),
use_accel_key: Application.prefs.getValue(
"extensions." + FFSHARE_EXT_ID + ".use_accel_key",
true
)
};
};
// dev and staging settings are based on the system pref
if (this.prefs.system === 'dev') {
this.prefs.share_url = 'http://linkdrop.caraveo.com:5000/share/panel/';
} else if (this.prefs.system === 'staging') {
this.prefs.share_url = 'https://f1-staging.mozillamessaging.com/share/panel/';
}
if (this.prefs.system === 'dev') {
this.prefs.frontpage_url = 'http://linkdrop.caraveo.com:5000/';
} else if (this.prefs.system === 'staging') {
this.prefs.frontpage_url = 'http://f1-staging.mozillamessaging.com/';
}
let self = this;
AddonManager.getAddonByID(FFSHARE_EXT_ID, function (addon) {
self.onInstallUpgrade(addon.version);
});
try {
this.canShareProgressListener = new LocationChangeProgressListener(this);
this.window.gBrowser.addProgressListener(this.canShareProgressListener);
} catch (e) {
error(e);
}
this.onContextMenuItemShowing = function(e) {
self._onContextMenuItemShowing(e);
}
this.window.document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", this.onContextMenuItemShowing, false);
this.initKeyCode();
Services.prefs.addObserver("extensions." + FFSHARE_EXT_ID + ".", this, false);
//Events triggered by TabView (panorama)
this.tabViewShowListener = function() { self.onTabViewShow() };
this.tabViewHideListener = function() { self.onTabViewHide() };
this.window.addEventListener('tabviewshow', this.tabViewShowListener, false);
this.window.addEventListener('tabviewhide', this.tabViewHideListener, false);
},
unload: function() {
@ -177,6 +308,150 @@ f1.prototype = {
} catch (e) {
error(e);
}
this.window.document.getElementById("contentAreaContextMenu").removeEventListener("popupshowing", this.onContextMenuItemShowing, false);
//Events triggered by TabView (panorama)
this.window.removeEventListener('tabviewshow', this.tabViewShowListener, false);
this.window.removeEventListener('tabviewhide', this.tabViewHideListener, false);
this.tabViewShowListener = null;
this.tabViewHideListener = null;
},
onInstallUpgrade: function (version) {
this.version = version;
//Only run if the versions do not match.
if (version === this.prefs.previous_version) {
return;
}
let Application = Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);
this.prefs.previous_version = version;
Application.prefs.setValue("extensions." + FFSHARE_EXT_ID + ".previous_version", version);
if (this.prefs.firstRun) {
//Make sure to set the pref first to avoid bad things if later code
//throws and we cannot set the pref.
this.prefs.firstRun = false;
Application.prefs.setValue("extensions." + FFSHARE_EXT_ID + ".first-install", false);
openAndReuseOneTabPerURL(this.prefs.frontpage_url);
}
},
// This function is to be run once at onLoad
// Checks for the existence of key code already and saves or gives it an ID for later
// We could get away without this check but we're being nice to existing key commands
initKeyCode: function () {
var keys = this.window.document.getElementsByTagName("key");
for (var i = 0; i < keys.length; i++) {
// has the keycode we want to take and isn't already ours
if (this.keycode === keys[i].getAttribute("keycode") &&
this.keycodeId !== keys[i].id) {
if (keys[i].id) {
this.oldKeycodeId = keys[i].id;
}
else {
keys[i].id = this.oldKeycodeId;
}
break;
}
}
this.setAccelKey(this.prefs.use_accel_key);
},
_onContextMenuItemShowing: function (e) {
try {
let contextMenu = this.window.gContextMenu;
let document = this.window.document;
let hide = (contextMenu.onTextInput || contextMenu.onLink ||
contextMenu.onImage || contextMenu.isContentSelected ||
contextMenu.onCanvas || contextMenu.onVideo ||
contextMenu.onAudio);
let hideSelected = (contextMenu.onTextInput || contextMenu.onLink ||
!contextMenu.isContentSelected ||
contextMenu.onImage || contextMenu.onCanvas ||
contextMenu.onVideo || contextMenu.onAudio);
document.getElementById("context-ffshare").hidden = hide;
document.getElementById("context-ffshare-separator").hidden = hide;
document.getElementById("context-selected-ffshare").hidden = hideSelected;
document.getElementById("context-selected-ffshare-separator").hidden = hideSelected;
} catch (e) { }
},
observe: function (subject, topic, data) {
if (topic !== "nsPref:changed") {
return;
}
let pref = subject.QueryInterface(Ci.nsIPrefBranch);
//dump("topic: " + topic + " -- data: " + data + " == pref: " + pref.getBoolPref(data) + "\n");
if ("extensions." + FFSHARE_EXT_ID + ".use_accel_key" === data) {
try {
this.setAccelKey(pref.getBoolPref(data));
} catch (e) {
error(e);
}
} else if ("extensions." + FFSHARE_EXT_ID + ".bookmarking" === data) {
try {
this.prefs.bookmarking = pref.getBoolPref(data);
} catch (e) {
error(e);
}
}
},
setAccelKey: function (keyOn) {
let document = this.window.document,
oldKey = document.getElementById(this.oldKeycodeId),
f1Key = document.getElementById(this.keycodeId),
keyset = document.getElementById("mainKeyset"),
p;
if (keyOn) {
try {
if (oldKey) {
oldKey.setAttribute("keycode", "");
}
f1Key.setAttribute("keycode", this.keycode);
} catch (e) {
error(e);
}
} else {
try {
f1Key.setAttribute("keycode", "");
if (oldKey) {
oldKey.setAttribute("keycode", this.keycode);
}
} catch (e) {
error(e);
}
}
// now we invalidate the keyset cache so our changes take effect
p = keyset.parentNode;
p.appendChild(p.removeChild(keyset));
},
onTabViewShow: function (event) {
// Triggered by TabView (panorama). Always hide it if being shown.
if (this.window.document.getElementById('share-popup').state === 'open') {
this.sharePanel.hide();
}
},
onTabViewHide: function (event) {
// Triggered by TabView (panorama). Restore share panel if needed.
// Hmm this never seems to be called? browser-tabview.js shows
// creation of a 'tabviewhide' event, but this function does
// not seem to be called.
this.switchTab();
}
};

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

@ -26,6 +26,7 @@ Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/PlacesUtils.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const FFSHARE_EXT_ID = "ffshare@mozilla.org";
const SHARE_STATUS = ["", "start", "", "finished"];
const SHARE_DONE = 0;
const SHARE_START = 1;
@ -159,6 +160,7 @@ sharePanel.prototype = {
// Fired when a pref changes from content space. the pref object has
// a name and value.
prefChanged: function (pref) {
let Application = Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);
Application.prefs.setValue("extensions." + FFSHARE_EXT_ID + "." + pref.name, pref.value);
},

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

@ -83,35 +83,5 @@ LocationChangeProgressListener.prototype = {
onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) {}
};
function FirstRunProgressListener(f1) {
this.f1 = f1;
}
FirstRunProgressListener.prototype = {
QueryInterface: XPCOMUtils.generateQI(
[Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference, Ci.nsISupports]
),
onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus) {
// maybe can just use onLocationChange, but I don't think so?
let flags = Ci.nsIWebProgressListener;
// This seems like an excessive check but works very well
if (aStateFlags & flags.STATE_IS_WINDOW && aStateFlags & flags.STATE_STOP) {
if (!this.f1.didOnFirstRun) {
//Be sure to disable first run after one try. Even if it does
//not work, do not want to annoy the user with continual popping up
//of the front page.
this.f1.didOnFirstRun = true;
this.f1.onFirstRun();
}
}
},
onLocationChange: function (aWebProgress, aRequest, aLocation) {},
onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {},
onSecurityChange: function (aWebProgress, aRequest, aState) {},
onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) {}
};
var EXPORTED_SYMBOLS = ["StateProgressListener",
"LocationChangeProgressListener",
"FirstRunProgressListener"];
"LocationChangeProgressListener"];