]> git.wh0rd.org - chrome-ext/crftp.git/blob - pnacl/crftp.js
init
[chrome-ext/crftp.git] / pnacl / crftp.js
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
2
3 function $(s) { return document.querySelector(s); }
4
5 var crftp = (function() {
6
7 var nacl_module = null;
8
9 var mimeType = 'application/x-pnacl';
10
11 // Generate the NaCl module. We do this on the fly so we can attach to events
12 // in the parent element in case loading the NaCl module crashed.
13 function create(nmf)
14 {
15 // Attach listeners early.
16 var listener = document.getElementById('listener');
17 listener.addEventListener('load', moduleDidLoad, true);
18 listener.addEventListener('message', handleMessage, true);
19 listener.addEventListener('error', handleError, true);
20 listener.addEventListener('crash', handleCrash, true);
21
22 // Create the NaCl module.
23 var embed = nacl_module = document.createElement('embed');
24 embed.name = embed.id = 'nacl_module';
25 // Don't hide it in case there is a plugin/load error.
26 embed.width = embed.height = 200;
27 embed.src = nmf;
28 embed.type = mimeType;
29
30 // Attach it to the DOM so it'll start running.
31 listener.appendChild(embed);
32
33 crftp.module = nacl_module;
34 }
35
36 function handleError(event)
37 {
38 set_status('ERROR [' + nacl_module.lastError + ']');
39 }
40
41 function handleCrash(event)
42 {
43 if (nacl_module.exitStatus == -1)
44 set_status('CRASHED');
45 else
46 set_status('EXITED [' + nacl_module.exitStatus + ']');
47 set_ui(false);
48 }
49
50 function moduleDidLoad()
51 {
52 set_status('RUNNING');
53 nacl_module.style.height = '0';
54
55 set_ui(true);
56 $('#connect').focus();
57 }
58
59 // Callback from the NaCl module (PPB_Messaging.PostMessage).
60 var levels = Array('status', 'response', 'cmd', 'error', 'release');
61 function handleMessage(message_event)
62 {
63 if (typeof message_event.data == "string") {
64 log(message_event.data, levels[0]);
65 } else {
66 var event = message_event.data[0];
67 var data = message_event.data[1];
68 if (event == 4)
69 URL.revokeObjectURL(data);
70 else
71 log(data, levels[event]);
72 }
73 }
74
75 // Unload the NaCl module.
76 function destroy()
77 {
78 nacl_module.parentNode.removeChild(nacl_module);
79 crftp.module = nacl_module = null;
80 }
81
82 var log_ele = undefined;
83 function log(message, opt_class)
84 {
85 var span = document.createElement('span');
86 span.className = opt_class || 'status';
87 if (message.substr(-1) != '\n')
88 message += '\n';
89 span.textContent = message;
90 log_ele.appendChild(span);
91 log_ele.scrollTop = log_ele.scrollHeight;
92 }
93
94 var status_ele = undefined;
95 function set_status(status)
96 {
97 status_ele.innerHTML = status;
98 }
99
100 function initialize(nmf)
101 {
102 set_ui(false);
103
104 log_ele = document.getElementById('log');
105 status_ele = document.getElementById('status');
106
107 set_status('Page loaded');
108
109 // See if NaCl is even supported. Makes debugging easier.
110 if (navigator.mimeTypes[mimeType] === undefined) {
111 set_status('Browser does not support ' + mimeType + ' or is disabled');
112
113 // We're good, so try loading the module if needed.
114 } else if (nacl_module == null) {
115 set_status('Loading NaCl module');
116 create(nmf);
117 } else {
118 set_status('Waiting');
119 }
120 }
121
122 function postMessage()
123 {
124 var args = Array.prototype.slice.call(arguments);
125 nacl_module.postMessage(args);
126 }
127
128 function connect(host, opt_port, opt_user, opt_pass)
129 {
130 if (opt_port === undefined) {
131 if (host.indexOf(':') == -1)
132 host += ':21';
133 } else
134 host += ':' + port
135
136 postMessage('connect', host);
137
138 if (opt_user !== undefined)
139 login(opt_user, opt_pass);
140 }
141
142 function login(user, opt_pass)
143 {
144 var pass = opt_pass || 'luser@crftp';
145 postMessage('login', user, pass);
146 }
147
148 function disconnect()
149 {
150 postMessage('disconnect');
151 }
152
153 function chdir(path)
154 {
155 postMessage('chdir', path);
156 }
157 function dir(path)
158 {
159 postMessage('dir', path);
160 }
161 function list(path)
162 {
163 postMessage('list', path);
164 }
165 function put(files)
166 {
167 files.forEach(function(file) {
168 var url = file[0], name = file[1];
169 postMessage('put', url, name);
170 });
171 }
172 function get(files)
173 {
174 files.forEach(function(file) {
175 var url = file[0], name = file[1];
176 postMessage('get', url, name);
177 });
178 }
179
180 return {
181 module: null,
182
183 create: create,
184 destroy: destroy,
185 initialize: initialize,
186
187 connect: connect,
188 disconnect: disconnect,
189 login: login,
190 chdir: chdir,
191 dir: dir,
192 list: list,
193 put: put,
194 get: get,
195 raw: postMessage,
196 };
197
198 }());