/* 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/. */


const { Windows } = require("./util/windows");


const system = require("sdk/system");
const extensionPrefs = require("sdk/simple-prefs");

if (37 < system.version) {
let domainUrlPattern = extensionPrefs.prefs['domainUrlPattern'];
console.log("Firefox 38 or newer detected, checking URL Patterns");
console.log("URL Patterns: "+domainUrlPattern);
if ("[\\\u0022*\\\u0022]" == domainUrlPattern) { 
console.warn("URL Patterns stored in old(cfx) format; correcting.");
let domainUrlPattern = "[\"*\"]"
console.log("URL Patterns (Corrected): "+domainUrlPattern);
extensionPrefs.prefs['domainUrlPattern'] = domainUrlPattern;
}
}

require("./patch/bug-78414");

exports.main = function(options, callbacks) {
    Windows.addEventListener("keydown", Windows.handleKeyPress);
};

exports.onUnload = function(reason) {
    Windows.removeEventListener("keydown", Windows.handleKeyPress);
};

/* 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/. */


const { Windows } = require("./util/windows");
const { windowMediator } = require("./util/functions");
const system = require("sdk/system");
const extensionPrefs = require("sdk/simple-prefs");

if (37 < system.version) {
let domainUrlPattern = extensionPrefs.prefs['domainUrlPattern'];
console.log("Firefox 38 or newer detected, checking URL Patterns");
console.log("URL Patterns: "+domainUrlPattern);
if ("[\\\u0022*\\\u0022]" == domainUrlPattern) { 
console.warn("URL Patterns stored in old(cfx) format; correcting.");
let domainUrlPattern = "[\"*\"]"
console.log("URL Patterns (Corrected): "+domainUrlPattern);
extensionPrefs.prefs['domainUrlPattern'] = domainUrlPattern;
}
}

require("./patch/bug-78414");

exports.main = function(options, callbacks) {
    Windows.addEventListener("keydown", Windows.handleKeyPress);
};

exports.onUnload = function(reason) {
    if (windowMediator.getMostRecentWindow("Keybinder:URLPatterns")) { windowMediator.getMostRecentWindow("Keybinder:URLPatterns").window.close(); }
    if (windowMediator.getMostRecentWindow("Keybinder:config")) { windowMediator.getMostRecentWindow("Keybinder:config").window.close(); }
    if (windowMediator.getMostRecentWindow("Keybinder:CustomXUL")) { windowMediator.getMostRecentWindow("Keybinder:CustomXUL").window.close(); }
    Windows.removeEventListener("keydown", Windows.handleKeyPress);
};

Fix bug when unloading/disabling add-on
Fix bug when unloading/disabling add-on

Bug reported by umage: Trying to unload/disable the add-on with one of its windows still open would freeze Firefox and render it unable to quit other than by killing the process. Now, we preemptively close all our windows on unload.
