forked from mirrors/gecko-dev
		
	 30b37e77e5
			
		
	
	
		30b37e77e5
		
	
	
	
	
		
			
			<!-- Please describe your changes on the following line: --> See https://github.com/servo/rust-mozjs/issues/343#issuecomment-294513870 Heap::new() constructor is unsafe. Heap should be set after reflect_dom_object call in order to prevent potential GC crashes. <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [x] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: e8aa3759bd8f74f7a962e421bd0cbf2a0a3e35da --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 1f7e2346a5f25efa3e055ff02cd6b9090de23837
		
			
				
	
	
		
			118 lines
		
	
	
	
		
			3.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
	
		
			3.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| /* This Source Code Form is subject to the terms of the Mozilla Public
 | |
|  * License, v. 2.0. If a copy of the MPL was not distributed with this
 | |
|  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | |
| 
 | |
| use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
 | |
| use dom::bindings::codegen::Bindings::MessageEventBinding;
 | |
| use dom::bindings::codegen::Bindings::MessageEventBinding::MessageEventMethods;
 | |
| use dom::bindings::error::Fallible;
 | |
| use dom::bindings::inheritance::Castable;
 | |
| use dom::bindings::js::Root;
 | |
| use dom::bindings::reflector::reflect_dom_object;
 | |
| use dom::bindings::str::DOMString;
 | |
| use dom::bindings::trace::RootedTraceableBox;
 | |
| use dom::event::Event;
 | |
| use dom::eventtarget::EventTarget;
 | |
| use dom::globalscope::GlobalScope;
 | |
| use dom_struct::dom_struct;
 | |
| use js::jsapi::{HandleValue, Heap, JSContext};
 | |
| use js::jsval::JSVal;
 | |
| use servo_atoms::Atom;
 | |
| 
 | |
| #[dom_struct]
 | |
| pub struct MessageEvent {
 | |
|     event: Event,
 | |
|     data: Heap<JSVal>,
 | |
|     origin: DOMString,
 | |
|     lastEventId: DOMString,
 | |
| }
 | |
| 
 | |
| impl MessageEvent {
 | |
|     pub fn new_uninitialized(global: &GlobalScope) -> Root<MessageEvent> {
 | |
|         MessageEvent::new_initialized(global,
 | |
|                                       HandleValue::undefined(),
 | |
|                                       DOMString::new(),
 | |
|                                       DOMString::new())
 | |
|     }
 | |
| 
 | |
|     pub fn new_initialized(global: &GlobalScope,
 | |
|                            data: HandleValue,
 | |
|                            origin: DOMString,
 | |
|                            lastEventId: DOMString) -> Root<MessageEvent> {
 | |
|         let ev = box MessageEvent {
 | |
|             event: Event::new_inherited(),
 | |
|             data: Heap::default(),
 | |
|             origin: origin,
 | |
|             lastEventId: lastEventId,
 | |
|         };
 | |
|         let ev = reflect_dom_object(ev, global, MessageEventBinding::Wrap);
 | |
|         ev.data.set(data.get());
 | |
| 
 | |
|         ev
 | |
|     }
 | |
| 
 | |
|     pub fn new(global: &GlobalScope, type_: Atom,
 | |
|                bubbles: bool, cancelable: bool,
 | |
|                data: HandleValue, origin: DOMString, lastEventId: DOMString)
 | |
|                -> Root<MessageEvent> {
 | |
|         let ev = MessageEvent::new_initialized(global, data, origin, lastEventId);
 | |
|         {
 | |
|             let event = ev.upcast::<Event>();
 | |
|             event.init_event(type_, bubbles, cancelable);
 | |
|         }
 | |
|         ev
 | |
|     }
 | |
| 
 | |
|     pub fn Constructor(global: &GlobalScope,
 | |
|                        type_: DOMString,
 | |
|                        init: RootedTraceableBox<MessageEventBinding::MessageEventInit>)
 | |
|                        -> Fallible<Root<MessageEvent>> {
 | |
|         let ev = MessageEvent::new(global,
 | |
|                                    Atom::from(type_),
 | |
|                                    init.parent.bubbles,
 | |
|                                    init.parent.cancelable,
 | |
|                                    init.data.handle(),
 | |
|                                    init.origin.clone(),
 | |
|                                    init.lastEventId.clone());
 | |
|         Ok(ev)
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl MessageEvent {
 | |
|     pub fn dispatch_jsval(target: &EventTarget,
 | |
|                           scope: &GlobalScope,
 | |
|                           message: HandleValue) {
 | |
|         let messageevent = MessageEvent::new(
 | |
|             scope,
 | |
|             atom!("message"),
 | |
|             false,
 | |
|             false,
 | |
|             message,
 | |
|             DOMString::new(),
 | |
|             DOMString::new());
 | |
|         messageevent.upcast::<Event>().fire(target);
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl MessageEventMethods for MessageEvent {
 | |
|     #[allow(unsafe_code)]
 | |
|     // https://html.spec.whatwg.org/multipage/#dom-messageevent-data
 | |
|     unsafe fn Data(&self, _cx: *mut JSContext) -> JSVal {
 | |
|         self.data.get()
 | |
|     }
 | |
| 
 | |
|     // https://html.spec.whatwg.org/multipage/#dom-messageevent-origin
 | |
|     fn Origin(&self) -> DOMString {
 | |
|         self.origin.clone()
 | |
|     }
 | |
| 
 | |
|     // https://html.spec.whatwg.org/multipage/#dom-messageevent-lasteventid
 | |
|     fn LastEventId(&self) -> DOMString {
 | |
|         self.lastEventId.clone()
 | |
|     }
 | |
| 
 | |
|     // https://dom.spec.whatwg.org/#dom-event-istrusted
 | |
|     fn IsTrusted(&self) -> bool {
 | |
|         self.event.IsTrusted()
 | |
|     }
 | |
| }
 |