AlexVallat 10 years ago
committed by gorhill
parent
commit
c9a8c8d6df
  1. 369
      platform/firefox/vapi-background.js

369
platform/firefox/vapi-background.js

@ -269,17 +269,20 @@ var windowWatcher = {
return;
}
if ( tabBrowser.tabContainer ) {
if ( tabBrowser.deck ) {
// Fennec
tabContainer = tabBrowser.deck;
} else if ( tabBrowser.tabContainer ) {
// desktop Firefox
tabContainer = tabBrowser.tabContainer;
tabBrowser.addTabsProgressListener(tabWatcher);
vAPI.contextMenu.register(this.document);
} else {
return;
}
tabContainer.addEventListener('TabClose', tabWatcher.onTabClose);
tabContainer.addEventListener('TabSelect', tabWatcher.onTabSelect);
vAPI.contextMenu.register(this.document);
// when new window is opened TabSelect doesn't run on the selected tab?
},
@ -417,7 +420,10 @@ vAPI.tabs.registerListeners = function() {
continue;
}
if ( tabBrowser.tabContainer ) {
if ( tabBrowser.deck ) {
// Fennec
tabContainer = tabBrowser.deck;
} else if ( tabBrowser.tabContainer ) {
tabContainer = tabBrowser.tabContainer;
tabBrowser.removeTabsProgressListener(tabWatcher);
}
@ -543,7 +549,7 @@ vAPI.tabs.get = function(tabId, callback) {
}
}
// for internal use
// For internal use
if ( typeof callback !== 'function' ) {
return tab;
}
@ -559,12 +565,14 @@ vAPI.tabs.get = function(tabId, callback) {
var browser = getBrowserForTab(tab);
var tabBrowser = getTabBrowser(win);
var tabIndex = vAPI.fennec
? tabBrowser.tabs.indexOf(tab)
: tabBrowser.browsers.indexOf(browser);
var tabTitle = vAPI.fennec
? browser.contentTitle
: tab.label;
var tabIndex, tabTitle;
if ( vAPI.fennec ) {
tabIndex = tabBrowser.tabs.indexOf(tab);
tabTitle = browser.contentTitle;
} else {
tabIndex = tabBrowser.browsers.indexOf(browser);
tabTitle = tab.label;
}
callback({
id: tabId,
@ -735,6 +743,24 @@ vAPI.tabs.reload = function(tabId) {
/******************************************************************************/
vAPI.tabs.select = function(tabId) {
var tab = this.get(tabId);
if ( !tab ) {
return;
}
var tabBrowser = getTabBrowser(getOwnerWindow(tab));
if (vAPI.fennec) {
tabBrowser.selectTab(tab);
} else {
tabBrowser.selectedTab = tab;
}
};
/******************************************************************************/
vAPI.tabs.injectScript = function(tabId, details, callback) {
var tab = this.get(tabId);
@ -747,7 +773,7 @@ vAPI.tabs.injectScript = function(tabId, details, callback) {
}
details.file = vAPI.getURL(details.file);
tab.linkedBrowser.messageManager.sendAsyncMessage(
getBrowserForTab(tab).messageManager.sendAsyncMessage(
location.host + ':broadcast',
JSON.stringify({
broadcast: true,
@ -785,23 +811,7 @@ vAPI.setIcon = function(tabId, iconStatus, badge) {
return;
}
var button = win.document.getElementById(tb.id);
if ( !button ) {
return;
}
var icon = tb.tabs[tabId];
button.setAttribute('badge', icon && icon.badge || '');
if ( !icon || !icon.img ) {
icon = '';
}
else {
icon = 'url(' + vAPI.getURL('img/browsericons/icon16.svg') + ')';
}
button.style.listStyleImage = icon;
tb.updateState(win, tabId);
};
/******************************************************************************/
@ -990,6 +1000,7 @@ var httpObserver = {
classDescription: 'net-channel-event-sinks for ' + location.host,
classID: Components.ID('{dc8d6319-5f6e-4438-999e-53722db99e84}'),
contractID: '@' + location.host + '/net-channel-event-sinks;1',
REQDATAKEY: location.host + 'reqdata',
ABORT: Components.results.NS_BINDING_ABORTED,
ACCEPT: Components.results.NS_SUCCEEDED,
// Request types: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIContentPolicy#Constants
@ -1008,14 +1019,7 @@ var httpObserver = {
12: 'object',
14: 'font'
},
lastRequest: {
url: null,
type: null,
tabId: null,
frameId: null,
parentFrameId: null,
openerURL: null
},
lastRequest: [{}, {}],
get componentRegistrar() {
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
@ -1086,9 +1090,9 @@ var httpObserver = {
}
var result = vAPI.tabs.onPopup({
targetTabId: tabId,
openerTabId: sourceTabId,
targetURL: URI.asciiSpec
tabId: tabId,
sourceTabId: sourceTabId,
url: URI.asciiSpec
});
return result === true;
@ -1120,13 +1124,13 @@ var httpObserver = {
return true;
}
if ( result.redirectUrl ) {
/*if ( result.redirectUrl ) {
channel.redirectionLimit = 1;
channel.redirectTo(
Services.io.newURI(result.redirectUrl, null, null)
);
return true;
}
}*/
return false;
},
@ -1136,9 +1140,6 @@ var httpObserver = {
return;
}
var reqDataKey = location.host + 'reqdata';
var URI = channel.URI;
var channelData, result;
@ -1148,14 +1149,7 @@ var httpObserver = {
}
try {
/*[
type,
tabId,
sourceTabId - given if it was a popup,
frameId,
parentFrameId
]*/
channelData = channel.getProperty(reqDataKey);
channelData = channel.getProperty(this.REQDATAKEY);
} catch (ex) {
return;
}
@ -1164,7 +1158,7 @@ var httpObserver = {
return;
}
if ( (1 << channelData[0] & this.VALID_CSP_TARGETS) === 0 ) {
if ( (1 << channelData[4] & this.VALID_CSP_TARGETS) === 0 ) {
return;
}
@ -1178,9 +1172,9 @@ var httpObserver = {
result = vAPI.net.onHeadersReceived.callback({
hostname: URI.asciiHost,
parentFrameId: channelData[4],
parentFrameId: channelData[1],
responseHeaders: result ? [{name: topic, value: result}] : [],
tabId: channelData[1],
tabId: channelData[3],
url: URI.asciiSpec
});
@ -1197,48 +1191,48 @@ var httpObserver = {
// http-on-opening-request
var lastRequest = this.lastRequest;
var lastRequest = this.lastRequest[0];
if ( lastRequest.url !== URI.spec ) {
if ( this.lastRequest[1].url === URI.spec ) {
lastRequest = this.lastRequest[1];
} else {
lastRequest.url = null;
}
}
if ( lastRequest.url === null ) {
this.handleRequest(channel, URI, {
lastRequest.type = channel.loadInfo && channel.loadInfo.contentPolicyType || 1;
result = this.handleRequest(channel, URI, {
tabId: vAPI.noTabId,
type: channel.loadInfo && channel.loadInfo.contentPolicyType || 1
type: lastRequest.type
});
if ( result === true ) {
return;
}
// Important! When loading file via XHR for mirroring,
// the URL will be the same, so it could fall into an infinite loop
lastRequest.url = null;
var sourceTabId = null;
// Popup candidate (only for main_frame type)
if ( lastRequest.openerURL ) {
for ( var tab of vAPI.tabs.getAll() ) {
var tabURI = tab.linkedBrowser.currentURI;
// Probably isn't the best method to identify the source tab
if ( tabURI.spec !== lastRequest.openerURL ) {
continue;
if ( channel instanceof Ci.nsIWritablePropertyBag === false ) {
return;
}
sourceTabId = vAPI.tabs.getTabId(tab);
if ( sourceTabId !== lastRequest.tabId ) {
break;
// Carry data for behind-the-scene redirects
channel.setProperty(
this.REQDATAKEY,
[lastRequest.type, vAPI.noTabId, null, 0, -1]
);
return;
}
sourceTabId = null;
}
// Important! When loading file via XHR for mirroring,
// the URL will be the same, so it could fall into an infinite loop
lastRequest.url = null;
if ( this.handlePopup(channel.URI, lastRequest.tabId, sourceTabId) ) {
channel.cancel(this.ABORT);
if ( this.handleRequest(channel, URI, lastRequest) ) {
return;
}
}
if ( lastRequest.type === this.MAIN_FRAME && lastRequest.frameId === 0 ) {
if ( vAPI.fennec && lastRequest.type === this.MAIN_FRAME && lastRequest.frameId === 0 ) {
vAPI.tabs.onNavigation({
frameId: 0,
tabId: lastRequest.tabId,
@ -1246,22 +1240,15 @@ var httpObserver = {
});
}
if ( this.handleRequest(channel, URI, lastRequest) ) {
return;
}
// If request is not handled we may use the data in on-modify-request
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
channel.setProperty(
reqDataKey,
[
lastRequest.type,
lastRequest.tabId,
sourceTabId,
channel.setProperty(this.REQDATAKEY, [
lastRequest.frameId,
lastRequest.parentFrameId
]
);
lastRequest.parentFrameId,
lastRequest.sourceTabId,
lastRequest.tabId,
lastRequest.type
]);
}
},
@ -1271,12 +1258,6 @@ var httpObserver = {
// If error thrown, the redirect will fail
try {
// skip internal redirects?
/*if ( flags & 4 ) {
console.log('internal redirect skipped');
return;
}*/
var URI = newChannel.URI;
if ( !URI.schemeIs('http') && !URI.schemeIs('https') ) {
@ -1287,21 +1268,18 @@ var httpObserver = {
return;
}
// TODO: what if a behind-the-scene request is being redirected?
// This data is present only for tabbed requests, so if this throws,
// the redirection won't be evaluated and canceled (if necessary)
var channelData = oldChannel.getProperty(location.host + 'reqdata');
var channelData = oldChannel.getProperty(this.REQDATAKEY);
if ( this.handlePopup(URI, channelData[1], channelData[2]) ) {
if ( this.handlePopup(URI, channelData[3], channelData[2]) ) {
result = this.ABORT;
return;
}
var details = {
type: channelData[0],
tabId: channelData[1],
frameId: channelData[3],
parentFrameId: channelData[4]
frameId: channelData[0],
parentFrameId: channelData[1],
tabId: channelData[3],
type: channelData[4]
};
if ( this.handleRequest(newChannel, URI, details) ) {
@ -1311,7 +1289,7 @@ var httpObserver = {
// Carry the data on in case of multiple redirects
if ( newChannel instanceof Ci.nsIWritablePropertyBag ) {
newChannel.setProperty(location.host + 'reqdata', channelData);
newChannel.setProperty(this.REQDATAKEY, channelData);
}
} catch (ex) {
// console.error(ex);
@ -1336,13 +1314,46 @@ vAPI.net.registerListeners = function() {
var shouldLoadListenerMessageName = location.host + ':shouldLoad';
var shouldLoadListener = function(e) {
var details = e.data;
var tabId = vAPI.tabs.getTabId(e.target);
var sourceTabId = null;
// Popup candidate
if ( details.openerURL ) {
for ( var tab of vAPI.tabs.getAll() ) {
var URI = tab.linkedBrowser.currentURI;
// Probably isn't the best method to identify the source tab
if ( URI.spec !== details.openerURL ) {
continue;
}
sourceTabId = vAPI.tabs.getTabId(tab);
if ( sourceTabId === tabId ) {
sourceTabId = null;
continue;
}
URI = Services.io.newURI(details.url, null, null);
if ( httpObserver.handlePopup(URI, tabId, sourceTabId) ) {
return;
}
break;
}
}
var lastRequest = httpObserver.lastRequest;
lastRequest.url = details.url;
lastRequest.type = details.type;
lastRequest.tabId = vAPI.tabs.getTabId(e.target);
lastRequest.frameId = details.frameId;
lastRequest.parentFrameId = details.parentFrameId;
lastRequest.openerURL = details.openerURL;
lastRequest[1] = lastRequest[0];
lastRequest[0] = {
frameId: details.frameId,
parentFrameId: details.parentFrameId,
sourceTabId: sourceTabId,
tabId: tabId,
type: details.type,
url: details.url
};
};
vAPI.messaging.globalMessageManager.addMessageListener(
@ -1373,9 +1384,77 @@ vAPI.toolbarButton = {
tabs: {/*tabId: {badge: 0, img: boolean}*/}
};
/******************************************************************************/
if (vAPI.fennec) {
// Menu UI
vAPI.toolbarButton.menuItemIds = new WeakMap();
vAPI.toolbarButton.init = function() {
vAPI.toolbarButton.getMenuItemLabel = function(tabId) {
var label = this.label;
if (tabId !== undefined) {
var tabDetails = this.tabs[tabId];
if (tabDetails) {
if (tabDetails.img) {
if (tabDetails.badge) {
label = label + " (" + tabDetails.badge + ")";
}
} else {
label = label + " (" + vAPI.i18n("fennecMenuItemBlockingOff") + ")";
}
}
}
return label;
}
vAPI.toolbarButton.init = function() {
// Only actually expecting one window under Fennec (note, not tabs, windows)
for (var win of vAPI.tabs.getWindows()) {
this.addToWindow(win, this.getMenuItemLabel());
}
cleanupTasks.push(this.cleanUp);
}
vAPI.toolbarButton.addToWindow = function(win, label) {
var id = win.NativeWindow.menu.add({
name: label,
callback: this.onClick
});
this.menuItemIds.set(win, id);
}
vAPI.toolbarButton.cleanUp = function() {
for (var win of vAPI.tabs.getWindows()) {
vAPI.toolbarButton.removeFromWindow(win);
}
}
vAPI.toolbarButton.removeFromWindow = function(win) {
var id = this.menuItemIds.get(win);
if (id) {
win.NativeWindow.menu.remove(id);
this.menuItemIds.delete(win);
}
}
vAPI.toolbarButton.updateState = function (win, tabId) {
var id = this.menuItemIds.get(win);
if (!id) {
return;
}
win.NativeWindow.menu.update(id, { name: this.getMenuItemLabel(tabId) });
}
vAPI.toolbarButton.onClick = function() {
var win = Services.wm.getMostRecentWindow('navigator:browser');
var curTabId = vAPI.tabs.getTabId(getTabBrowser(win).selectedTab);
vAPI.tabs.open({ url: vAPI.getURL("popup.html?tabId=" + curTabId), index: -1, select: true });
}
} else {
// Toolbar button UI
/******************************************************************************/
vAPI.toolbarButton.init = function() {
var CustomizableUI;
try {
CustomizableUI = Cu.import('resource:///modules/CustomizableUI.jsm', null).CustomizableUI;
@ -1416,7 +1495,7 @@ vAPI.toolbarButton.init = function() {
} else {
this.CUIEvents = {};
var updateBadge = function() {
this.CUIEvents.onCustomizeEnd = function() {
var wId = vAPI.toolbarButton.id;
var buttonInPanel = CustomizableUI.getWidget(wId).areaType === CustomizableUI.TYPE_MENU_PANEL;
@ -1437,12 +1516,9 @@ vAPI.toolbarButton.init = function() {
}
// Anonymous elements need some time to be reachable
setTimeout(this.updateBadgeStyle, 50);
setTimeout(this.updateBadgeStyle, 250);
}.bind(this.CUIEvents);
this.CUIEvents.onCustomizeEnd = updateBadge;
this.CUIEvents.onWidgetUnderflow = updateBadge;
this.CUIEvents.updateBadgeStyle = function() {
var css = [
'background: #666',
@ -1469,7 +1545,7 @@ vAPI.toolbarButton.init = function() {
this.onCreated = function(button) {
button.setAttribute('badge', '');
setTimeout(this.CUIEvents.onCustomizeEnd, 50);
setTimeout(this.CUIEvents.onCustomizeEnd, 250);
};
CustomizableUI.addListener(this.CUIEvents);
@ -1512,11 +1588,11 @@ vAPI.toolbarButton.init = function() {
.removeSheet(this.styleURI, 1);
}
}.bind(this));
};
};
/******************************************************************************/
/******************************************************************************/
vAPI.toolbarButton.onBeforeCreated = function(doc) {
vAPI.toolbarButton.onBeforeCreated = function(doc) {
var panel = doc.createElement('panelview');
panel.setAttribute('id', this.viewId);
@ -1568,20 +1644,41 @@ vAPI.toolbarButton.onBeforeCreated = function(doc) {
doc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.loadSheet(this.styleURI, 1);
};
};
/******************************************************************************/
/******************************************************************************/
vAPI.toolbarButton.onViewShowing = function({target}) {
vAPI.toolbarButton.onViewShowing = function({target}) {
target.firstChild.setAttribute('src', vAPI.getURL('popup.html'));
};
};
/******************************************************************************/
/******************************************************************************/
vAPI.toolbarButton.onViewHiding = function({target}) {
vAPI.toolbarButton.onViewHiding = function({target}) {
target.parentNode.style.maxWidth = '';
target.firstChild.setAttribute('src', 'about:blank');
};
};
vAPI.toolbarButton.updateState = function(win, tabId) {
var button = win.document.getElementById(this.id);
if ( !button ) {
return;
}
var icon = this.tabs[tabId];
button.setAttribute('badge', icon && icon.badge || '');
if ( !icon || !icon.img ) {
icon = '';
}
else {
icon = 'url(' + vAPI.getURL('img/browsericons/icon16.svg') + ')';
}
button.style.listStyleImage = icon;
}
}
/******************************************************************************/
@ -1716,7 +1813,7 @@ vAPI.contextMenu.create = function(details, callback) {
if ( gContextMenu.inFrame ) {
details.tagName = 'iframe';
// Probably won't work with e01s
// Probably won't work with e10s
details.frameUrl = gContextMenu.focusedWindow.location.href;
} else if ( gContextMenu.onImage ) {
details.tagName = 'img';

Loading…
Cancel
Save