|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @fileoverview Defines the PostMessager interface needed for the |
| 19 | + * `fireauth.messagechannel.Sender`, in addition to 2 types of implementations. |
| 20 | + */ |
| 21 | + |
| 22 | +goog.provide('fireauth.messagechannel.PostMessager'); |
| 23 | +goog.provide('fireauth.messagechannel.WindowPostMessager'); |
| 24 | +goog.provide('fireauth.messagechannel.WorkerClientPostMessager'); |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * This is the interface defining the postMessage format of a window which |
| 29 | + * takes an additional second parameter for target origin. |
| 30 | + * |
| 31 | + * @typedef {{ |
| 32 | + * postMessage: function(*, string, !Array<!Transferable>) |
| 33 | + * }} |
| 34 | + */ |
| 35 | +fireauth.messagechannel.Window; |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * This is the interface defining the postMessage format of a worker or |
| 40 | + * ServiceWorkerClient, etc. which just takes a message and a list of |
| 41 | + * Transferables. |
| 42 | + * |
| 43 | + * @typedef {{ |
| 44 | + * postMessage: function(*, !Array<!Transferable>) |
| 45 | + * }} |
| 46 | + */ |
| 47 | +fireauth.messagechannel.WorkerClient; |
| 48 | + |
| 49 | + |
| 50 | +/** |
| 51 | + * Defines a common interface to postMessage data to a specified PostMessager. |
| 52 | + * @interface |
| 53 | + */ |
| 54 | +fireauth.messagechannel.PostMessager = function() {}; |
| 55 | + |
| 56 | + |
| 57 | +/** |
| 58 | + * Sends a message to the specified context. |
| 59 | + * @param {*} message The message to send. |
| 60 | + * @param {!Array<!Transferable>} transfer The list of `Transferable` objects |
| 61 | + * that are transferred with the message. The ownsership fo these objects is |
| 62 | + * given to the destination side and they are no longer usable on the |
| 63 | + * sending side. |
| 64 | + */ |
| 65 | +fireauth.messagechannel.PostMessager.prototype.postMessage = |
| 66 | + function(message, transfer) {}; |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +/** |
| 71 | + * Defines the implementation for postMessaging to a window context. |
| 72 | + * @param {!fireauth.messagechannel.Window} win The window PostMessager. |
| 73 | + * @param {?string=} opt_targetOrigin The target origin. |
| 74 | + * @constructor |
| 75 | + * @implements {fireauth.messagechannel.PostMessager} |
| 76 | + */ |
| 77 | +fireauth.messagechannel.WindowPostMessager = function(win, opt_targetOrigin) { |
| 78 | + /** |
| 79 | + * @const @private {!fireauth.messagechannel.Window} The window PostMessager. |
| 80 | + */ |
| 81 | + this.win_ = win; |
| 82 | + /** @const @private {string} The postMessage target origin. */ |
| 83 | + this.targetOrigin_ = opt_targetOrigin || '*'; |
| 84 | +}; |
| 85 | + |
| 86 | + |
| 87 | +/** |
| 88 | + * Sends a message to the specified window context. |
| 89 | + * @param {*} message The message to send. |
| 90 | + * @param {!Array<!Transferable>} transfer The list of `Transferable` objects |
| 91 | + * that are transferred with the message. The ownsership fo these objects is |
| 92 | + * given to the destination side and they are no longer usable on the |
| 93 | + * sending side. |
| 94 | + * @override |
| 95 | + */ |
| 96 | +fireauth.messagechannel.WindowPostMessager.prototype.postMessage = |
| 97 | + function(message, transfer) { |
| 98 | + this.win_.postMessage(message, this.targetOrigin_, transfer); |
| 99 | +}; |
| 100 | + |
| 101 | + |
| 102 | +/** |
| 103 | + * Defines the implementation for postMessaging to a worker/client context. |
| 104 | + * @param {!fireauth.messagechannel.WorkerClient} worker The worker/client |
| 105 | + * PostMessager. |
| 106 | + * @constructor |
| 107 | + * @implements {fireauth.messagechannel.PostMessager} |
| 108 | + */ |
| 109 | +fireauth.messagechannel.WorkerClientPostMessager = function(worker) { |
| 110 | + /** |
| 111 | + * @const @private {!fireauth.messagechannel.WorkerClient} The worker/client |
| 112 | + * PostMessager. |
| 113 | + */ |
| 114 | + this.worker_ = worker; |
| 115 | +}; |
| 116 | + |
| 117 | + |
| 118 | +/** |
| 119 | + * Sends a message to the specified worker/client context. |
| 120 | + * @param {*} message The message to send. |
| 121 | + * @param {!Array<!Transferable>} transfer The list of `Transferable` objects |
| 122 | + * that are transferred with the message. The ownsership fo these objects is |
| 123 | + * given to the destination side and they are no longer usable on the |
| 124 | + * sending side. |
| 125 | + * @override |
| 126 | + */ |
| 127 | +fireauth.messagechannel.WorkerClientPostMessager.prototype.postMessage = |
| 128 | + function(message, transfer) { |
| 129 | + this.worker_.postMessage(message, transfer); |
| 130 | +}; |
0 commit comments