Skip to content

Commit

Permalink
Fix loading resources on macOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Mar 13, 2024
1 parent cf8ef94 commit 7c346c6
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions webview/platform/mac/webview_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/flat_map.h"

#include <crl/crl_on_main.h>
#include <crl/crl_time.h>
#include <rpl/rpl.h>

#include <QtCore/QUrl>
Expand Down Expand Up @@ -238,8 +239,8 @@ - (void) dealloc {

private:
struct Task {
bool cancelled = false;
rpl::lifetime destructor;
int index = 0;
crl::time started = 0;
};
struct PartialResource {
uint32 index = 0;
Expand All @@ -264,9 +265,10 @@ explicit operator bool() const {
using CacheKey = uint64;

static void TaskFail(TaskPointer task);
void taskFail(TaskPointer task);
void taskFail(TaskPointer task, int indexToCheck);
void taskDone(
TaskPointer task,
int indexToCheck,
const std::string &mime,
NSData *data,
int64 offset,
Expand Down Expand Up @@ -295,6 +297,7 @@ void taskDone(
base::flat_map<CacheKey, PartData> _partsCache;
std::vector<CacheKey> _partsLRU;
int64 _cacheTotal = 0;
int _taskAutoincrement = 0;

};

Expand Down Expand Up @@ -342,19 +345,34 @@ void taskDone(
[task didFailWithError:[NSError errorWithDomain:@"org.telegram.desktop" code:404 userInfo:nil]];
}

void Instance::taskFail(TaskPointer task) {
const auto removed = _tasks.take(task);
void Instance::taskFail(TaskPointer task, int indexToCheck) {
if (indexToCheck) {
const auto i = _tasks.find(task);
if (i == end(_tasks) || i->second.index != indexToCheck) {
return;
}
_tasks.erase(i);
}
TaskFail(task);
}

void Instance::taskDone(
TaskPointer task,
int indexToCheck,
const std::string &mime,
NSData *data,
int64 offset,
int64 total) {
Expects(data != nil);

if (indexToCheck) {
const auto i = _tasks.find(task);
if (i == end(_tasks) || i->second.index != indexToCheck) {
return;
}
_tasks.erase(i);
}

const auto length = int64([data length]);
const auto partial = (offset > 0) || (total != length);
NSMutableDictionary *headers = [@{
Expand Down Expand Up @@ -530,9 +548,7 @@ void taskDone(

void Instance::processDataRequest(TaskPointer task, bool started) {
if (!started) {
if (const auto i = _tasks.find(task); i != end(_tasks)) {
i->second.cancelled = true;
}
_tasks.remove(task);
return;
}

Expand All @@ -541,7 +557,7 @@ void taskDone(
NSString *url = task.request.URL.absoluteString;
NSString *prefix = stdToNS(kFullDomain);
if (![url hasPrefix:prefix]) {
taskFail(task);
taskFail(task, 0);
return;
}

Expand All @@ -554,24 +570,27 @@ void taskDone(
ParseRangeHeaderFor(prepared, std::string([rangeHeader UTF8String]));

if (const auto cached = fillFromCache(prepared)) {
taskDone(task, cached.mime, cached.data, prepared.offset, cached.total);
taskDone(task, 0, cached.mime, cached.data, prepared.offset, cached.total);
return;
}
}

const auto index = ++_taskAutoincrement;
_tasks[task] = Task{ .index = index, .started = crl::now() };

const auto requestedOffset = prepared.offset;
const auto requestedLimit = prepared.limit;
prepared.done = crl::guard(this, [=](DataResponse resolved) {
auto &stream = resolved.stream;
if (!stream) {
return taskFail(task);
return taskFail(task, index);
}
const auto length = stream->size();
Assert(length > 0);

const auto offset = resolved.streamOffset;
if (requestedOffset >= offset + length || offset > requestedOffset) {
return taskFail(task);
return taskFail(task, index);
}

auto bytes = std::unique_ptr<char[]>(new char[length]);
Expand All @@ -598,15 +617,13 @@ void taskDone(
}
addToCache(partial.index, offset, { std::move(bytes), length });
}
taskDone(task, mime, data, requestedOffset, total);
taskDone(task, index, mime, data, requestedOffset, total);
});
const auto result = _dataRequestHandler
? _dataRequestHandler(prepared)
: DataResult::Failed;
if (result == DataResult::Failed) {
return taskFail(task);
} else if (result == DataResult::Pending) {
_tasks.emplace(task, Task());
return taskFail(task, index);
}

}
Expand Down

0 comments on commit 7c346c6

Please sign in to comment.