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

Commit

Permalink
Added Firefox Embedded WebExtension code.
Browse files Browse the repository at this point in the history
  • Loading branch information
uBlockAdmin committed May 31, 2018
1 parent abf116d commit ba96d73
Show file tree
Hide file tree
Showing 14 changed files with 590 additions and 34 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ env:
matrix:
- BROWSER=chromium EXT=zip
- BROWSER=firefox EXT=xpi
- BROWSER=embed-webext EXT=xpi
script: ./tools/make-${BROWSER}.sh all
deploy:
provider: releases
Expand Down
19 changes: 18 additions & 1 deletion platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ var manifest = chrome.runtime.getManifest();

vAPI.chrome = true;

vAPI.webextFlavor = '';
if (
self.browser instanceof Object &&
typeof self.browser.runtime.getBrowserInfo === 'function'
) {
self.browser.runtime.getBrowserInfo().then(function(info) {
vAPI.webextFlavor = info.vendor + '-' + info.name + '-' + info.version;
});
}

var noopFunc = function(){};

/******************************************************************************/
Expand Down Expand Up @@ -219,7 +229,9 @@ vAPI.tabs.registerListeners = function() {
onClosedClient(tabId);
};

chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
if ( chrome.webNavigation.onCreatedNavigationTarget instanceof Object ) {
chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
}
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate);
chrome.webNavigation.onCommitted.addListener(onCommitted);
chrome.tabs.onUpdated.addListener(onUpdated);
Expand Down Expand Up @@ -337,6 +349,11 @@ vAPI.tabs.open = function(details) {
wrapper();
return;
}

if ( /^Mozilla-Firefox-5[2-5]\./.test(vAPI.webextFlavor) ) {
wrapper();
return;
}

chrome.tabs.query({ url: targetURL }, function(tabs) {
var tab = tabs[0];
Expand Down
35 changes: 35 additions & 0 deletions platform/webext/background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>uBlock</title>
</head>
<body>
<script src="lib/punycode.js"></script>
<script src="lib/publicsuffixlist.js"></script>
<script src="lib/yamd5.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-background.js"></script>
<script src="js/background.js"></script>
<script src="js/async.js"></script>
<script src="js/utils.js"></script>
<script src="js/uritools.js"></script>
<script src="js/assets.js"></script>
<script src="js/dynamic-net-filtering.js"></script>
<script src="js/static-net-filtering.js"></script>
<script src="js/cosmetic-filtering.js"></script>
<script src="js/ublock.js"></script>
<script src="js/messaging.js"></script>
<script src="js/profiler.js"></script>
<script src="js/storage.js"></script>
<script src="js/logger.js"></script>
<script src="js/pagestore.js"></script>
<script src="js/tab.js"></script>
<script src="js/traffic.js"></script>
<script src="js/contextmenu.js"></script>
<script src="js/mirrors.js"></script>
<script src="js/from-legacy.js"></script>
<script src="js/stats.js"></script>
<script src="js/start.js"></script>
</body>
</html>
196 changes: 196 additions & 0 deletions platform/webext/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*******************************************************************************
µBlock - a browser extension to block requests.
Copyright (C) 2014 The µBlock authors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/uBlockAdmin/uBlock
*/

'use strict';

const hostName = 'ublock';
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);

function startup({ webExtension }) {
webExtension.startup().then(api => {
let { browser } = api,
dataMigrator;
let onMessage = function(message, sender, callback) {
if ( message.what === 'getNextMigrateItem' ) {
dataMigrator = dataMigrator || getDataMigrate();
dataMigrator.sendNextItemData((key, value) => {
if ( key === undefined ) {
dataMigrator.closeDbConn();
dataMigrator = undefined;
browser.runtime.onMessage.removeListener(onMessage);
}
callback({ key: key, value: JSON.stringify(value) });
});
return true;
}
if ( message.what === 'dataMigrateDone' ) {
browser.runtime.onMessage.removeListener(onMessage);
}
if ( typeof callback === 'function' ) {
callback();
}
};
browser.runtime.onMessage.addListener(onMessage);
});
}

function shutdown() {
}

function install() {
}

function uninstall() {
}

var SQLite = {

open: function() {

var path = Services.dirsvc.get('ProfD', Ci.nsIFile);
path.append('extension-data');
path.append(hostName + '.sqlite');
if ( !path.exists() || !path.isFile() ) {
return null;
}
this.db = Services.storage.openDatabase(path);
return this.db;
},

close: function() {
SQLite.db.asyncClose();
},

run: function(query, values, callback) {

if ( !this.db ) {
if ( this.open() === null ) {
callback({});
return;
}
}

var result = {};
query = this.db.createAsyncStatement(query);

if ( Array.isArray(values) && values.length ) {
var i = values.length;

while ( i-- ) {
query.bindByIndex(i, values[i]);
}
}

query.executeAsync({
handleResult: function(rows) {
if ( !rows || typeof callback !== 'function' ) {
return;
}

var row;

while ( row = rows.getNextRow() ) {
result[row.getResultByIndex(0)] = row.getResultByIndex(1);
}
},
handleCompletion: function(reason) {
if ( typeof callback === 'function' && reason === 0 ) {
callback(result);
}
},
handleError: function(error) {
if ( typeof callback === 'function' && reason === 0 ) {
callback();
}
result = null;
if ( error.result.toString() === '11' ) {
close();
}
}
});
}
};

var getDataMigrate = function() {

var legacyData = null;
var legacyDataKeys = null;

var fetchLegacyData = function(cb) {
var values = [];

var prepareResult = function(result) {

if ( result === undefined ) {
cb();
return;
}

var key;
for ( key in result ) {
result[key] = JSON.parse(result[key]);
}

legacyData = result;
legacyDataKeys = Object.keys(result);

var key = legacyDataKeys.pop();
var value = legacyData[key];
cb({ key: key, value: value });
};

SQLite.run(
'SELECT * FROM settings',
values,
prepareResult
);
};

var sendNextItemData = function(callback) {

if(!legacyData) {
fetchLegacyData( bin => {
callback(bin.key, bin.value);
});
return;
}
else {
var key = legacyDataKeys.pop();
var value = legacyData[key];
callback(key,value);
return;
}
};

var closeDbConn = function() {
SQLite.close();
};

return {
sendNextItemData: sendNextItemData,
closeDbConn: closeDbConn
};
}



/******************************************************************************/
1 change: 1 addition & 0 deletions platform/webext/chrome.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content ublock ./
62 changes: 62 additions & 0 deletions platform/webext/from-legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
µBlock - a browser extension to block requests.
Copyright (C) 2014 The µBlock authors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/uBlockAdmin/uBlock
*/

'use strict';

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

µBlock.migrateLegacyData = (function() {

let µb = µBlock;

let migrateLegacyData = function(callback) {

let storeKeyValue = function(details, callback) {
let bin = {};
bin[details.key] = JSON.parse(details.value);
vAPI.storage.set(bin, callback);
};

let migrateNextDataItem = function() {
self.browser.runtime.sendMessage({ what: 'getNextMigrateItem' }, response => {
if ( response.key === undefined ) {
return callback();
}
storeKeyValue(response, migrateNextDataItem);
});
};

self.browser.storage.local.get('dataMigrateDone', bin => {
if ( bin && bin.dataMigrateDone ) {
self.browser.runtime.sendMessage({ what: 'dataMigrateDone' });
return callback();
}
self.browser.storage.local.set({ dataMigrateDone: true });
migrateNextDataItem();
});
};

return migrateLegacyData;

})();


/******************************************************************************/
26 changes: 26 additions & 0 deletions platform/webext/install.rdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>{{2b10c1c8-a11f-4bad-fe9c-1c11e82cac42}}</em:id>
<em:version>{version}</em:version>
<em:name>{name}</em:name>
<em:description>{description}</em:description>
<em:homepageURL>{homepage}</em:homepageURL>
<em:creator>{author}</em:creator>
<em:type>2</em:type>
<em:bootstrap>true</em:bootstrap>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:hasEmbeddedWebExtension>true</em:hasEmbeddedWebExtension>
{localized}

<!-- Firefox -->
<em:targetApplication>
<Description>
<em:id>{{ec8030f7-c20a-464f-9b0e-13a3a9e97384}}</em:id>
<em:minVersion>52.0a1</em:minVersion>
<em:maxVersion>56.*</em:maxVersion>
</Description>
</em:targetApplication>

</Description>
</RDF>
Loading

0 comments on commit ba96d73

Please sign in to comment.