<commit_before>/* Chrome token signing extension
 *
 * This software is released under the GNU Library General Public License.
 * See LICENSE.LGPL
 *
 * Note that the only valid version of the LGPL license as far as this
 * project is concerned is the original GNU Library General Public License
 * Version 2.1, February 1999
 */
var NO_NATIVE_URL = "https://pubkey.ee/sign/no_native_installed.html";
var HELLO_URL = "https://pubkey.ee/sign/hello.html";
var NATIVE_HOST = "ee.ria.esteid";
var K_SRC = "src";
var K_ORIGIN = "origin";
var K_NONCE = "nonce";
var K_RESULT = "result";
var K_TAB = "tab";

// Store the nonce/tabID pairs
// Used for routing messages back to the right tab
var sessions = {};

// Stores the longrunning ports per origin
// Used to route all request from an origin to the same host instance
var ports = {};

console.log("Background page activated");
// When extension is loaded
chrome.runtime.onSuspend.addListener(function() {
	console.log("Suspending");
});
// When extension is loaded
chrome.runtime.onConnect.addListener(function(port) {
	console.log("Connection from " + port.name);
});

chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
	console.log("Closing tab " + tabId);
	for (var k in ports) {
		// FIXME: ===
		if (k == tabId) {
			console.log("Our tab! Closing port");
			ports[k].disconnect();
			delete ports[k];
		}
	}
});
// When extension is installed
chrome.runtime.onInstalled.addListener(function(details) {
	// Check if native is available or direct to help page.
	if (details.reason === "install" || details.reason === "update") {
		// Check during every install or upgrade if we have the native components at hand.
		// Probe with empty message and expect a "result: invalid_argument" response
		chrome.runtime.sendNativeMessage(NATIVE_HOST, {}, function(response) {
			console.log("Query for native component");
			if (!response) {
				console.log("ERROR: " + JSON.stringify(chrome.runtime.lastError));
				chrome.tabs.create({'url': NO_NATIVE_URL + "?" + details.reason});
			} else {
				console.log("RECV: " + JSON.stringify(response));
				// TODO: Check for native component version and require upgrade if too old
				if (details.reason == "install" && response.result == "invalid_argument") {
					chrome.tabs.create({'url': HELLO_URL + "?" + details.reason});
				}
			}
		});
	}
});
// When message is received from page
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
	console.log("Marshal from url " + sender.url + " and extension id  " + sender.id);<commit_msg>Have a handy tip page for those who load the extension in developer mode without correct key<commit_after>/* Chrome token signing extension
 *
 * This software is released under the GNU Library General Public License.
 * See LICENSE.LGPL
 *
 * Note that the only valid version of the LGPL license as far as this
 * project is concerned is the original GNU Library General Public License
 * Version 2.1, February 1999
 */
var NO_NATIVE_URL = "https://pubkey.ee/sign/no_native_installed.html";
var HELLO_URL = "https://pubkey.ee/sign/hello.html";
var DEVELOPER_URL = "https://github.com/open-eid/chrome-token-signing/wiki/DeveloperTips";

var NATIVE_HOST = "ee.ria.esteid";

var K_SRC = "src";
var K_ORIGIN = "origin";
var K_NONCE = "nonce";
var K_RESULT = "result";
var K_TAB = "tab";

// Store the nonce/tabID pairs
// Used for routing messages back to the right tab
var sessions = {};

// Stores the longrunning ports per origin
// Used to route all request from an origin to the same host instance
var ports = {};

console.log("Background page activated");
// When extension is loaded
chrome.runtime.onSuspend.addListener(function() {
	console.log("Suspending");
});
// When extension is loaded
chrome.runtime.onConnect.addListener(function(port) {
	console.log("Connection from " + port.name);
});

chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
	console.log("Closing tab " + tabId);
	for (var k in ports) {
		// FIXME: ===
		if (k == tabId) {
			console.log("Our tab! Closing port");
			ports[k].disconnect();
			delete ports[k];
		}
	}
});
// When extension is installed
chrome.runtime.onInstalled.addListener(function(details) {
	// Check if native is available or direct to help page.
	if (details.reason === "install" || details.reason === "update") {
		// Check during every install or upgrade if we have the native components at hand.
		// Probe with empty message and expect a "result: invalid_argument" response
		chrome.runtime.sendNativeMessage(NATIVE_HOST, {}, function(response) {
			console.log("Query for native component");
			if (!response) {
				console.log("ERROR: " + JSON.stringify(chrome.runtime.lastError));
				var permissions = "Access to the specified native messaging host is forbidden.";
				if (chrome.runtime.lastError.message === permissions) {
					chrome.tabs.create({'url': DEVELOPER_URL + "?" + details.reason});
				} else {
					chrome.tabs.create({'url': NO_NATIVE_URL + "?" + details.reason});
				}
			} else {
				console.log("RECV: " + JSON.stringify(response));
				// TODO: Check for native component version and require upgrade if too old
				if (details.reason == "install" && response.result == "invalid_argument") {
					chrome.tabs.create({'url': HELLO_URL + "?" + details.reason});
				}
			}
		});
	}
});
// When message is received from page
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
	console.log("Marshal from url " + sender.url + " and extension id  " + sender.id);
{'fraction_non_alphanumeric': 0.13241926754465091, 'fraction_numerical': 0.002164892657405737, 'mean_word_length': 4.0537830446672745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4084751', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1641, 'n_words': 520}