Skip to content

Commit d4386b1

Browse files
committed
refactor(runtime/{webview,serviceworker,conduit}): improve service worker and conduit routing
1 parent e70bcff commit d4386b1

File tree

13 files changed

+185
-80
lines changed

13 files changed

+185
-80
lines changed

api/service-worker/context.js

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class Context {
8282
return await clients.get(this.event.clientId)
8383
}
8484

85-
console.log('NO EVENT CLIENT ID')
8685
return null
8786
}
8887
}

src/runtime/bridge/bridge.cc

+7-17
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,7 @@ export * from '{{url}}'
482482

483483
const auto app = App::sharedApplication();
484484
const auto options = serviceworker::Fetch::Options { request->client };
485-
const auto fetched = serviceWorker->fetch(fetch, options, [
486-
this,
487-
applicationResources,
488-
contentLocation,
489-
serviceWorker,
490-
resourcePath,
491-
userConfig,
492-
callback,
493-
response,
494-
request
495-
] (auto res) mutable {
485+
const auto fetched = serviceWorker->fetch(fetch, options, [=, this, &request] (auto res) mutable {
496486
if (!request->isActive()) {
497487
return;
498488
}
@@ -602,12 +592,12 @@ export * from '{{url}}'
602592
});
603593

604594
if (fetched) {
605-
this->getRuntime()->services.timers.setTimeout(32000, [request] () mutable {
606-
if (request->isActive()) {
607-
auto response = SchemeHandlers::Response(request, 408);
608-
response.fail("ServiceWorker request timed out.");
609-
}
610-
});
595+
this->getRuntime()->services.timers.setTimeout(32000, [&request] () mutable {
596+
if (request->isActive()) {
597+
auto response = SchemeHandlers::Response(request, 408);
598+
response.fail("ServiceWorker request timed out.");
599+
}
600+
});
611601
return;
612602
}
613603
}

src/runtime/core/services/conduit.cc

+1
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ namespace ssc::runtime::core::services {
467467
}
468468

469469
client->queue.clear();
470+
client->frameBuffer.resize(0);
470471

471472
const auto bytes = vectorToSharedPointer(buffer);
472473
const auto message = ipc::Message(uri);

src/runtime/core/services/permissions.cc

+4
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,11 @@ namespace ssc::runtime::core::services {
260260
}
261261

262262
if (name == "geolocation") {
263+
#if SOCKET_RUNTIME_PLATFORM_APPLE
263264
__block JSON::Object json;
265+
#else
266+
JSON::Object json;
267+
#endif
264268

265269
#if SOCKET_RUNTIME_PLATFORM_APPLE
266270
const auto performedActivation = [this->services.geolocation.locationObserver attemptActivationWithCompletion: ^(BOOL isAuthorized) {

src/runtime/http.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ namespace ssc::runtime::http {
3838
Value (int64_t);
3939
Value (uint64_t);
4040
Value (double_t);
41-
Value (size_t value);
4241
#if SOCKET_RUNTIME_PLATFORM_APPLE
42+
Value (size_t value);
4343
Value (ssize_t value);
4444
#endif
4545

src/runtime/http/headers.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ namespace ssc::runtime::http {
234234
this->string = std::to_string(value);
235235
}
236236

237+
#if SOCKET_RUNTIME_PLATFORM_APPLE
237238
Headers::Value::Value (size_t value) {
238239
this->string = std::to_string(value);
239240
}
240241

241-
#if SOCKET_RUNTIME_PLATFORM_APPLE
242242
Headers::Value::Value (ssize_t value) {
243243
this->string = std::to_string(value);
244244
}

src/runtime/serviceworker/container.cc

+11-4
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,18 @@ namespace ssc::runtime::serviceworker {
239239
fetch->response.body = bytes::Buffer::from(html);
240240
}
241241

242-
fetch->callback(fetch->response);
243-
reply(ipc::Result { message.seq, message });
242+
do {
243+
Lock lock(this->mutex);
244+
this->fetches.erase(id);
245+
} while (0);
246+
247+
this->bridge->dispatch([=](){
248+
fetch->callback(fetch->response);
249+
});
244250

245-
Lock lock(this->mutex);
246-
this->fetches.erase(id);
251+
this->bridge->dispatch([=](){
252+
reply(ipc::Result { message.seq, message });
253+
});
247254
});
248255
}
249256

src/runtime/serviceworker/fetch.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ namespace ssc::runtime::serviceworker {
2727
return false;
2828
}
2929

30-
this->callback = callback;
30+
if (callback != nullptr) {
31+
this->callback = std::move(callback);
32+
}
3133

3234
for (const auto& entry : this->container.registrations) {
3335
const auto& registration = entry.second;
@@ -69,10 +71,10 @@ namespace ssc::runtime::serviceworker {
6971
)
7072
) {
7173
auto runtime = this->container.bridge->getRuntime();
72-
runtime->dispatch([this, runtime, callback, &registration]() {
73-
const auto interval = runtime->services.timers.setInterval(8, [this, runtime, callback, &registration] (auto cancel) {
74+
runtime->dispatch([this, runtime, &registration]() {
75+
const auto interval = runtime->services.timers.setInterval(8, [this, runtime, &registration] (auto cancel) {
7476
if (registration.state == Registration::State::Activated) {
75-
if (!this->init(callback)) {
77+
if (!this->init(this->callback)) {
7678
debug(
7779
#if SOCKET_RUNTIME_PLATFORM_APPLE
7880
"ServiceWorkerContainer: Failed to dispatch fetch request '%s %s%s' for client '%llu'",
@@ -114,7 +116,7 @@ namespace ssc::runtime::serviceworker {
114116
{"host", request.url.hostname},
115117
{"scheme", request.url.scheme},
116118
{"pathname", pathname},
117-
{"query", request.url.query},
119+
{"query", request.url.searchParams.str()},
118120
{"headers", request.headers.json()},
119121
{"client", client}
120122
};

src/runtime/url/builder.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace ssc::runtime::url {
7777
}
7878

7979
for (const auto& entry : params) {
80-
this->search += entry.first + "=" + entry.second + "&";
80+
this->search += encodeURIComponent(entry.first) + "=" + encodeURIComponent(entry.second) + "&";
8181
}
8282
}
8383

src/runtime/url/url.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ namespace ssc::runtime::url {
406406
}
407407

408408
const JSON::Object URL::json () const {
409+
const auto search = this->searchParams.str();
409410
return JSON::Object::Entries {
410411
{"href", this->href()},
411412
{"origin", this->origin},
@@ -414,7 +415,7 @@ namespace ssc::runtime::url {
414415
{"password", this->password},
415416
{"hostname", this->hostname},
416417
{"pathname", this->pathname},
417-
{"search", this->search},
418+
{"search", "?" + search},
418419
{"hash", this->hash}
419420
};
420421
}

src/runtime/webview.hh

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ namespace ssc::runtime::webview {
265265
int statusCode = 200;
266266
http::Headers headers;
267267
Client client;
268+
mutable Mutex mutex;
268269

269270
Atomic<bool> finished = false;
270271

src/runtime/webview/navigator.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ namespace ssc::runtime::webview {
229229
gpointer userData
230230
) {
231231
const auto navigator = reinterpret_cast<Navigator*>(userData);
232-
auto window = navigator->bridge->context.getRuntime()->windowManager.getWindowForWebView(webview);
232+
auto window = navigator->bridge.context.getRuntime()->windowManager.getWindowForWebView(webview);
233233

234234
if (!window) {
235235
webkit_policy_decision_ignore(decision);
@@ -412,15 +412,15 @@ namespace ssc::runtime::webview {
412412
for (const auto& entry : this->location.mounts) {
413413
const auto& path = entry.first;
414414
#if SOCKET_RUNTIME_PLATFORM_LINUX
415-
auto webContext = this->bridge->webContext;
415+
auto webContext = this->bridge.webContext;
416416
if (path != wellKnownPaths.home.string()) {
417417
webkit_web_context_add_path_to_sandbox(webContext, path.c_str(), false);
418418
}
419419
#endif
420420
}
421421

422422
#if SOCKET_RUNTIME_PLATFORM_LINUX
423-
auto webContext = this->bridge->webContext;
423+
auto webContext = this->bridge.webContext;
424424
for (const auto& entry : wellKnownPaths.entries()) {
425425
if (filesystem::Resource::isDirectory(entry) && entry != wellKnownPaths.home) {
426426
webkit_web_context_add_path_to_sandbox(webContext, entry.c_str(), false);

0 commit comments

Comments
 (0)