View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software 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    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.javascript.host.event;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_ONMESSAGE_DEFAULT_DATA_NULL;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
22  
23  import com.gargoylesoftware.htmlunit.HttpHeader;
24  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
25  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
28  import com.gargoylesoftware.htmlunit.javascript.host.Window;
29  import com.gargoylesoftware.htmlunit.javascript.host.WindowProxy;
30  
31  import net.sourceforge.htmlunit.corejs.javascript.Context;
32  import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
33  import net.sourceforge.htmlunit.corejs.javascript.Undefined;
34  
35  /**
36   * A JavaScript object for {@code MessageEvent}.
37   *
38   * For general information on which properties and functions should be supported, see
39   * <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#messageevent">
40   * Event definitions</a>.
41   *
42   * @see <a href="https://developer.mozilla.org/en/WebSockets/WebSockets_reference/MessageEvent">
43   *          Mozilla documentation</a>
44   * @author Ahmed Ashour
45   * @author Frank Danek
46   */
47  @JsxClass
48  public class MessageEvent extends Event {
49  
50      private Object data_;
51      private String origin_;
52      private String lastEventId_;
53      private Window source_;
54      private Object ports_;
55  
56      /**
57       * Default constructor used to build the prototype.
58       */
59      public MessageEvent() {
60          setType(TYPE_MESSAGE);
61          origin_ = "";
62          lastEventId_ = "";
63          data_ = Undefined.instance;
64      }
65  
66      /**
67       * Constructs a Message Event with the provided data.
68       * @param data the data
69       */
70      public MessageEvent(final Object data) {
71          this();
72          data_ = data;
73      }
74  
75      /**
76       * JavaScript constructor.
77       *
78       * @param type the event type
79       * @param details the event details (optional)
80       */
81      @Override
82      @JsxConstructor({CHROME, FF, EDGE})
83      public void jsConstructor(final String type, final ScriptableObject details) {
84          super.jsConstructor(type, details);
85  
86          if (getBrowserVersion().hasFeature(EVENT_ONMESSAGE_DEFAULT_DATA_NULL)) {
87              data_ = null;
88          }
89  
90          String origin = "";
91          String lastEventId = "";
92          if (details != null && details != Undefined.instance) {
93              data_ = details.get("data");
94  
95              final String detailOrigin = (String) details.get(HttpHeader.ORIGIN_LC);
96              if (detailOrigin != null) {
97                  origin = detailOrigin;
98              }
99  
100             final Object detailLastEventId = details.get("lastEventId");
101             if (detailLastEventId != null) {
102                 lastEventId = Context.toString(detailLastEventId);
103             }
104 
105             source_ = null;
106             final Object detailSource = details.get("source");
107             if (detailSource instanceof Window) {
108                 source_ = (Window) detailSource;
109             }
110             else if (detailSource instanceof WindowProxy) {
111                 source_ = ((WindowProxy) detailSource).getDelegee();
112             }
113             ports_ = details.get("ports");
114         }
115         origin_ = origin;
116         lastEventId_ = lastEventId;
117     }
118 
119     /**
120      * Initializes an event object.
121      * @param type the event type
122      * @param canBubble can the event bubble
123      * @param cancelable can the event be canceled
124      * @param data the message
125      * @param origin the scheme, hostname and port of the document that caused the event
126      * @param lastEventId the identifier of the last event
127      * @param source the window object that contains the document that caused the event
128      * @param ports the message ports
129      */
130     @JsxFunction({CHROME, IE, FF})
131     public void initMessageEvent(
132             final String type,
133             final boolean canBubble,
134             final boolean cancelable,
135             final Object data,
136             final String origin,
137             final String lastEventId,
138             final Window source,
139             final Object ports) {
140         initEvent(type, canBubble, cancelable);
141         data_ = data;
142         origin_ = origin;
143         lastEventId_ = lastEventId;
144         source_ = source;
145         ports_ = ports;
146     }
147 
148     /**
149      * Retrieves the data contained.
150      * @return the data contained
151      */
152     @JsxGetter
153     public Object getData() {
154         return data_;
155     }
156 
157     /**
158      * Gets the URI of the document of origin.
159      * @return the origin
160      */
161     @JsxGetter
162     public String getOrigin() {
163         return origin_;
164     }
165 
166     /**
167      * Sets the URI of the document of origin.
168      * @param origin the origin
169      */
170     public void setOrigin(final String origin) {
171         origin_ = origin;
172     }
173 
174     /**
175      * Retrieves the identifier of the last event.
176      * @return the identified of the last event
177      */
178     @JsxGetter({CHROME, FF})
179     public String getLastEventId() {
180         return lastEventId_;
181     }
182 
183     /**
184      * Retrieves the data contained.
185      * @return the data contained
186      */
187     @JsxGetter
188     public Window getSource() {
189         return source_;
190     }
191 
192     /**
193      * Returns the {@code ports} property.
194      * @return the {@code ports} property
195      */
196     @JsxGetter({CHROME, IE, FF})
197     public Object getPorts() {
198         return ports_;
199     }
200 }