-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_pluginWeb.js
96 lines (79 loc) · 2.28 KB
/
_pluginWeb.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
/**
* Contains the routines loaded by the plugin Worker under web-browser.
*
* Initializes the web environment version of the platform-dependent
* connection object for the plugin site
*/
self.application = {};
self.connection = {};
(function(){
/**
* Event lisener for the plugin message
*/
self.addEventListener('message', function(e){
var m = e.data.data;
switch (m.type) {
case 'import':
case 'importJailed': // already jailed in the Worker
importScript(m.url);
break;
case 'execute':
execute(m.code);
break;
case 'message':
conn._messageHandler(m.data);
break;
}
});
/**
* Loads and executes the JavaScript file with the given url
*
* @param {String} url to load
*/
var importScript = function(url) {
var error = null;
try {
importScripts(url);
} catch (e) {
error = e;
}
if (error) {
self.postMessage({type: 'importFailure', url: url});
throw error;
} else {
self.postMessage({type: 'importSuccess', url: url});
}
}
/**
* Executes the given code in a jailed environment. For web
* implementation, we're already jailed in the worker, so simply
* eval()
*
* @param {String} code code to execute
*/
var execute = function(code) {
try {
eval(code);
} catch (e) {
self.postMessage({type: 'executeFailure'});
throw e;
}
self.postMessage({type: 'executeSuccess'});
}
/**
* Connection object provided to the JailedSite constructor,
* plugin site implementation for the web-based environment.
* Global will be then cleared to prevent exposure into the
* Worker, so we put this local connection object into a closure
*/
var conn = {
disconnect: function(){ self.close(); },
send: function(data) {
self.postMessage({type: 'message', data: data});
},
onMessage: function(h){ conn._messageHandler = h; },
_messageHandler: function(){},
onDisconnect: function() {}
};
connection = conn;
})();