dom/activities/src/ActivityWrapper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/activities/src/ActivityWrapper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     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 file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +
    1.17 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    1.18 +                                   "@mozilla.org/childprocessmessagemanager;1",
    1.19 +                                   "nsISyncMessageSender");
    1.20 +
    1.21 +function debug(aMsg) {
    1.22 +  //dump("-- ActivityWrapper.js " + Date.now() + " : " + aMsg + "\n");
    1.23 +}
    1.24 +
    1.25 +/**
    1.26 +  * nsISystemMessagesWrapper implementation. Will return a
    1.27 +  * nsIDOMMozActivityRequestHandler
    1.28 +  */
    1.29 +function ActivityWrapper() {
    1.30 +  debug("ActivityWrapper");
    1.31 +}
    1.32 +
    1.33 +ActivityWrapper.prototype = {
    1.34 +  wrapMessage: function wrapMessage(aMessage, aWindow) {
    1.35 +    debug("Wrapping " + JSON.stringify(aMessage));
    1.36 +
    1.37 +    // This message is useful to communicate that the activity message has been
    1.38 +    // properly received by the app. If the app will be killed, the
    1.39 +    // ActivitiesService will be able to fire an error and complete the
    1.40 +    // Activity workflow.
    1.41 +    cpmm.sendAsyncMessage("Activity:Ready", { id: aMessage.id });
    1.42 +
    1.43 +    let handler = new aWindow.ActivityRequestHandler(aMessage.id, aMessage.payload);
    1.44 +
    1.45 +    // When the activity window is closed, fire an error to notify the activity
    1.46 +    // caller of the situation.
    1.47 +    // We don't need to check whether the activity itself already sent
    1.48 +    // back something since ActivitiesService.jsm takes care of that.
    1.49 +    let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    1.50 +                      .getInterface(Ci.nsIDOMWindowUtils);
    1.51 +    let innerWindowID = util.currentInnerWindowID;
    1.52 +
    1.53 +    let observer = {
    1.54 +      observe: function(aSubject, aTopic, aData) {
    1.55 +
    1.56 +        switch (aTopic) {
    1.57 +          case 'inner-window-destroyed':
    1.58 +            let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
    1.59 +            if (wId == innerWindowID) {
    1.60 +              debug("Closing activity window " + innerWindowID);
    1.61 +              Services.obs.removeObserver(observer, "inner-window-destroyed");
    1.62 +              cpmm.sendAsyncMessage("Activity:PostError",
    1.63 +                                    { id: aMessage.id,
    1.64 +                                      error: "ActivityCanceled"
    1.65 +                                    });
    1.66 +            }
    1.67 +            break;
    1.68 +          case 'activity-error':
    1.69 +          case 'activity-success':
    1.70 +            if (aData !== aMessage.id) {
    1.71 +              return;
    1.72 +            }
    1.73 +            Services.obs.removeObserver(observer, "activity-error");
    1.74 +            Services.obs.removeObserver(observer, "activity-success");
    1.75 +            let docshell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    1.76 +                                  .getInterface(Ci.nsIWebNavigation);
    1.77 +            Services.obs.notifyObservers(docshell, "activity-done", aTopic);
    1.78 +            break;
    1.79 +        }
    1.80 +      }
    1.81 +    }
    1.82 +
    1.83 +    Services.obs.addObserver(observer, "activity-error", false);
    1.84 +    Services.obs.addObserver(observer, "activity-success", false);
    1.85 +    Services.obs.addObserver(observer, "inner-window-destroyed", false);
    1.86 +    return handler;
    1.87 +  },
    1.88 +
    1.89 +  classID: Components.ID("{5430d6f9-32d6-4924-ba39-6b6d1b093cd6}"),
    1.90 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemMessagesWrapper])
    1.91 +}
    1.92 +
    1.93 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivityWrapper]);
    1.94 +

mercurial