1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/apps/src/InterAppConnection.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 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 { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; 1.11 + 1.12 +Cu.import("resource://gre/modules/Services.jsm"); 1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.14 +Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); 1.15 + 1.16 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm", 1.17 + "@mozilla.org/childprocessmessagemanager;1", 1.18 + "nsIMessageSender"); 1.19 + 1.20 +XPCOMUtils.defineLazyServiceGetter(this, "appsService", 1.21 + "@mozilla.org/AppsService;1", 1.22 + "nsIAppsService"); 1.23 + 1.24 +const DEBUG = false; 1.25 +function debug(aMsg) { 1.26 + dump("-- InterAppConnection: " + Date.now() + ": " + aMsg + "\n"); 1.27 +} 1.28 + 1.29 +/** 1.30 + * MozInterAppConnection implementation. 1.31 + */ 1.32 + 1.33 +function InterAppConnection() { 1.34 + if (DEBUG) debug("InterAppConnection()"); 1.35 + this.keyword = null; 1.36 + this.publisher = null; 1.37 + this.subscriber = null; 1.38 +}; 1.39 + 1.40 +InterAppConnection.prototype = { 1.41 + __proto__: DOMRequestIpcHelper.prototype, 1.42 + 1.43 + classDescription: "MozInterAppConnection", 1.44 + 1.45 + classID: Components.ID("{9dbfa904-0718-11e3-8e77-0721a45514b8}"), 1.46 + 1.47 + contractID: "@mozilla.org/dom/inter-app-connection;1", 1.48 + 1.49 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, 1.50 + Ci.nsISupportsWeakReference, 1.51 + Ci.nsIObserver]), 1.52 + 1.53 + __init: function(aKeyword, aPublisher, aSubscriber) { 1.54 + if (DEBUG) { 1.55 + debug("__init: aKeyword: " + aKeyword + 1.56 + " aPublisher: " + aPublisher + " aSubscriber: " + aSubscriber); 1.57 + } 1.58 + this.keyword = aKeyword; 1.59 + this.publisher = aPublisher; 1.60 + this.subscriber = aSubscriber; 1.61 + }, 1.62 + 1.63 + // Ci.nsIDOMGlobalPropertyInitializer implementation. 1.64 + init: function(aWindow) { 1.65 + if (DEBUG) debug("init"); 1.66 + 1.67 + this.initDOMRequestHelper(aWindow, []); 1.68 + let principal = aWindow.document.nodePrincipal; 1.69 + this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); 1.70 + }, 1.71 + 1.72 + cancel: function() { 1.73 + if (DEBUG) debug("cancel"); 1.74 + 1.75 + cpmm.sendAsyncMessage("InterAppConnection:Cancel", 1.76 + { keyword: this.keyword, 1.77 + pubAppManifestURL: this.publisher, 1.78 + subAppManifestURL: this.subscriber, 1.79 + manifestURL: this._manifestURL }); 1.80 + } 1.81 +}; 1.82 + 1.83 + 1.84 +/** 1.85 + * MozInterAppConnectionRequest implementation. 1.86 + */ 1.87 + 1.88 +function InterAppConnectionRequest() { 1.89 + if (DEBUG) debug("InterAppConnectionRequest()"); 1.90 + this.keyword = null; 1.91 + this.port = null; 1.92 +}; 1.93 + 1.94 +InterAppConnectionRequest.prototype = { 1.95 + classDescription: "MozInterAppConnectionRequest", 1.96 + 1.97 + classID: Components.ID("{6a77e9e0-0645-11e3-b90b-73bb7c78e06a}"), 1.98 + 1.99 + contractID: "@mozilla.org/dom/inter-app-connection-request;1", 1.100 + 1.101 + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), 1.102 + 1.103 + __init: function(aKeyword, aPort) { 1.104 + if (DEBUG) debug("__init: aKeyword: " + aKeyword + " aPort: " + aPort); 1.105 + this.keyword = aKeyword; 1.106 + this.port = aPort; 1.107 + } 1.108 +}; 1.109 + 1.110 +/** 1.111 + * InterAppConnectionRequestWrapper implementation. 1.112 + * 1.113 + * This implements nsISystemMessagesWrapper.wrapMessage(), which provides a 1.114 + * plugable way to wrap a "connection" type system message. 1.115 + * 1.116 + * Please see SystemMessageManager.js to know how it customizes the wrapper. 1.117 + */ 1.118 + 1.119 +function InterAppConnectionRequestWrapper() { 1.120 + if (DEBUG) debug("InterAppConnectionRequestWrapper()"); 1.121 +} 1.122 + 1.123 +InterAppConnectionRequestWrapper.prototype = { 1.124 + // nsISystemMessagesWrapper implementation. 1.125 + wrapMessage: function(aMessage, aWindow) { 1.126 + if (DEBUG) debug("wrapMessage: " + JSON.stringify(aMessage)); 1.127 + 1.128 + let port = new aWindow.MozInterAppMessagePort(aMessage.messagePortID); 1.129 + let connectionRequest = 1.130 + new aWindow.MozInterAppConnectionRequest(aMessage.keyword, port); 1.131 + 1.132 + return connectionRequest; 1.133 + }, 1.134 + 1.135 + classDescription: "InterAppConnectionRequestWrapper", 1.136 + 1.137 + classID: Components.ID("{d7c7a466-f91d-11e2-812a-6fab12ece58e}"), 1.138 + 1.139 + contractID: "@mozilla.org/dom/system-messages/wrapper/connection;1", 1.140 + 1.141 + QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemMessagesWrapper]) 1.142 +} 1.143 + 1.144 + 1.145 +this.NSGetFactory = 1.146 + XPCOMUtils.generateNSGetFactory([InterAppConnection, 1.147 + InterAppConnectionRequest, 1.148 + InterAppConnectionRequestWrapper]);