-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathplugin.btapp.js
executable file
·364 lines (338 loc) · 14.7 KB
/
plugin.btapp.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// (c) 2012 Kyle Graehl / Patrick Williams, BitTorrent Inc.
// Btapp may be freely distributed under the MIT license.
// For all details and documentation:
// http://pwmckenna.github.com/btapp
(function() {
"use strict";
function assert(b, err) { if(!b) { throw err; } }
//validate dependencies
assert(typeof JSON !== 'undefined', 'JSON is a hard dependency');
assert(typeof _ !== 'undefined', 'underscore/lodash is a hard dependency');
assert(typeof jQuery !== 'undefined', 'jQuery is a hard dependency');
function isMac() {
var match = navigator.userAgent.match(/Macintosh/);
return match !== undefined && match !== null;
}
var add_plugin = _.memoize(function(mime_type) {
var ret = new jQuery.Deferred();
var obj = document.createElement('object');
var onload = mime_type + '_onload';
window[onload] = function() {
ret.resolve();
};
var div = document.createElement('div');
jQuery(div).css({'position':'absolute','left':'-999em','z-index':-1});
div.innerHTML =
'<object type="' + mime_type + '" width="0" height="0">' +
'<param name="onload" value="' + onload + '" />' +
'</object>';
document.body.appendChild(div);
return ret;
});
//utility function to wait for some condition
//this ends up being helpful as we toggle between a flow chart and a state diagram
function when(condition, functionality, interval) {
var when_func = function() {
if(condition.call()) {
functionality.call();
} else {
setTimeout(when_func, interval || 500);
}
};
_.defer(when_func);
}
function getCSS(url) {
jQuery(document.createElement('link') ).attr({
href: url,
type: 'text/css',
rel: 'stylesheet'
}).appendTo('head');
}
function initializeFacebox() {
jQuery.facebox.settings.overlay = true; // to disable click outside overlay to disable it
jQuery.facebox.settings.closeImage =
'https://torque.bittorrent.com/facebox/src/closelabel.png';
jQuery.facebox.settings.loadingImage =
'https://torque.bittorrent.com/facebox/src/loading.gif';
jQuery.facebox.settings.opacity = 0.6;
}
this.PluginManagerView = Backbone.View.extend({
initialize: function(options) {
this.model.on('plugin:install_plugin', this.download, this);
this.model.on('plugin:plugin_updated', this.restart, this);
},
setup: function(callback, context) {
//make sure that we've loaded what we need to display
if(typeof jQuery.facebox === 'undefined') {
getCSS('https://torque.bittorrent.com/facebox/src/facebox.css');
jQuery.getScript(
'https://torque.bittorrent.com/facebox/src/facebox.js',
_.bind(this.setup, this, callback, context)
);
return;
}
initializeFacebox();
callback.call(context);
},
restart: function(options) {
options.abort = true;
this.setup(function() {
var dialog = jQuery('<div></div>');
dialog.attr('id', 'plugin_download');
dialog.css('position', 'absolute');
dialog.css('height', '200px');
dialog.css('width', '400px');
dialog.css('left', '%50');
dialog.css('margin-left', '-200px');
var paragraph = jQuery('<p></p>');
paragraph.text('The ' + this.model.get('product') + ' plugin needs to complete an update. Please restart your browser.');
dialog.append(paragraph);
dialog.hide();
jQuery('body').append(dialog);
jQuery.facebox({ div: '#plugin_download' });
}, this);
},
download: function(options) {
options.install = true;
this.setup(function() {
var dialog = jQuery('<div></div>');
dialog.attr('id', 'plugin_download');
dialog.css('position', 'absolute');
dialog.css('height', '200px');
dialog.css('width', '400px');
dialog.css('left', '%50');
dialog.css('margin-left', '-200px');
var button_url = this.model.get('download_url');
var html ='<p style="text-align:center;">' +
'This site requires the ' + this.model.get('product') + ' plugin.<br>' +
'<span style="font-size:8pt;">By installing this software, you<br>are agreeing to the <a href="http://www.bittorrent.com/legal/eula">EULA</a></span><br><br>' +
'<a class="btn" id="download" href="' + button_url + '">Download</a>' + '</p>';
dialog.append(html);
this.model.on('plugin:plugin_installed', function() {
jQuery(document).trigger('close.facebox');
jQuery('#plugin_download').remove();
});
dialog.hide();
jQuery('body').append(dialog);
jQuery.facebox({ div: '#plugin_download' });
}, this);
}
});
this.PluginManager = Backbone.Model.extend({
soshare_props: {
latest_version: '4.4.1',
mime_type: 'application/x-gyre-soshare',
activex_progid: 'gyre.soshare',
windows_download_url: 'https://torque.bittorrent.com/SoShare.msi',
osx_download_url: 'https://torque.bittorrent.com/SoShare.pkg'
},
torque_props: {
latest_version: '4.3.8',
mime_type: 'application/x-bittorrent-torque',
activex_progid: 'bittorrent.torque',
windows_download_url: 'https://torque.bittorrent.com/Torque.msi',
osx_download_url: 'https://torque.bittorrent.com/Torque.pkg'
},
defaults: {
//Avoid DOM collisions by having a ridiculous id.
pid: 'btapp_plugin_WARNING_HAVE_NOT_INITIALIZED',
//All BitTorrent products have this number appended to their window names
window_hash: '4823',
product: 'Torque'
},
initialize: function() {
_.bindAll(this);
this.set('pid', 'btapp_plugin_' + Math.floor(Math.random() * 1024));
if(this.get('product') === 'SoShare') {
_.each(this.soshare_props, function(value, key) {
if(!this.has(key)) {
this.set(key, value);
}
}, this);
} else if(this.get('product') === 'Torque' || this.get('product') === 'uTorrent' || this.get('product') === 'BitTorrent') {
//Everyone else can piggy back on the torque plugin
_.each(this.torque_props, function(value, key) {
if(!this.has(key)) {
this.set(key, value);
}
}, this);
}
var download_url = isMac() ? this.get('osx_download_url') : this.get('windows_download_url');
this.set('download_url', download_url);
//when we load jquery, we should defer a call to mime_type_check
jQuery(_.bind(_.defer, this, this.mime_type_check));
},
disconnect: function() {
},
//we know nothing. we want:
//the plugin installed
//the plugin up to date
//the client installed
//the client running
mime_type_check: function() {
if(this.supports_mime_type()) {
this.mime_type_check_yes();
} else {
this.mime_type_check_no();
}
},
mime_type_check_no: function() {
var switches = {'install':false};
this.trigger('plugin:install_plugin', switches);
when(this.supports_mime_type, this.mime_type_check_yes);
},
mime_type_check_yes: function() {
this.trigger('plugin:plugin_installed');
var add = add_plugin(this.get('mime_type'));
add.then(_.bind(function() {
this.trigger('plugin:plugin_running');
this.plugin_up_to_date_check();
}, this));
},
plugin_up_to_date_check: function() {
if(this.plugin_up_to_date()) {
this.plugin_up_to_date_yes();
} else {
this.plugin_up_to_date_no();
}
},
plugin_up_to_date_yes: function() {
this.client_installed_check();
},
plugin_up_to_date_no: function() {
var updateoptions = {'update':true};
this.trigger('plugin:update_plugin', updateoptions);
if(updateoptions.update) {
this.get_plugin().checkForUpdate(_.bind(this.plugin_up_to_date_yes, this));
} else {
this.plugin_up_to_date_yes();
}
},
//the plugin is installed. good.
client_installed_check: function() {
if(this.client_installed()) {
this.client_installed_check_yes();
} else {
this.client_installed_check_no();
}
},
client_installed_check_no: function() {
var installoptions = {'install':true};
this.trigger('plugin:install_client', installoptions);
if(installoptions.install) {
this.get_plugin().downloadProgram(this.get('product'), _.bind(function(a,b,c,d,key) {
jQuery.jStorage.set('pairing_key', key);
this.trigger('plugin:downloaded_client');
}, this));
when(this.client_installed, this.client_running_check_yes);
} else {
var runningcheckoptions = {'check':false};
this.trigger('plugin:check_for_running_client', runningcheckoptions);
if(runningcheckoptions.check) {
this.client_running_check();
}
}
},
client_installed_check_yes: function() {
this.trigger('plugin:client_installed');
this.client_running_check();
},
//the client is installed. good.
client_running_check: function() {
if(this.client_running()) {
this.client_running_check_yes();
} else {
this.client_running_check_no();
}
},
client_running_check_no: function() {
var switches = {'run':true};
this.trigger('plugin:run_client', switches);
if(switches.run) {
this.get_plugin().runProgram(this.get('product'), function() {});
}
when(this.client_running, this.client_running_check_yes);
},
client_running_check_yes: function() {
//well i'll be...looks like we made it to the end.
this.trigger('plugin:client_running');
},
// Plugin Specific Functionality
// ---------------------------
supports_mime_type: function() {
if(window.location.protocol === 'chrome-extension:') {
//if we're in a chrome extension, assume we have the mime type available
return true;
}
var isIE = (navigator.appVersion.indexOf('MSIE') !== -1) ? true : false;
if(isIE) {
try {
var tq = new ActiveXObject(this.get('activex_progid'));
return tq !== undefined;
} catch (e) {
return false;
}
} else {
navigator.plugins.refresh();
for (var i = 0; i < navigator.plugins.length; i++) {
var plugin = navigator.plugins[i][0];
if (plugin.type === this.get('mime_type')) {
return true;
}
}
return false;
}
},
plugin_up_to_date: function() {
if(window.location.protocol === 'chrome-extension:') {
//if we're in a chrome extension, there's a different update path
return true;
}
var version = this.get_plugin().version;
var version_arr = _.map(version.split('.'), function(i) { return parseInt(i, 10); });
var required_version_arr = _.map(this.get('latest_version').split('.'), function(i) { return parseInt(i, 10); });
for (var i=0; i<version_arr.length; i++) {
if (version_arr[i] < required_version_arr[i]) {
return false;
} else if(version_arr[i] > required_version_arr[i]) {
return true;
}
}
return true;
},
get_plugin: function() {
var plugins = jQuery('[type="' + this.get('mime_type') + '"]');
assert(plugins.length === 1, 'cannot call get_plugin before adding the plugin');
return plugins[0];
},
plugin_loaded: function() {
assert(this.supports_mime_type(), 'you have not installed the plugin yet');
assert(jQuery('#' + this.get('pid')).length !== 0, 'you have not yet added the plugin');
return this.get_plugin().version;
},
// Client Specific Functionality
// ---------------------------
// Lets ask the plugin if the specific client is running.
get_window_name: function(product) {
if (product === 'uTorrent') {
return 'Torrent4823';
} else {
return product;
}
},
client_running: function() {
var ret = this.get_plugin().isRunning(this.get_window_name(this.get('product')));
if(typeof ret === 'object') {
return ret && ret.length > 0;
} else {
return ret;
}
},
client_installed: function() {
var version = this.get_plugin().getInstallVersion(this.get('product'));
var not_supported = 'This application is not supported.';
assert(version !== not_supported, not_supported);
return version;
}
});
}).call(this);