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