dom/apps/src/InterAppConnection.js

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:c3256dfe36a1
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
8
9 Cu.import("resource://gre/modules/Services.jsm");
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
12
13 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
14 "@mozilla.org/childprocessmessagemanager;1",
15 "nsIMessageSender");
16
17 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
18 "@mozilla.org/AppsService;1",
19 "nsIAppsService");
20
21 const DEBUG = false;
22 function debug(aMsg) {
23 dump("-- InterAppConnection: " + Date.now() + ": " + aMsg + "\n");
24 }
25
26 /**
27 * MozInterAppConnection implementation.
28 */
29
30 function InterAppConnection() {
31 if (DEBUG) debug("InterAppConnection()");
32 this.keyword = null;
33 this.publisher = null;
34 this.subscriber = null;
35 };
36
37 InterAppConnection.prototype = {
38 __proto__: DOMRequestIpcHelper.prototype,
39
40 classDescription: "MozInterAppConnection",
41
42 classID: Components.ID("{9dbfa904-0718-11e3-8e77-0721a45514b8}"),
43
44 contractID: "@mozilla.org/dom/inter-app-connection;1",
45
46 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
47 Ci.nsISupportsWeakReference,
48 Ci.nsIObserver]),
49
50 __init: function(aKeyword, aPublisher, aSubscriber) {
51 if (DEBUG) {
52 debug("__init: aKeyword: " + aKeyword +
53 " aPublisher: " + aPublisher + " aSubscriber: " + aSubscriber);
54 }
55 this.keyword = aKeyword;
56 this.publisher = aPublisher;
57 this.subscriber = aSubscriber;
58 },
59
60 // Ci.nsIDOMGlobalPropertyInitializer implementation.
61 init: function(aWindow) {
62 if (DEBUG) debug("init");
63
64 this.initDOMRequestHelper(aWindow, []);
65 let principal = aWindow.document.nodePrincipal;
66 this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
67 },
68
69 cancel: function() {
70 if (DEBUG) debug("cancel");
71
72 cpmm.sendAsyncMessage("InterAppConnection:Cancel",
73 { keyword: this.keyword,
74 pubAppManifestURL: this.publisher,
75 subAppManifestURL: this.subscriber,
76 manifestURL: this._manifestURL });
77 }
78 };
79
80
81 /**
82 * MozInterAppConnectionRequest implementation.
83 */
84
85 function InterAppConnectionRequest() {
86 if (DEBUG) debug("InterAppConnectionRequest()");
87 this.keyword = null;
88 this.port = null;
89 };
90
91 InterAppConnectionRequest.prototype = {
92 classDescription: "MozInterAppConnectionRequest",
93
94 classID: Components.ID("{6a77e9e0-0645-11e3-b90b-73bb7c78e06a}"),
95
96 contractID: "@mozilla.org/dom/inter-app-connection-request;1",
97
98 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
99
100 __init: function(aKeyword, aPort) {
101 if (DEBUG) debug("__init: aKeyword: " + aKeyword + " aPort: " + aPort);
102 this.keyword = aKeyword;
103 this.port = aPort;
104 }
105 };
106
107 /**
108 * InterAppConnectionRequestWrapper implementation.
109 *
110 * This implements nsISystemMessagesWrapper.wrapMessage(), which provides a
111 * plugable way to wrap a "connection" type system message.
112 *
113 * Please see SystemMessageManager.js to know how it customizes the wrapper.
114 */
115
116 function InterAppConnectionRequestWrapper() {
117 if (DEBUG) debug("InterAppConnectionRequestWrapper()");
118 }
119
120 InterAppConnectionRequestWrapper.prototype = {
121 // nsISystemMessagesWrapper implementation.
122 wrapMessage: function(aMessage, aWindow) {
123 if (DEBUG) debug("wrapMessage: " + JSON.stringify(aMessage));
124
125 let port = new aWindow.MozInterAppMessagePort(aMessage.messagePortID);
126 let connectionRequest =
127 new aWindow.MozInterAppConnectionRequest(aMessage.keyword, port);
128
129 return connectionRequest;
130 },
131
132 classDescription: "InterAppConnectionRequestWrapper",
133
134 classID: Components.ID("{d7c7a466-f91d-11e2-812a-6fab12ece58e}"),
135
136 contractID: "@mozilla.org/dom/system-messages/wrapper/connection;1",
137
138 QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemMessagesWrapper])
139 }
140
141
142 this.NSGetFactory =
143 XPCOMUtils.generateNSGetFactory([InterAppConnection,
144 InterAppConnectionRequest,
145 InterAppConnectionRequestWrapper]);

mercurial