content/base/src/messageWakeupService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/messageWakeupService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +
    1.13 +const CATEGORY_WAKEUP_REQUEST = "wakeup-request";
    1.14 +
    1.15 +function MessageWakeupService() { };
    1.16 +
    1.17 +MessageWakeupService.prototype =
    1.18 +{
    1.19 +  classID:          Components.ID("{f9798742-4f7b-4188-86ba-48b116412b29}"),
    1.20 +  QueryInterface:   XPCOMUtils.generateQI([Ci.nsIObserver]),
    1.21 +
    1.22 +  messagesData: [],
    1.23 +
    1.24 +  get messageManager() {
    1.25 +    if (!this._messageManager)
    1.26 +      this._messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"].
    1.27 +                             getService(Ci.nsIMessageListenerManager);
    1.28 +    return this._messageManager;
    1.29 +  },
    1.30 +
    1.31 +  requestWakeup: function(aMessageName, aCid, aIid, aMethod) {
    1.32 +    this.messagesData[aMessageName] = {
    1.33 +      cid: aCid,
    1.34 +      iid: aIid,
    1.35 +      method: aMethod,
    1.36 +    };
    1.37 +
    1.38 +    this.messageManager.addMessageListener(aMessageName, this);
    1.39 +  },
    1.40 +
    1.41 +  receiveMessage: function(aMessage) {
    1.42 +    let data = this.messagesData[aMessage.name];
    1.43 +    // TODO: When bug 593407 is ready, stop doing the wrappedJSObject hack
    1.44 +    //       and use this line instead:
    1.45 +    //                  QueryInterface(Ci.nsIMessageListener);
    1.46 +    let service = Cc[data.cid][data.method](Ci[data.iid]).
    1.47 +                  wrappedJSObject;
    1.48 +
    1.49 +    // The receiveMessage() call itself may spin an event loop, and we
    1.50 +    // do not want to swap listeners in that - it would cause the current
    1.51 +    // message to be answered by two listeners. So, we call that first,
    1.52 +    // then queue the swap for the next event loop
    1.53 +    let ret = service.receiveMessage(aMessage);
    1.54 +
    1.55 +    if (data.timer) {
    1.56 +      // Handle the case of two such messages happening in quick succession
    1.57 +      data.timer.cancel();
    1.58 +      data.timer = null;
    1.59 +    }
    1.60 +
    1.61 +    data.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.62 +    let self = this;
    1.63 +    data.timer.initWithCallback(function() {
    1.64 +      self.messageManager.addMessageListener(aMessage.name, service);
    1.65 +      self.messageManager.removeMessageListener(aMessage.name, self);
    1.66 +      delete self.messagesData[aMessage.name];
    1.67 +    }, 0, Ci.nsITimer.TYPE_ONE_SHOT);
    1.68 +
    1.69 +    return ret;
    1.70 +  },
    1.71 +
    1.72 +  observe: function TM_observe(aSubject, aTopic, aData) {
    1.73 +    switch (aTopic) {
    1.74 +      case "profile-after-change":
    1.75 +        {
    1.76 +          var catMan = Cc["@mozilla.org/categorymanager;1"].
    1.77 +                           getService(Ci.nsICategoryManager);
    1.78 +          var entries = catMan.enumerateCategory(CATEGORY_WAKEUP_REQUEST);
    1.79 +          while (entries.hasMoreElements()) {
    1.80 +            var entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
    1.81 +            var value = catMan.getCategoryEntry(CATEGORY_WAKEUP_REQUEST, entry);
    1.82 +            var parts = value.split(",");
    1.83 +            var cid = parts[0];
    1.84 +            var iid = parts[1];
    1.85 +            var method = parts[2];
    1.86 +            var messages = parts.slice(3);
    1.87 +            messages.forEach(function(messageName) {
    1.88 +              this.requestWakeup(messageName, cid, iid, method);
    1.89 +            }, this);
    1.90 +          }
    1.91 +        }
    1.92 +        break;
    1.93 +    }
    1.94 +  },
    1.95 +};
    1.96 +
    1.97 +var components = [MessageWakeupService];
    1.98 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
    1.99 +

mercurial