~ubuntu-branches/ubuntu/utopic/ubufox/utopic-proposed

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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is ubufox.
 *
 * The Initial Developer of the Original Code is
 * Canonical Ltd.
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Chris Coulson <chris.coulson@canonical.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

const Cc = Components.classes;
const Cu = Components.utils;
const Ci = Components.interfaces;

const nsITimer = Ci.nsITimer;

const INSTALL_NOT_STARTED = 0;
const INSTALL_IN_PROGRESS = 1;
const INSTALL_FINISHED = 2;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://ubufox/Distro.jsm");

var EXPORTED_SYMBOLS = [ "PluginFinder", "PluginFinderDevel" ];

var install = null;

["LOG", "WARN", "ERROR"].forEach(function(aName) {
  this.__defineGetter__(aName, function() {
    Components.utils.import("resource://gre/modules/AddonLogging.jsm");

    LogManager.getLogger("ubufox.pluginfinder", this);
    return this[aName];
  });
}, this);

function safeCall() {
  let obj = arguments[0];
  let member = arguments[1];
  if (obj[member]) {
    let args = Array.prototype.splice.call(arguments, 2);
    try {
      obj[member].apply(obj, args);
    } catch(ex) { Cu.reportError(ex); }
  }
}

function installContext() {
  this.state = INSTALL_NOT_STARTED;
  this.progress = -1;
}

function installMonitor(aPid, aRequest) {
  this.pid = aPid;
  this.request = aRequest;
}

installMonitor.prototype = {
  onInstallStarted: function iM_onInstallStarted() {
    PluginFinderInternal.onInstallStarted(this.pid, this.request);
  },

  onProgressChanged: function iM_onProgressChange(aProgress) {
    PluginFinderInternal.onProgressChanged(this.pid, this.request, aProgress);
  },

  onInstallFinished: function iM_onInstallFinished() {
    PluginFinderInternal.onInstallFinished(this.pid, this.request);
  },

  onInstallFailed: function iM_onInstallFailed(aError) {
    PluginFinderInternal.onInstallFailed(this.pid, this.request, aError);
  }
};

function installRequest(aPluginInfos, aListener) {
  this.listener = aListener;
  this.installers = {};
  this.state = INSTALL_NOT_STARTED;
  this.pluginInfosLength = aPluginInfos.length;
}

function getPluginInfoRequest(aMimeType, aCallback) {
  this.mimetype = aMimeType;
  this.callback = aCallback;
  this.pendingReplies = PluginFinderInternal.providers.length;
  this.pluginInfos = [];
}

getPluginInfoRequest.prototype = {
  pluginInfoReceived: function gPIR_pluginInfoReceived(aPluginInfoArray) {
    PluginFinderInternal.pluginInfoReceived(aPluginInfoArray, this);
  },

  providerErrorReceived: function gPIR_providerErrorReceived(aError) {
    PluginFinderInternal.providerErrorReceived(aError, this);
  }
};

var PluginFinderInternal = {
  providers: [],
  installHandlers: {},

  init: function PFI_init() {
    LOG("Initializing plugin finder module");

    distro.PFSProviders.forEach(function(provider) {
      try {
        LOG("Loading provider " + provider);
        Cu.import(provider);
      } catch(e) {
        Cu.reportError("Failed to import provider: " + e);
      }
    });

    distro.PFSInstallers.forEach(function(installer) {
      try {
        LOG("Loading installer " + installer);
        Cu.import(installer);
      } catch(e) {
        Cu.reportError("Failed to import installer: " + e);
      }
    });
  },

  registerProvider: function PFI_registerProvider(aProvider) {
    if (!aProvider || typeof(aProvider.getPluginInfo) != "function") {
      throw new Error("Invalid provider");
    }

    if (this.providers.indexOf(aProvider) != -1) {
      throw new Error("Provider already registered");
    }

    this.providers.push(aProvider);
  },

  unregisterProvider: function PFI_unregisterProvider(aProvider) {
    let index = this.providers.indexOf(aProvider);
    if (index == -1) {
      throw new Error("Provider not registered");
    }

    this.providers.splice(index, 1);
  },

  registerInstallHandler: function PFI_registerInstallHandler(aHandler) {
    if (!aHandler || !aHandler.types ||
        typeof(aHandler.install) != "function") {
      throw new Error("Invalid installer");
    }

    let types = aHandler.types.split(",");
    types.forEach(function(type) {
      if (this.installHandlers[type]) {
        throw new Error("Handler already registered for type " + type);
      }
    }, this);

    types.forEach(function(type) {
      this.installHandlers[type] = aHandler;
    }, this);
  },

  unregisterInstallHandler: function PFI_unregisterInstallHandler(aHandler) {
    for (let type in this.installHandlers) {
      if (this.installHandlers[type] == aHandler) {
        delete this.installHandlers[type];
      }
    }
  },

  getPluginInfo: function PFI_getPluginInfo(aMimeType, aCallback) {
    LOG("New getPluginInfo request for " + aMimeType);
    if (typeof(aCallback) != "function") {
      throw new Error("There's no point in calling an async function \
                      without a callback");
    }

    if (!aMimeType) {
      throw new Error("No mimetype specified");
    }

    var request = new getPluginInfoRequest(aMimeType, aCallback);

    this.providers.forEach(function(provider) {
      try {
        provider.getPluginInfo(this.mimetype, this);
      } catch(ex) {
        this.pendingReplies -= 1;
        Cu.reportError(ex);
      }
    }, request);

    if (this.providers.length == 0) {
      this.doGetPluginInfoRespond(request);
    }
  },

  install: function PFI_install(aPluginInfos, aListener) {
    if (!aPluginInfos) {
      throw new Error("You forgot to specify an array of plugins");
    }

    var pluginInfos = {};

    aPluginInfos.forEach(function(pluginInfo) {
      if (pluginInfos[pluginInfo.pid]) {
        throw new Error("Duplicate plugin ID's detected");
      }

      pluginInfos[pluginInfo.pid] = pluginInfo;

      if (!this.installHandlers[pluginInfo.type]) {
        throw new Error("No handler for plugin with installation type "
                        + aPluginInfo.type);
      }
    }, this);

    var request = new installRequest(aPluginInfos, aListener);

    var timer = Cc["@mozilla.org/timer;1"].createInstance(nsITimer);
    timer.initWithCallback(function() {
      let timerKungFuDeathGrip = timer;
      aPluginInfos.forEach(function(pluginInfo) {
        this.installers[pluginInfo.pid] = new installContext();
        let mon = new installMonitor(pluginInfo.pid, this);
        try {
          PluginFinderInternal.installHandlers[pluginInfo.type].install(pluginInfo, mon);
        } catch(ex) {
          Cu.reportError("Install failed: " + ex);
          mon.onInstallFailed(null);
        }
      }, request);
    }, 0, nsITimer.TYPE_ONE_SHOT);
  },

  pluginInfoReceived: function PFI_pluginInfoReceived(aPluginInfoArray, aRequest) {
    LOG("getPluginInfo response from provider");
    if (!aPluginInfoArray) {
      throw new Error("Invalid getPluginInfo response from provider");
    }

    if (aRequest.pendingReplies <= 0) {
      ERROR("Unexpected reply from provider");
      return;
    }

    aRequest.pendingReplies -= 1;
    aRequest.pluginInfos = aRequest.pluginInfos.concat(aPluginInfoArray);
    LOG("Provider found " + aPluginInfoArray.length.toString() + " plugins");

    if (aRequest.pendingReplies == 0) {
      this.doGetPluginInfoRespond(aRequest);
    }
  },

  providerErrorReceived: function PFI_providerErrorReceived(aError, aRequest) {
    if (!aError) {
      throw new Error("Error from provider without any message");
    }

    if (aRequest.pendingReplies <= 0) {
      ERROR("Unexpected error from provider");
      return;
    }

    aRequest.pendingReplies -= 1;

    if (aRequest.pendingReplies == 0) {
      this.doGetPluginInfoRespond(aRequest);
    }
  },

  doGetPluginInfoRespond: function PFI_doGetPluginInfoRespond(aRequest) {
    var filteredPluginInfos = [];
    aRequest.pluginInfos.forEach(function(pluginInfo) {
      if (!this.installHandlers[pluginInfo.type] &&
          pluginInfo.type != "manual") {
        WARN("No install handler for " + pluginInfo.name);
      } else {
        filteredPluginInfos.push(pluginInfo);
      }
    }, this);

    LOG("Responding to getPluginInfo. Found " +
        filteredPluginInfos.length.toString() + " plugins");

    try {
      aRequest.callback(aRequest.mimetype, filteredPluginInfos);
    } catch(ex) {
      Cu.reportError(ex);
    }
  },

  onInstallStarted: function PFI_onInstallStarted(aPid, aRequest) {
    aRequest.installers[aPid].state = INSTALL_IN_PROGRESS;
    if (aRequest.state == INSTALL_NOT_STARTED) {
      aRequest.state = INSTALL_IN_PROGRESS;
      safeCall(aRequest.listener, "onInstallStarted");
    }
  },

  onProgressChanged: function PFI_onProgressChanged(aPid, aRequest, aProgress) {
    aRequest.installers[aPid].progress = aProgress;
    let progress = 0;
    for each (let installer in aRequest.installers) {
      if (installer.progress == -1) {
        continue;
      }

      progress += installer.progress / aRequest.pluginInfosLength;
      progress = Math.min(progress, 100);
      progress = Math.max(progress, 0);
    }

    safeCall(aRequest.listener, "onProgressChanged", progress);
  },

  onInstallFinished: function PFI_onInstallFinished(aPid, aRequest) {
    if (aRequest.installers[aPid].state == INSTALL_FINISHED) {
      WARN("Got onInstallFinished more than once");
      return;
    }

    this.onProgressChanged(aPid, aRequest, 100);

    aRequest.installers[aPid].state = INSTALL_FINISHED;
    safeCall(aRequest.listener, "onPluginFinished", aPid);

    for each (let installer in aRequest.installers) {
      if (installer.state != INSTALL_FINISHED) {
        return;
      }
    }

    aRequest.state = INSTALL_FINISHED;
    safeCall(aRequest.listener, "onInstallFinished");
  },

  onInstallFailed: function PFI_onInstallFailed(aPid, aRequest, aError) {
    if (aRequest.installers[aPid].state == INSTALL_FINISHED) {
      WARN("Got onInstallFinished more than once");
      return;
    }

    aRequest.installers[aPid].state = INSTALL_FINISHED;
    safeCall(aRequest.listener, "onPluginFailed", aPid, aError);

    for each (let installer in aRequest.installers) {
      if (installer.state != INSTALL_FINISHED) {
        return;
      }
    }

    aRequest.state = INSTALL_FINISHED;
    safeCall(aRequest.listener, "onInstallFinished");
  }
};

var PluginFinder = {
  registerProvider: function PF_registerProvider(aProvider) {
    PluginFinderInternal.registerProvider(aProvider);
  },

  unregisterProvider: function PF_unregisterProvider(aProvider) {
    PluginFinderInternal.unregisterProvider(aProvider);
  },

  registerInstallHandler: function PF_registerInstallHandler(aHandler) {
    PluginFinderInternal.registerInstallHandler(aHandler);
  },

  unregisterInstallHandler: function PF_unregisterInstallHandler(aHandler) {
    PluginFinderInternal.unregisterInstallHandler(aHandler);
  },

  getPluginInfo: function PF_getPluginInfo(aMimeType, aCallback) {
    PluginFinderInternal.getPluginInfo(aMimeType, aCallback);
  },

  install: function PF_install(aPluginInfos, aListener) {
    PluginFinderInternal.install(aPluginInfos, aListener);
  }
};

var PluginFinderDevel = {
  getNumberOfProviders: function PFD_getNumberOfProviders() {
    return PluginFinderInternal.providers.length;
  }
};

PluginFinderInternal.init();