Skip to content
Snippets Groups Projects
main.js 3.04 KiB
Newer Older
Thomas Rientjes's avatar
Thomas Rientjes committed
/**
 * Entry Point
 * Belongs to Decentraleyes.
 *
 * @author      Thomas Rientjes
 * @since       2014-05-30
 * @license     MPL 2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

'use strict';

/**
 * Imports
 */

var preferences = require('sdk/simple-prefs');
var webextension = null;
Thomas Rientjes's avatar
Thomas Rientjes committed
var self = require('sdk/self');
var tabs = require("sdk/tabs");
Thomas Rientjes's avatar
Thomas Rientjes committed

var Interceptor = require('./interceptor');
var LoadWatcher = require('./load-watcher');

Thomas Rientjes's avatar
Thomas Rientjes committed
/**
 * Variables
 */

var webextensionPort = null;

/**
 * Initializations
Thomas Rientjes's avatar
Thomas Rientjes committed
 */

var interceptor = new Interceptor();
var loadWatcher = new LoadWatcher();
Thomas Rientjes's avatar
Thomas Rientjes committed
var featurelessVersions = {

    '1.3.7': true
};

// Executed as soon as the add-on is loaded.
exports.main = function (options) {

    // Initialize add-on state.
    interceptor.register();
    loadWatcher.register();

    // Display the release notes if desired.
    if (preferences.prefs.showReleaseNotes) {

        if (options.loadReason === 'install' || (options.loadReason === 'upgrade' && !featurelessVersions[self.version])) {

            if (preferences.prefs['sdk.baseURI']) {
                tabs.open(preferences.prefs['sdk.baseURI'] + 'static/release-notes.html');
            }
        }
    }

    try {
        webextension = require('sdk/webextension');
    } catch (exception) {
        return;
    }

    // Initialize the embedded WebExtension.
    webextension.startup().then(({ browser }) => {

        browser.runtime.onConnect.addListener(port => {

            if (port.name === 'webextension') {

                webextensionPort = port;

                preferences.on('', function (preferenceName) {

                    if (preferenceName === 'amountInjected') {
                        return;
                    }

                    port.postMessage({
                        'subject': 'update-preferences',
                        'content': {
                            [preferenceName]: preferences.prefs[preferenceName]
                        }
                    });
                });

                port.postMessage({
                    'subject': 'migrate-preferences',
                    'content': {
                        'amountInjected': preferences.prefs['amountInjected'],
                        'blockMissing': preferences.prefs['blockMissing'],
                        'domainWhitelist': preferences.prefs['domainWhitelist'],
                        'showReleaseNotes': preferences.prefs['showReleaseNotes']
};

// Executed as soon as the add-on is unloaded.
exports.onUnload = function () {

    // Clean up add-on state.
Thomas Rientjes's avatar
Thomas Rientjes committed
    interceptor.unregister();

// Sends injection updates to the WebExtension.
exports.broadcastInjection = function () {

    if (webextensionPort !== null) {

        webextensionPort.postMessage({
            'subject': 'register-injection'
        });
    }
};