Skip to content

Commit

Permalink
feat(extension): add sapi_ipc_set_cancellation_handler method
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson authored and jwerle committed Oct 30, 2023
1 parent f006877 commit dd6b12f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
14 changes: 14 additions & 0 deletions include/socket/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,20 @@ extern "C" {
bool finished
);

/**
* When an IPC route is triggered through HTTP, you can set a cancellation
* handler for cleanup if the HTTP request is aborted.
*
* @param result - An IPC request result
* @param handler - The cancellation handler
* @param data - Optional user data
*/
bool sapi_ipc_set_cancellation_handler (
sapi_ipc_result_t* result,
void (*handler)(void*),
void* data
);

/**
* Creates a "reply" for an IPC route request.
* @param result - An IPC request result
Expand Down
14 changes: 14 additions & 0 deletions src/extension/ipc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ bool sapi_ipc_reply (const sapi_ipc_result_t* result) {
return success;
}

bool sapi_ipc_set_cancellation_handler (
sapi_ipc_result_t* result,
void (*handler)(void*),
void* data
) {
if (result == nullptr || !result->message.isHTTP) {
return false;
}
auto message = result->message;
message.cancel = handler;
message.cancel_data = data;
return true;
}

bool sapi_ipc_send_chunk (
sapi_ipc_result_t* result,
const char* chunk,
Expand Down
17 changes: 12 additions & 5 deletions src/ipc/bridge.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <regex>
#include <unordered_map>
#include <set>

#if defined(__APPLE__)
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
Expand Down Expand Up @@ -2379,13 +2378,13 @@ static void registerSchemeHandler (Router *router) {
@implementation SSCIPCSchemeHandler
{
SSC::Mutex mutex;
std::set<Task> tasks;
std::unordered_map<Task, IPC::Message> tasks;
}

- (void) enqueueTask: (Task) task {
- (void) enqueueTask: (Task) task withMessage: (IPC::Message) message {
Lock lock(mutex);
if (task != nullptr && !tasks.contains(task)) {
tasks.insert(task);
tasks.emplace(task, message);
}
}

Expand All @@ -2402,6 +2401,14 @@ static void registerSchemeHandler (Router *router) {
}

- (void) webView: (SSCBridgedWebView*) webview stopURLSchemeTask: (Task) task {
Lock lock(mutex);
if (tasks.contains(task)) {
auto message = tasks[task];
if (message.cancel != nullptr) {
message.cancel(message.cancel_data);
message.cancel = nullptr;
}
}
[self finalizeTask: task];
}

Expand Down Expand Up @@ -2688,7 +2695,7 @@ static void registerSchemeHandler (Router *router) {
body = (char *) data;
}

[self enqueueTask: task];
[self enqueueTask: task withMessage: message];

auto invoked = self.router->invoke(message, body, bufsize, [=](Result result) {
// @TODO Communicate task cancellation to the route, so it can cancel its work.
Expand Down
2 changes: 2 additions & 0 deletions src/ipc/ipc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ namespace SSC::IPC {
Seq seq = "";
Map args;
bool isHTTP = false;
void (*cancel)(void*) = nullptr;
void *cancel_data = nullptr;

Message () = default;
Message (const Message& message);
Expand Down

0 comments on commit dd6b12f

Please sign in to comment.