mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:07:45 +00:00
LibWeb: Keep XMLHttpRequest alive while handling load/error events
A weakly held XHR object is not guaranteed to remain alive after running arbitrary JavaScript, so let's make sure we take a strong reference in the ResourceLoader callbacks here.
This commit is contained in:
parent
398a95c3c9
commit
ee8a1a9b3f
1 changed files with 9 additions and 6 deletions
|
@ -212,7 +212,8 @@ DOM::ExceptionOr<void> XMLHttpRequest::send()
|
||||||
ResourceLoader::the().load(
|
ResourceLoader::the().load(
|
||||||
request,
|
request,
|
||||||
[weak_this = make_weak_ptr()](auto data, auto& response_headers, auto status_code) {
|
[weak_this = make_weak_ptr()](auto data, auto& response_headers, auto status_code) {
|
||||||
if (!weak_this)
|
auto strong_this = weak_this.strong_ref();
|
||||||
|
if (!strong_this)
|
||||||
return;
|
return;
|
||||||
auto& xhr = const_cast<XMLHttpRequest&>(*weak_this);
|
auto& xhr = const_cast<XMLHttpRequest&>(*weak_this);
|
||||||
// FIXME: Handle OOM failure.
|
// FIXME: Handle OOM failure.
|
||||||
|
@ -235,12 +236,14 @@ DOM::ExceptionOr<void> XMLHttpRequest::send()
|
||||||
xhr.fire_progress_event(EventNames::loadend, transmitted, length);
|
xhr.fire_progress_event(EventNames::loadend, transmitted, length);
|
||||||
},
|
},
|
||||||
[weak_this = make_weak_ptr()](auto& error, auto status_code) {
|
[weak_this = make_weak_ptr()](auto& error, auto status_code) {
|
||||||
if (!weak_this)
|
|
||||||
return;
|
|
||||||
dbgln("XHR failed to load: {}", error);
|
dbgln("XHR failed to load: {}", error);
|
||||||
const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
|
auto strong_this = weak_this.strong_ref();
|
||||||
const_cast<XMLHttpRequest&>(*weak_this).set_status(status_code.value_or(0));
|
if (!strong_this)
|
||||||
const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::error));
|
return;
|
||||||
|
auto& xhr = const_cast<XMLHttpRequest&>(*strong_this);
|
||||||
|
xhr.set_ready_state(ReadyState::Done);
|
||||||
|
xhr.set_status(status_code.value_or(0));
|
||||||
|
xhr.dispatch_event(DOM::Event::create(HTML::EventNames::error));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
TODO();
|
TODO();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue