Source: main/webapp/modules/Object.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * An object used by the Guacamole client to house arbitrarily-many named
  22. * input and output streams.
  23. *
  24. * @constructor
  25. * @param {!Guacamole.Client} client
  26. * The client owning this object.
  27. *
  28. * @param {!number} index
  29. * The index of this object.
  30. */
  31. Guacamole.Object = function guacamoleObject(client, index) {
  32. /**
  33. * Reference to this Guacamole.Object.
  34. *
  35. * @private
  36. * @type {!Guacamole.Object}
  37. */
  38. var guacObject = this;
  39. /**
  40. * Map of stream name to corresponding queue of callbacks. The queue of
  41. * callbacks is guaranteed to be in order of request.
  42. *
  43. * @private
  44. * @type {!Object.<string, function[]>}
  45. */
  46. var bodyCallbacks = {};
  47. /**
  48. * Removes and returns the callback at the head of the callback queue for
  49. * the stream having the given name. If no such callbacks exist, null is
  50. * returned.
  51. *
  52. * @private
  53. * @param {!string} name
  54. * The name of the stream to retrieve a callback for.
  55. *
  56. * @returns {function}
  57. * The next callback associated with the stream having the given name,
  58. * or null if no such callback exists.
  59. */
  60. var dequeueBodyCallback = function dequeueBodyCallback(name) {
  61. // If no callbacks defined, simply return null
  62. var callbacks = bodyCallbacks[name];
  63. if (!callbacks)
  64. return null;
  65. // Otherwise, pull off first callback, deleting the queue if empty
  66. var callback = callbacks.shift();
  67. if (callbacks.length === 0)
  68. delete bodyCallbacks[name];
  69. // Return found callback
  70. return callback;
  71. };
  72. /**
  73. * Adds the given callback to the tail of the callback queue for the stream
  74. * having the given name.
  75. *
  76. * @private
  77. * @param {!string} name
  78. * The name of the stream to associate with the given callback.
  79. *
  80. * @param {!function} callback
  81. * The callback to add to the queue of the stream with the given name.
  82. */
  83. var enqueueBodyCallback = function enqueueBodyCallback(name, callback) {
  84. // Get callback queue by name, creating first if necessary
  85. var callbacks = bodyCallbacks[name];
  86. if (!callbacks) {
  87. callbacks = [];
  88. bodyCallbacks[name] = callbacks;
  89. }
  90. // Add callback to end of queue
  91. callbacks.push(callback);
  92. };
  93. /**
  94. * The index of this object.
  95. *
  96. * @type {!number}
  97. */
  98. this.index = index;
  99. /**
  100. * Called when this object receives the body of a requested input stream.
  101. * By default, all objects will invoke the callbacks provided to their
  102. * requestInputStream() functions based on the name of the stream
  103. * requested. This behavior can be overridden by specifying a different
  104. * handler here.
  105. *
  106. * @event
  107. * @param {!Guacamole.InputStream} inputStream
  108. * The input stream of the received body.
  109. *
  110. * @param {!string} mimetype
  111. * The mimetype of the data being received.
  112. *
  113. * @param {!string} name
  114. * The name of the stream whose body has been received.
  115. */
  116. this.onbody = function defaultBodyHandler(inputStream, mimetype, name) {
  117. // Call queued callback for the received body, if any
  118. var callback = dequeueBodyCallback(name);
  119. if (callback)
  120. callback(inputStream, mimetype);
  121. };
  122. /**
  123. * Called when this object is being undefined. Once undefined, no further
  124. * communication involving this object may occur.
  125. *
  126. * @event
  127. */
  128. this.onundefine = null;
  129. /**
  130. * Requests read access to the input stream having the given name. If
  131. * successful, a new input stream will be created.
  132. *
  133. * @param {!string} name
  134. * The name of the input stream to request.
  135. *
  136. * @param {function} [bodyCallback]
  137. * The callback to invoke when the body of the requested input stream
  138. * is received. This callback will be provided a Guacamole.InputStream
  139. * and its mimetype as its two only arguments. If the onbody handler of
  140. * this object is overridden, this callback will not be invoked.
  141. */
  142. this.requestInputStream = function requestInputStream(name, bodyCallback) {
  143. // Queue body callback if provided
  144. if (bodyCallback)
  145. enqueueBodyCallback(name, bodyCallback);
  146. // Send request for input stream
  147. client.requestObjectInputStream(guacObject.index, name);
  148. };
  149. /**
  150. * Creates a new output stream associated with this object and having the
  151. * given mimetype and name. The legality of a mimetype and name is dictated
  152. * by the object itself.
  153. *
  154. * @param {!string} mimetype
  155. * The mimetype of the data which will be sent to the output stream.
  156. *
  157. * @param {!string} name
  158. * The defined name of an output stream within this object.
  159. *
  160. * @returns {!Guacamole.OutputStream}
  161. * An output stream which will write blobs to the named output stream
  162. * of this object.
  163. */
  164. this.createOutputStream = function createOutputStream(mimetype, name) {
  165. return client.createObjectOutputStream(guacObject.index, mimetype, name);
  166. };
  167. };
  168. /**
  169. * The reserved name denoting the root stream of any object. The contents of
  170. * the root stream MUST be a JSON map of stream name to mimetype.
  171. *
  172. * @constant
  173. * @type {!string}
  174. */
  175. Guacamole.Object.ROOT_STREAM = '/';
  176. /**
  177. * The mimetype of a stream containing JSON which maps available stream names
  178. * to their corresponding mimetype. The root stream of a Guacamole.Object MUST
  179. * have this mimetype.
  180. *
  181. * @constant
  182. * @type {!string}
  183. */
  184. Guacamole.Object.STREAM_INDEX_MIMETYPE = 'application/vnd.glyptodon.guacamole.stream-index+json';