|
| 1 | +//! `flash.net.URLLoader` builtin/prototype |
| 2 | +
|
| 3 | +use crate::avm2::activation::Activation; |
| 4 | +use crate::avm2::class::Class; |
| 5 | +use crate::avm2::method::{Method, NativeMethodImpl}; |
| 6 | +use crate::avm2::names::{Namespace, QName}; |
| 7 | +use crate::avm2::object::TObject; |
| 8 | +use crate::avm2::value::Value; |
| 9 | +use crate::avm2::{Error, Object}; |
| 10 | +use crate::backend::navigator::RequestOptions; |
| 11 | +use crate::loader::DataFormat; |
| 12 | +use crate::string::AvmString; |
| 13 | +use gc_arena::{GcCell, MutationContext}; |
| 14 | + |
| 15 | +/// Implements `flash.net.URLLoader`'s class constructor. |
| 16 | +pub fn class_init<'gc>( |
| 17 | + _activation: &mut Activation<'_, 'gc, '_>, |
| 18 | + _this: Option<Object<'gc>>, |
| 19 | + _args: &[Value<'gc>], |
| 20 | +) -> Result<Value<'gc>, Error> { |
| 21 | + Ok(Value::Undefined) |
| 22 | +} |
| 23 | + |
| 24 | +/// Implements `flash.net.URLLoader`'s instance constructor. |
| 25 | +pub fn instance_init<'gc>( |
| 26 | + activation: &mut Activation<'_, 'gc, '_>, |
| 27 | + this: Option<Object<'gc>>, |
| 28 | + args: &[Value<'gc>], |
| 29 | +) -> Result<Value<'gc>, Error> { |
| 30 | + if let Some(mut this) = this { |
| 31 | + activation.super_init(this, &[])?; |
| 32 | + this.set_property( |
| 33 | + &QName::new(Namespace::public(), "dataFormat").into(), |
| 34 | + "text".into(), |
| 35 | + activation, |
| 36 | + )?; |
| 37 | + this.set_property( |
| 38 | + &QName::new(Namespace::public(), "data").into(), |
| 39 | + Value::Undefined, |
| 40 | + activation, |
| 41 | + )?; |
| 42 | + |
| 43 | + if let Some(request) = args.get(0) { |
| 44 | + if request != &Value::Null { |
| 45 | + load(activation, Some(this), args)?; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + Ok(Value::Undefined) |
| 50 | +} |
| 51 | + |
| 52 | +pub fn bytes_loaded<'gc>( |
| 53 | + activation: &mut Activation<'_, 'gc, '_>, |
| 54 | + this: Option<Object<'gc>>, |
| 55 | + args: &[Value<'gc>], |
| 56 | +) -> Result<Value<'gc>, Error> { |
| 57 | + // For now, just use `bytes_total`. The `bytesLoaded` value |
| 58 | + // should really update as the download progresses, instead |
| 59 | + // of jumping at completion from 0 to the total length |
| 60 | + log::warn!("URLLoader.bytesLoaded - not yet implemented"); |
| 61 | + bytes_total(activation, this, args) |
| 62 | +} |
| 63 | + |
| 64 | +pub fn bytes_total<'gc>( |
| 65 | + activation: &mut Activation<'_, 'gc, '_>, |
| 66 | + this: Option<Object<'gc>>, |
| 67 | + _args: &[Value<'gc>], |
| 68 | +) -> Result<Value<'gc>, Error> { |
| 69 | + if let Some(this) = this { |
| 70 | + let data = |
| 71 | + this.get_property(&QName::new(Namespace::public(), "data").into(), activation)?; |
| 72 | + |
| 73 | + if let Value::Object(data) = data { |
| 74 | + // `bytesTotal` should be 0 while the download is in progress |
| 75 | + // (the `data` property is only set after the download is completed) |
| 76 | + if let Some(array) = data.as_bytearray() { |
| 77 | + return Ok(array.len().into()); |
| 78 | + } else { |
| 79 | + return Err(format!("Unexpected value for `data` property: {:?}", data).into()); |
| 80 | + } |
| 81 | + } else if let Value::String(data) = data { |
| 82 | + return Ok(data.len().into()); |
| 83 | + } |
| 84 | + return Ok(0.into()); |
| 85 | + } |
| 86 | + Ok(Value::Undefined) |
| 87 | +} |
| 88 | + |
| 89 | +pub fn load<'gc>( |
| 90 | + activation: &mut Activation<'_, 'gc, '_>, |
| 91 | + this: Option<Object<'gc>>, |
| 92 | + args: &[Value<'gc>], |
| 93 | +) -> Result<Value<'gc>, Error> { |
| 94 | + if let Some(this) = this { |
| 95 | + let request = match args.get(0) { |
| 96 | + Some(Value::Object(request)) => request, |
| 97 | + // This should never actually happen |
| 98 | + _ => return Ok(Value::Undefined), |
| 99 | + }; |
| 100 | + |
| 101 | + let data_format = this |
| 102 | + .get_property( |
| 103 | + &QName::new(Namespace::public(), "dataFormat").into(), |
| 104 | + activation, |
| 105 | + )? |
| 106 | + .coerce_to_string(activation)?; |
| 107 | + |
| 108 | + let data_format = if data_format == AvmString::from("binary") { |
| 109 | + DataFormat::Binary |
| 110 | + } else if data_format == AvmString::from("text") { |
| 111 | + DataFormat::Text |
| 112 | + } else if data_format == AvmString::from("variables") { |
| 113 | + DataFormat::Variables |
| 114 | + } else { |
| 115 | + return Err(format!("Unknown data format: {}", data_format).into()); |
| 116 | + }; |
| 117 | + |
| 118 | + return spawn_fetch(activation, this, request, data_format); |
| 119 | + } |
| 120 | + Ok(Value::Undefined) |
| 121 | +} |
| 122 | + |
| 123 | +fn spawn_fetch<'gc>( |
| 124 | + activation: &mut Activation<'_, 'gc, '_>, |
| 125 | + loader_object: Object<'gc>, |
| 126 | + url_request: &Object<'gc>, |
| 127 | + data_format: DataFormat, |
| 128 | +) -> Result<Value<'gc>, Error> { |
| 129 | + let url = url_request |
| 130 | + .get_property(&QName::new(Namespace::public(), "url").into(), activation)? |
| 131 | + .coerce_to_string(activation)?; |
| 132 | + |
| 133 | + let url = url.to_utf8_lossy(); |
| 134 | + |
| 135 | + let future = activation.context.load_manager.load_data_into_url_loader( |
| 136 | + activation.context.player.clone(), |
| 137 | + loader_object, |
| 138 | + &url, |
| 139 | + // FIXME - get these from the `URLRequest` |
| 140 | + RequestOptions::get(), |
| 141 | + data_format, |
| 142 | + ); |
| 143 | + activation.context.navigator.spawn_future(future); |
| 144 | + Ok(Value::Undefined) |
| 145 | +} |
| 146 | + |
| 147 | +pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { |
| 148 | + let class = Class::new( |
| 149 | + QName::new(Namespace::package("flash.net"), "URLLoader"), |
| 150 | + Some(QName::new(Namespace::package("flash.events"), "EventDispatcher").into()), |
| 151 | + Method::from_builtin(instance_init, "<URLLoader instance initializer>", mc), |
| 152 | + Method::from_builtin(class_init, "<URLLoader class initializer>", mc), |
| 153 | + mc, |
| 154 | + ); |
| 155 | + |
| 156 | + let mut write = class.write(mc); |
| 157 | + |
| 158 | + const PUBLIC_INSTANCE_PROPERTIES: &[( |
| 159 | + &str, |
| 160 | + Option<NativeMethodImpl>, |
| 161 | + Option<NativeMethodImpl>, |
| 162 | + )] = &[ |
| 163 | + ("bytesLoaded", Some(bytes_loaded), None), |
| 164 | + ("bytesTotal", Some(bytes_total), None), |
| 165 | + ]; |
| 166 | + write.define_public_builtin_instance_properties(mc, PUBLIC_INSTANCE_PROPERTIES); |
| 167 | + |
| 168 | + const PUBLIC_INSTANCE_SLOTS: &[(&str, &str, &str)] = &[("data", "", "Object")]; |
| 169 | + write.define_public_slot_instance_traits(PUBLIC_INSTANCE_SLOTS); |
| 170 | + |
| 171 | + const PUBLIC_INSTANCE_METHODS: &[(&str, NativeMethodImpl)] = &[("load", load)]; |
| 172 | + write.define_public_builtin_instance_methods(mc, PUBLIC_INSTANCE_METHODS); |
| 173 | + |
| 174 | + class |
| 175 | +} |
0 commit comments