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