1use std::sync::Arc;
6
7use indexmap::IndexMap;
8use js::context::JSContext;
9use pixels::RasterImage;
10use servo_constellation_traits::BlobImpl;
11
12use crate::dom::bindings::error::{Error, Fallible};
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::file::File;
16use crate::dom::globalscope::GlobalScope;
17
18#[derive(MallocSizeOf)]
20pub(crate) enum Kind {
21 Text {
22 data: DOMString,
23 type_: DOMString,
24 },
25 File {
26 bytes: Vec<u8>,
27 name: DOMString,
28 type_: String,
29 },
30}
31
32impl Kind {
33 pub(crate) fn type_(&self) -> DOMString {
34 match self {
35 Kind::Text { type_, .. } => type_.clone(),
36 Kind::File { type_, .. } => DOMString::from(type_.clone()),
37 }
38 }
39
40 pub(crate) fn as_string(&self) -> Option<String> {
41 match self {
42 Kind::Text { data, .. } => Some(data.to_string()),
43 Kind::File { .. } => None,
44 }
45 }
46
47 pub(crate) fn as_file(
50 &self,
51 cx: &mut JSContext,
52 global: &GlobalScope,
53 ) -> Option<DomRoot<File>> {
54 match self {
55 Kind::Text { .. } => None,
56 Kind::File { bytes, name, type_ } => Some(File::new(
57 cx,
58 global,
59 BlobImpl::new_from_bytes(bytes.clone(), type_.clone()),
60 name.clone(),
61 None,
62 )),
63 }
64 }
65
66 fn text_type_matches(&self, text_type: &DOMString) -> bool {
67 matches!(self, Kind::Text { type_, .. } if type_.eq(text_type))
68 }
69
70 fn is_file(&self) -> bool {
71 matches!(self, Kind::File { .. })
72 }
73}
74
75#[derive(MallocSizeOf)]
77struct Bitmap {
78 #[conditional_malloc_size_of]
79 image: Option<Arc<RasterImage>>,
80 x: i32,
81 y: i32,
82}
83
84#[derive(Clone, Copy, Eq, MallocSizeOf, PartialEq)]
86pub(crate) enum Mode {
87 ReadWrite,
89 ReadOnly,
91 Protected,
93}
94
95#[derive(MallocSizeOf)]
96pub(crate) struct DragDataStore {
97 item_list: IndexMap<u16, Kind>,
99 next_item_id: u16,
100 default_feedback: Option<String>,
102 bitmap: Option<Bitmap>,
103 mode: Mode,
104 allowed_effects_state: String,
106 pub clear_was_called: bool,
107}
108
109impl DragDataStore {
110 pub(crate) fn new() -> DragDataStore {
113 DragDataStore {
114 item_list: IndexMap::new(),
115 next_item_id: 0,
116 default_feedback: None,
117 bitmap: None,
118 mode: Mode::Protected,
119 allowed_effects_state: String::from("uninitialized"),
120 clear_was_called: false,
121 }
122 }
123
124 pub(crate) fn mode(&self) -> Mode {
126 self.mode
127 }
128
129 pub(crate) fn set_mode(&mut self, mode: Mode) {
131 self.mode = mode;
132 }
133
134 pub(crate) fn set_bitmap(&mut self, image: Option<Arc<RasterImage>>, x: i32, y: i32) {
135 self.bitmap = Some(Bitmap { image, x, y });
136 }
137
138 pub(crate) fn types(&self) -> Vec<DOMString> {
140 let mut types = Vec::new();
141
142 let has_files = self.item_list.values().fold(false, |has_files, item| {
143 match item {
146 Kind::Text { type_, .. } => types.push(type_.clone()),
147 Kind::File { .. } => return true,
148 }
149
150 has_files
151 });
152
153 if has_files {
156 types.push(DOMString::from("Files"));
157 }
158 types
159 }
160
161 pub(crate) fn find_matching_text(&self, type_: &DOMString) -> Option<DOMString> {
162 self.item_list
163 .values()
164 .find(|item| item.text_type_matches(type_))
165 .and_then(|item| item.as_string())
166 .map(DOMString::from)
167 }
168
169 pub(crate) fn add(&mut self, kind: Kind) -> Fallible<u16> {
170 if let Kind::Text { ref type_, .. } = kind {
171 if self
174 .item_list
175 .values()
176 .any(|item| item.text_type_matches(type_))
177 {
178 return Err(Error::NotSupported(None));
179 }
180 }
181
182 let item_id = self.next_item_id;
183
184 self.item_list.insert(item_id, kind);
186
187 self.next_item_id += 1;
188 Ok(item_id)
189 }
190
191 pub(crate) fn set_data(&mut self, format: DOMString, data: DOMString) {
192 let type_ = normalize_mime(format);
194
195 self.item_list
198 .retain(|_, item| !item.text_type_matches(&type_));
199
200 self.item_list
202 .insert(self.next_item_id, Kind::Text { data, type_ });
203 self.next_item_id += 1;
204 }
205
206 pub(crate) fn clear_data(&mut self, format: Option<DOMString>) -> bool {
207 let mut was_modified = false;
208
209 if let Some(format) = format {
210 let type_ = normalize_mime(format);
212
213 self.item_list.retain(|_, item| {
215 let matches = item.text_type_matches(&type_);
216
217 if matches {
218 was_modified = true;
219 }
220 !matches
221 });
222 } else {
223 self.item_list.retain(|_, item| {
225 let matches = item.is_file();
226
227 if !matches {
228 was_modified = true;
229 }
230 matches
231 });
232 }
233
234 was_modified
235 }
236
237 pub(crate) fn files(
238 &self,
239 cx: &mut JSContext,
240 global: &GlobalScope,
241 file_list: &mut Vec<DomRoot<File>>,
242 ) {
243 if self.mode == Mode::Protected {
245 return;
246 }
247
248 self.item_list
250 .values()
251 .filter_map(|item| item.as_file(cx, global))
252 .for_each(|file| file_list.push(file));
253 }
254
255 pub(crate) fn list_len(&self) -> usize {
256 self.item_list.len()
257 }
258
259 pub(crate) fn iter_item_list(&self) -> indexmap::map::Values<'_, u16, Kind> {
260 self.item_list.values()
261 }
262
263 pub(crate) fn get_by_index(&self, index: usize) -> Option<(&u16, &Kind)> {
264 self.item_list.get_index(index)
265 }
266
267 pub(crate) fn get_by_id(&self, id: &u16) -> Option<&Kind> {
268 self.item_list.get(id)
269 }
270
271 pub(crate) fn remove(&mut self, index: usize) {
272 self.item_list.shift_remove_index(index);
273 }
274
275 pub(crate) fn clear_list(&mut self) {
276 self.item_list.clear();
277 self.clear_was_called = true;
278 }
279}
280
281fn normalize_mime(mut format: DOMString) -> DOMString {
282 format.make_ascii_lowercase();
284
285 match &*format.str() {
286 "text" => DOMString::from("text/plain"),
288 "url" => DOMString::from("text/uri-list"),
290 s => DOMString::from(s),
291 }
292}