-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
99 lines (85 loc) · 3.53 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* GPII Website Connector
*
* Copyright 2015 Emergya
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this license.
*
* The research leading to these results has received funding from the
* European Union's Seventh Framework Programme (FP7/2007-2013) under
* grant agreement no. 289016.
*
* You may obtain a copy of the license at
* https://github.com/javihernandez/gwc-chrome/blob/master/LICENSE.txt
*/
var socketServer = 'http://localhost:8081/browserChannel';
var allSettings = {};
function connectToGPII (port) {
// Here we will replace port.solutionId with port.sender.url
var solutionId = port.solutionId;
var socket = io.connect(socketServer, {'force new connection': true});
socket.on("connect", function (data) {
socket.send(solutionId);
});
socket.on("connectionSucceeded", function (settings) {
console.log("## on connectionSucceeded - got: " + JSON.stringify(settings));
allSettings[solutionId] = settings;
port.postMessage({settings: settings});
});
socket.on("onBrowserSettingsChanged", function (settings) {
console.log("onBrowserSettingsChanged: " + JSON.stringify(settings));
allSettings[solutionId] = settings;
port.postMessage({settings: settings});
});
socket.socket.on("disconnect", function (request) {
// We can tell the website what's going on
console.log("## on disconnect: " + request);
});
socket.on("error", function (err) {
// We can tell the website what's going on
console.log("## on error: " + err);
});
port.onDisconnect.addListener(function () {
console.log("## port has been closed - closing socket");
socket.disconnect();
});
};
// Taken from http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string
function extractDomain (url) {
var domain;
// Find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
} else {
domain = url.split('/')[0];
}
// Find & remove port number
domain = domain.split(':')[0];
return domain;
}
chrome.runtime.onConnectExternal.addListener(function (port) {
port.onMessage.addListener(function (msg) {
if (msg.type === "connectionRequest") {
// Right now, we only check that the solutionId coming from the website
// matches the domain. Also, we can ask to our privacy system whether this
// solution is allowed to be used within chrome according to the user's
// privacy settings.
console.log("## received connection request from: " + JSON.stringify(msg.solutionId));
// We'll check that the last two domain levels (i.e. example.com)
// are the same. Of course, this may change in the future
//var domain1 = msg.solutionId.split(".").reverse().slice(-2).join(".");
//var domain2 = extractDomain(port.sender.url).split(".").slice(-2).join(".");
//if (domain1 === domain2) {
if (true) {
port.solutionId = msg.solutionId;
connectToGPII(port);
port.postMessage({accepted: true});
console.log("## accepted connection request from: " + JSON.stringify(msg.solutionId));
} else {
port.postMessage({accepted: false});
console.log("## rejected connection request from: " + JSON.stringify(msg.solutionId));
}
};
});
});