dom/workers/test/extensions/traditional/WorkerTest.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 const Cc = Components.classes;
     7 const Ci = Components.interfaces;
     8 const Cu = Components.utils;
    10 Cu.import("resource://gre/modules/Services.jsm");
    11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    13 var gWorkerAndCallback = {
    14   _worker: null,
    15   _callback: null,
    17   _ensureStarted: function() {
    18     if (!this._worker) {
    19       throw new Error("Not yet started!");
    20     }
    21   },
    23   start: function() {
    24     if (!this._worker) {
    25       var file = __LOCATION__.parent.parent;
    26       var fileuri = file.isDirectory() ?
    27                     Services.io.newFileURI(file) :
    28                     Services.io.newURI('jar:' + file.path + '!/', null, null);
    29       var resourceName = "worker-test";
    31       Services.io.getProtocolHandler("resource").
    32                   QueryInterface(Ci.nsIResProtocolHandler).
    33                   setSubstitution(resourceName, fileuri);
    35       var worker = new Worker("resource://" + resourceName + "/worker.js");
    36       worker.onerror = function(event) {
    37         Cu.reportError(event.message);
    38         event.preventDefault();
    39       };
    41       this._worker = worker;
    42     }
    43   },
    45   stop: function() {
    46     if (this._worker) {
    47       try {
    48         this.terminate();
    49       }
    50       catch(e) {
    51         Cu.reportError(e);
    52       }
    53       this._worker = null;
    54     }
    55   },
    57   set callback(val) {
    58     this._ensureStarted();
    59     if (val) {
    60       var callback = val.QueryInterface(Ci.nsIWorkerTestCallback);
    61       if (this.callback != callback) {
    62         this._worker.onmessage = function(event) {
    63           callback.onmessage(event.data);
    64         };
    65         this._worker.onerror = function(event) {
    66           callback.onerror(event.message);
    67           event.preventDefault();
    68         };
    69         this._callback = callback;
    70       }
    71     }
    72     else {
    73       this._worker.onmessage = null;
    74       this._worker.onerror = null;
    75       this._callback = null;
    76     }
    77   },
    79   get callback() {
    80     return this._callback;
    81   },
    83   postMessage: function(data) {
    84     this._ensureStarted();
    85     this._worker.postMessage(data);
    86   },
    88   terminate: function() {
    89     this._ensureStarted();
    90     this._worker.terminate();
    91     this.callback = null;
    92   }
    93 };
    95 function WorkerTest() {
    96 }
    97 WorkerTest.prototype = {
    98   observe: function(subject, topic, data) {
    99     switch(topic) {
   100       case "profile-after-change":
   101         gWorkerAndCallback.start();
   102         Services.obs.addObserver(this, "profile-before-change", false);
   103         break;
   104       case "profile-before-change":
   105         gWorkerAndCallback.stop();
   106         break;
   107       default:
   108         Cu.reportError("Unknown topic: " + topic);
   109     }
   110   },
   112   set callback(val) {
   113     gWorkerAndCallback.callback = val;
   114   },
   116   get callback() {
   117     return gWorkerAndCallback.callback;
   118   },
   120   postMessage: function(message) {
   121     gWorkerAndCallback.postMessage(message);
   122   },
   124   terminate: function() {
   125     gWorkerAndCallback.terminate();
   126   },
   128   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIWorkerTest]),
   129   classID: Components.ID("{3b52b935-551f-4606-ba4c-decc18b67bfd}")
   130 };
   132 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WorkerTest]);

mercurial