1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 use dom::bindings::inheritance::Castable;
6 use dom::bindings::refcounted::Trusted;
7 use dom::event::{EventBubbles, EventCancelable, EventTask, SimpleEventTask};
8 use dom::eventtarget::EventTarget;
9 use dom::window::Window;
10 use msg::constellation_msg::PipelineId;
11 use script_runtime::{CommonScriptMsg, ScriptThreadEventCategory};
12 use script_thread::MainThreadScriptMsg;
13 use servo_atoms::Atom;
14 use std::fmt;
15 use std::result::Result;
16 use std::sync::mpsc::Sender;
17 use task::{TaskCanceller, TaskOnce};
18 use task_source::TaskSource;
19 
20 #[derive(Clone, JSTraceable)]
21 pub struct DOMManipulationTaskSource(pub Sender<MainThreadScriptMsg>, pub PipelineId);
22 
23 impl fmt::Debug for DOMManipulationTaskSource {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result24     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25         write!(f, "DOMManipulationTaskSource(...)")
26     }
27 }
28 
29 impl TaskSource for DOMManipulationTaskSource {
queue_with_canceller<T>( &self, task: T, canceller: &TaskCanceller, ) -> Result<(), ()> where T: TaskOnce + 'static,30     fn queue_with_canceller<T>(
31         &self,
32         task: T,
33         canceller: &TaskCanceller,
34     ) -> Result<(), ()>
35     where
36         T: TaskOnce + 'static,
37     {
38         let msg = MainThreadScriptMsg::Common(CommonScriptMsg::Task(
39             ScriptThreadEventCategory::ScriptEvent,
40             Box::new(canceller.wrap_task(task)),
41             Some(self.1)
42         ));
43         self.0.send(msg).map_err(|_| ())
44     }
45 }
46 
47 impl DOMManipulationTaskSource {
queue_event(&self, target: &EventTarget, name: Atom, bubbles: EventBubbles, cancelable: EventCancelable, window: &Window)48     pub fn queue_event(&self,
49                        target: &EventTarget,
50                        name: Atom,
51                        bubbles: EventBubbles,
52                        cancelable: EventCancelable,
53                        window: &Window) {
54         let target = Trusted::new(target);
55         let task = EventTask {
56             target: target,
57             name: name,
58             bubbles: bubbles,
59             cancelable: cancelable,
60         };
61         let _ = self.queue(task, window.upcast());
62     }
63 
queue_simple_event(&self, target: &EventTarget, name: Atom, window: &Window)64     pub fn queue_simple_event(&self, target: &EventTarget, name: Atom, window: &Window) {
65         let target = Trusted::new(target);
66         let _ = self.queue(SimpleEventTask { target, name }, window.upcast());
67     }
68 }
69