Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #956 from AlexVallat/master
Browse files Browse the repository at this point in the history
UI for Fennec (Firefox for Android)
  • Loading branch information
Deathamns committed Mar 11, 2015
2 parents 10f656f + 825adfa commit 415846e
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 68 deletions.
3 changes: 1 addition & 2 deletions platform/firefox/install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
<type>2</type>
<bootstrap>true</bootstrap>
<multiprocessCompatible>true</multiprocessCompatible>
<optionsType>3</optionsType>
<optionsURL>chrome://ublock/content/dashboard.html</optionsURL>
<optionsType>2</optionsType>
{localized}

<!-- Firefox -->
Expand Down
9 changes: 9 additions & 0 deletions platform/firefox/options.xul
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" ?>
<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<setting type="control">
<vbox>
<button id="showDashboardButton"/>
<button id="showNetworkLogButton"/>
</vbox>
</setting>
</vbox>
190 changes: 138 additions & 52 deletions platform/firefox/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,17 @@ var windowWatcher = {
if ( tabBrowser.deck ) {
// Fennec
tabContainer = tabBrowser.deck;
tabContainer.addEventListener(
'DOMTitleChanged',
tabWatcher.onFennecLocationChange
);
} 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?
},
Expand Down Expand Up @@ -353,28 +349,6 @@ var tabWatcher = {
url: location.asciiSpec
});
},

onFennecLocationChange: function({target: doc}) {
// Fennec "equivalent" to onLocationChange
// note that DOMTitleChanged is selected as it fires very early
// (before DOMContentLoaded), and it does fire even if there is no title

var win = doc.defaultView;
if ( win !== win.top ) {
return;
}

var loc = win.location;
/*if ( loc.protocol === 'http' || loc.protocol === 'https' ) {
return;
}*/

vAPI.tabs.onNavigation({
frameId: 0,
tabId: getOwnerWindow(win).BrowserApp.getTabForWindow(win).id,
url: Services.io.newURI(loc.href, null, null).asciiSpec
});
}
};

/******************************************************************************/
Expand Down Expand Up @@ -449,10 +423,6 @@ vAPI.tabs.registerListeners = function() {
if ( tabBrowser.deck ) {
// Fennec
tabContainer = tabBrowser.deck;
tabContainer.removeEventListener(
'DOMTitleChanged',
tabWatcher.onFennecLocationChange
);
} else if ( tabBrowser.tabContainer ) {
tabContainer = tabBrowser.tabContainer;
tabBrowser.removeTabsProgressListener(tabWatcher);
Expand Down Expand Up @@ -773,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);

Expand All @@ -785,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,
Expand Down Expand Up @@ -823,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);
};

/******************************************************************************/
Expand Down Expand Up @@ -1260,13 +1232,13 @@ var httpObserver = {
return;
}

/*if ( vAPI.fennec && lastRequest.type === this.MAIN_FRAME ) {
if ( vAPI.fennec && lastRequest.type === this.MAIN_FRAME && lastRequest.frameId === 0 ) {
vAPI.tabs.onNavigation({
frameId: 0,
tabId: lastRequest.tabId,
url: URI.asciiSpec
});
}*/
}

// If request is not handled we may use the data in on-modify-request
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
Expand Down Expand Up @@ -1412,8 +1384,67 @@ vAPI.toolbarButton = {
tabs: {/*tabId: {badge: 0, img: boolean}*/}
};

/******************************************************************************/
if (vAPI.fennec) {
// Menu UI
vAPI.toolbarButton.menuItemIds = new WeakMap();

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.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: "popup.html?tabId=" + curTabId, index: -1, select: true });
};
} else {
// Toolbar button UI
vAPI.toolbarButton.init = function() {
var CustomizableUI;
try {
Expand Down Expand Up @@ -1622,6 +1653,27 @@ vAPI.toolbarButton.onViewHiding = function({target}) {
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;
};
}

/******************************************************************************/

vAPI.toolbarButton.init();
Expand Down Expand Up @@ -1818,6 +1870,40 @@ vAPI.punycodeURL = function(url) {

/******************************************************************************/

vAPI.optionsObserver = {
register: function () {
var obs = Components.classes['@mozilla.org/observer-service;1'].getService(Components.interfaces.nsIObserverService);
obs.addObserver(this, "addon-options-displayed", false);

cleanupTasks.push(this.unregister.bind(this));
},

observe: function (aSubject, aTopic, aData) {
if (aTopic === "addon-options-displayed" && aData === "{2b10c1c8-a11f-4bad-fe9c-1c11e82cac42}") {
var doc = aSubject;
this.setupOptionsButton(doc, "showDashboardButton", "dashboard.html");
this.setupOptionsButton(doc, "showNetworkLogButton", "devtools.html");
}
},
setupOptionsButton: function (doc, id, page) {
var button = doc.getElementById(id);
button.addEventListener("command", function () {
vAPI.tabs.open({ url: page, index: -1 });
});
button.label = vAPI.i18n(id);
},

unregister: function () {
var obs = Components.classes['@mozilla.org/observer-service;1'].getService(Components.interfaces.nsIObserverService);
obs.removeObserver(this, "addon-options-displayed");
},
};

vAPI.optionsObserver.register();

/******************************************************************************/


// clean up when the extension is disabled

window.addEventListener('unload', function() {
Expand Down
12 changes: 12 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,18 @@
"message":"{{value}} days ago",
"description":"English: {{value}} days ago"
},
"showDashboardButton":{
"message":"Show Dashboard",
"description":"English: Show Dashboard"
},
"showNetworkLogButton":{
"message":"Show Network Request Log",
"description":"English: Show Network Request Log"
},
"fennecMenuItemBlockingOff": {
"message": "off",
"description": "Appears as µBlock (off)"
},
"dummy":{
"message":"This entry must be the last one",
"description":"so we dont need to deal with comma for last entry"
Expand Down
2 changes: 2 additions & 0 deletions src/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">

<title data-i18n="dashboardName"></title>
<link href="css/dashboard.css" rel="stylesheet" type="text/css">
<link href="css/common.css" rel="stylesheet" type="text/css">
Expand Down
2 changes: 2 additions & 0 deletions src/devtools.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">

<title data-i18n="statsPageName"></title>
<link rel="stylesheet" type="text/css" href="css/common.css">
<link rel="stylesheet" type="text/css" href="css/devtools.css">
Expand Down
15 changes: 11 additions & 4 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ var onMessage = function(request, sender, callback) {
case 'reloadTab':
if ( vAPI.isNoTabId(request.tabId) === false ) {
vAPI.tabs.reload(request.tabId);
if (request.select && vAPI.tabs.select) {
vAPI.tabs.select(request.tabId);
}
}
break;

Expand Down Expand Up @@ -195,7 +198,7 @@ var getFirewallRules = function(srcHostname, desHostnames) {

/******************************************************************************/

var getStats = function(tabId) {
var getStats = function(tabId, tabTitle) {
var r = {
advancedUserEnabled: µb.userSettings.advancedUserEnabled,
appName: vAPI.app.name,
Expand All @@ -209,7 +212,8 @@ var getStats = function(tabId) {
pageURL: '',
pageAllowedRequestCount: 0,
pageBlockedRequestCount: 0,
tabId: tabId
tabId: tabId,
tabTitle: tabTitle
};
var pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore ) {
Expand Down Expand Up @@ -277,8 +281,8 @@ var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
case 'getPopupData':
vAPI.tabs.get(null, function(tab) {
callback(getStats(getTargetTabId(tab)));
vAPI.tabs.get(request.tabId, function(tab) {
callback(getStats(getTargetTabId(tab), tab.title));
});
return;

Expand All @@ -296,6 +300,9 @@ var onMessage = function(request, sender, callback) {
µb.contextMenuClientX = -1;
µb.contextMenuClientY = -1;
µb.elementPickerExec(request.tabId);
if (request.select && vAPI.tabs.select) {
vAPI.tabs.select(request.tabId);
}
break;

case 'hasPopupContentChanged':
Expand Down
Loading

0 comments on commit 415846e

Please sign in to comment.