Skip to content

Commit

Permalink
refactor(core,ipc,window,serviceworker): simplify wrangling options
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Jun 13, 2024
1 parent dbd9cdc commit 911dd37
Show file tree
Hide file tree
Showing 24 changed files with 400 additions and 337 deletions.
8 changes: 4 additions & 4 deletions src/app/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ didFailToContinueUserActivityWithType: (NSString*) userActivityType

self.app->windowManager.configure(windowManagerOptions);

auto defaultWindow = self.app->windowManager.createDefaultWindow(WindowOptions {
auto defaultWindow = self.app->windowManager.createDefaultWindow(Window::Options {
.shouldExitApplicationOnClose = true
});

Expand All @@ -218,7 +218,7 @@ didFailToContinueUserActivityWithType: (NSString*) userActivityType
self.app->userConfig["webview_service_worker_mode"] != "hybrid" &&
self.app->userConfig["permissions_allow_service_worker"] != "false"
) {
auto serviceWorkerWindowOptions = WindowOptions {};
auto serviceWorkerWindowOptions = Window::Options {};
auto serviceWorkerUserConfig = self.app->userConfig;
const auto screen = defaultWindow->getScreenSize();

Expand Down Expand Up @@ -1063,7 +1063,7 @@ extern "C" {
app->windowManager.configure(windowManagerOptions);

app->dispatch([=]() {
auto defaultWindow = app->windowManager.createDefaultWindow(WindowOptions {
auto defaultWindow = app->windowManager.createDefaultWindow(Window::Options {
.shouldExitApplicationOnClose = true
});

Expand All @@ -1074,7 +1074,7 @@ extern "C" {
app->userConfig["permissions_allow_service_worker"] != "false"
) {
if (app->windowManager.getWindowStatus(SOCKET_RUNTIME_SERVICE_WORKER_CONTAINER_WINDOW_INDEX) == WindowManager::WINDOW_NONE) {
auto serviceWorkerWindowOptions = WindowOptions {};
auto serviceWorkerWindowOptions = Window::Options {};
auto serviceWorkerUserConfig = app->userConfig;
auto screen = defaultWindow->getScreenSize();

Expand Down
6 changes: 0 additions & 6 deletions src/app/app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ namespace SSC {
void dispatch (Function<void()>);
String getcwd ();
bool hasRuntimePermission (const String& permission) const;

/*
#if SOCKET_RUNTIME_PLATFORM_ANDROID
bool isAndroidPermissionAllowed (const String& permission);
#endif
*/
};
}
#endif
27 changes: 14 additions & 13 deletions src/cli/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1692,22 +1692,23 @@ void run (const String& targetPlatform, Map& settings, const Paths& paths, const
exit(1);
}

struct Option {
struct CommandLineOption {
std::vector<String> aliases;
bool isOptional;
bool shouldHaveValue;
};
using Options = std::vector<Option>;

using CommandLineOptions = Vector<CommandLineOption>;

struct optionsAndEnv {
Map optionsWithValue;
std::unordered_set<String> optionsWithoutValue;
std::vector<String> envs;
Vector<String> envs;
};

optionsAndEnv parseCommandLineOptions (
const std::span<const char*>& options,
const Options& availableOptions,
const CommandLineOptions& availableOptions,
const String& subcommand
) {
optionsAndEnv result;
Expand Down Expand Up @@ -1755,7 +1756,7 @@ optionsAndEnv parseCommandLineOptions (
}

// find option
Option recognizedOption;
CommandLineOption recognizedOption;
bool found = false;
for (const auto option : availableOptions) {
for (const auto alias : option.aliases) {
Expand Down Expand Up @@ -1988,7 +1989,7 @@ int main (const int argc, const char* argv[]) {

auto createSubcommand = [&](
const String& subcommand,
const Options& availableOptions,
const CommandLineOptions& availableOptions,
const bool& needsConfig,
std::function<void(Map, std::unordered_set<String>)> subcommandHandler
) -> void {
Expand Down Expand Up @@ -2187,7 +2188,7 @@ int main (const int argc, const char* argv[]) {

// first flag indicating whether option is optional
// second flag indicating whether option should be followed by a value
Options initOptions = {
CommandLineOptions initOptions = {
{ { "--config", "-C" }, true, false },
{ { "--name", "-n" }, true, true }
};
Expand Down Expand Up @@ -2246,7 +2247,7 @@ int main (const int argc, const char* argv[]) {

// first flag indicating whether option is optional
// second flag indicating whether option should be followed by a value
Options listDevicesOptions = {
CommandLineOptions listDevicesOptions = {
{ { "--platform" }, false, true },
{ { "--ecid" }, true, false },
{ { "--udid" }, true, false },
Expand Down Expand Up @@ -2352,7 +2353,7 @@ int main (const int argc, const char* argv[]) {

// first flag indicating whether option is optional
// second flag indicating whether option should be followed by a value
Options installAppOptions = {
CommandLineOptions installAppOptions = {
{ { "--debug", "-D" }, true, false },
{ { "--device" }, true, true },
{ { "--platform" }, true, true },
Expand Down Expand Up @@ -2571,7 +2572,7 @@ int main (const int argc, const char* argv[]) {

// first flag indicating whether option is optional
// second flag indicating whether option should be followed by a value
Options printBuildDirOptions = {
CommandLineOptions printBuildDirOptions = {
{ { "--platform" }, true, true }, { { "--root" }, true, false}
};

Expand All @@ -2592,7 +2593,7 @@ int main (const int argc, const char* argv[]) {

// first flag indicating whether option is optional
// second flag indicating whether option should be followed by a value
Options runOptions = {
CommandLineOptions runOptions = {
{ { "--platform" }, true, true },
{ { "--prod", "-P" }, true, false },
{ { "--test", "-t" }, true, true },
Expand All @@ -2604,7 +2605,7 @@ int main (const int argc, const char* argv[]) {
{ { "--host"}, true, true }
};

Options buildOptions = {
CommandLineOptions buildOptions = {
{ { "--quiet", "-q" }, true, false },
{ { "--only-build", "-o" }, true, false },
{ { "--run", "-r" }, true, false },
Expand Down Expand Up @@ -7125,7 +7126,7 @@ int main (const int argc, const char* argv[]) {

// first flag indicating whether option is optional
// second flag indicating whether option should be followed by a value
Options setupOptions = {
CommandLineOptions setupOptions = {
{ { "--platform" }, true, true },
{ { "--yes", "-y" }, true, false },
{ { "--quiet", "-q" }, true, false },
Expand Down
14 changes: 9 additions & 5 deletions src/core/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,21 @@ namespace SSC {
static constexpr auto resolution = RELEASE_STRONG_REFERENCE_SHARED_POINTER_BUFFERS_RESOLUTION;
Lock lock(core->mutex);
for (int i = 0; i < core->sharedPointerBuffers.size(); ++i) {
auto& entry = core->sharedPointerBuffers[i];
auto entry = &core->sharedPointerBuffers[i];
if (entry == nullptr) {
continue;
}

// expired
if (entry.ttl <= resolution) {
entry.pointer = nullptr;
entry.ttl = 0;
if (entry->ttl <= resolution) {
entry->pointer = nullptr;
entry->ttl = 0;
if (i == core->sharedPointerBuffers.size() - 1) {
core->sharedPointerBuffers.pop_back();
break;
}
} else {
entry.ttl = entry.ttl - resolution;
entry->ttl = entry->ttl - resolution;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/core.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include "ip.hh"
#include "json.hh"
#include "module.hh"
#include "socket.hh"
#include "options.hh"
#include "post.hh"
#include "resource.hh"
#include "socket.hh"
#include "unique_client.hh"
#include "url.hh"
#include "version.hh"
Expand Down
14 changes: 14 additions & 0 deletions src/core/options.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SOCKET_RUNTIME_CORE_OPTIONS_H
#define SOCKET_RUNTIME_CORE_OPTIONS_H

#include "../platform/types.hh"

namespace SSC {
struct Options {
template <typename T> const T& as () const {
static_assert(std::is_base_of<Options, T>::value);
return *reinterpret_cast<const T*>(this);
}
};
}
#endif
3 changes: 2 additions & 1 deletion src/desktop/extension/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ extern "C" {

if (bridge == nullptr) {
g_object_ref(context);
bridge = std::make_shared<IPC::Bridge>(app->core, app->userConfig);
auto options = IPC::Bridge::Options(app->userConfig);
bridge = std::make_shared<IPC::Bridge>(app->core, options);
bridge->evaluateJavaScriptFunction = [context] (const auto source) {
auto _ = jsc_context_evaluate(context, source.c_str(), source.size());
};
Expand Down
4 changes: 2 additions & 2 deletions src/desktop/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ MAIN {
auto isMinimizable = getProperty("window_minimizable");
auto isClosable = getProperty("window_closable");

auto defaultWindow = app.windowManager.createDefaultWindow(WindowOptions {
auto defaultWindow = app.windowManager.createDefaultWindow(Window::Options {
.minimizable = (isMinimizable == "" || isMinimizable == "true") ? true : false,
.maximizable = (isMaximizable == "" || isMaximizable == "true") ? true : false,
.resizable = getProperty("window_resizable") == "false" ? false : true,
Expand All @@ -1093,7 +1093,7 @@ MAIN {
userConfig["webview_service_worker_mode"] != "hybrid" &&
userConfig["permissions_allow_service_worker"] != "false"
) {
auto serviceWorkerWindowOptions = WindowOptions {};
auto serviceWorkerWindowOptions = Window::Options {};
auto serviceWorkerUserConfig = userConfig;
auto screen = defaultWindow->getScreenSize();

Expand Down
18 changes: 13 additions & 5 deletions src/ipc/bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,18 @@ export default module)S";
}
}
#endif

Bridge::Bridge (SharedPointer<Core> core, Map userConfig)
: core(core),
userConfig(userConfig),
Bridge::Options::Options (
const Map& userConfig,
const Preload::Options& preload
) : userConfig(userConfig),
preload(preload)
{}

Bridge::Bridge (
SharedPointer<Core> core,
const Options& options
) : core(core),
userConfig(options.userConfig),
router(this),
navigator(this),
schemeHandlers(this)
Expand Down Expand Up @@ -644,7 +652,7 @@ export default module)S";
if (resource.mimeType() != "text/html") {
response.send(resource);
} else {
const auto html = this->preload.insertIntoHTML(resource.str(), {
const auto html = this->client.preload.insertIntoHTML(resource.str(), {
.protocolHandlerSchemes = this->navigator.serviceWorker.protocols.getSchemes()
});

Expand Down
19 changes: 14 additions & 5 deletions src/ipc/bridge.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SOCKET_RUNTIME_IPC_BRIDGE_H

#include "../core/core.hh"
#include "../core/options.hh"
#include "../core/webview.hh"

#include "client.hh"
Expand All @@ -18,6 +19,15 @@ namespace SSC::IPC {
using DispatchCallback = Function<void()>;
using DispatchFunction = Function<void(DispatchCallback)>;

struct Options : public SSC::Options {
Map userConfig = {};
Preload::Options preload;
Options (
const Map& userConfig = {},
const Preload::Options& preload = {}
);
};

static Vector<Bridge*> getInstances();

const CoreNetworkStatus::Observer networkStatusObserver;
Expand All @@ -31,24 +41,23 @@ namespace SSC::IPC {
DispatchFunction dispatchFunction = nullptr;

Bluetooth bluetooth;
Client client = {};
Map userConfig;
Navigator navigator;
SchemeHandlers schemeHandlers;
Preload preload;
Router router;
Map userConfig;
SchemeHandlers schemeHandlers;

SharedPointer<Core> core = nullptr;
Client client = {};
uint64_t id = 0;

#if SOCKET_RUNTIME_PLATFORM_ANDROID
bool isAndroidEmulator = false;
#endif

Bridge (SharedPointer<Core>core, const Options& options);
Bridge () = delete;
Bridge (const Bridge&) = delete;
Bridge (Bridge&&) = delete;
Bridge (SharedPointer<Core>core, Map userConfig);
~Bridge ();

Bridge& operator = (const Bridge&) = delete;
Expand Down
3 changes: 2 additions & 1 deletion src/ipc/client.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace SSC::IPC {
struct Client : public UniqueClient {
using ID = Client::ID;
using ID = UniqueClient::ID;
ID id = 0;
IPC::Preload preload = {};
};
}
Expand Down
18 changes: 9 additions & 9 deletions src/ipc/preload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ namespace SSC::IPC {
R"HTML(</script>)HTML"
);

Preload::Preload (const PreloadOptions& options)
const Preload Preload::compile (const Options& options) {
auto preload = Preload(options);
preload.compile();
return preload;
}

Preload::Preload (const Options& options)
: options(options)
{
this->configure();
Expand All @@ -48,7 +54,7 @@ namespace SSC::IPC {
this->configure(this->options);
}

void Preload::configure (const PreloadOptions& options) {
void Preload::configure (const Options& options) {
this->options = options;
this->headers = options.headers;
this->metadata = options.metadata;
Expand Down Expand Up @@ -236,7 +242,7 @@ namespace SSC::IPC {
)JAVASCRIPT",
Map {
{"id", std::to_string(rand64())},
{"clientId", std::to_string(this->options.clientId)}
{"clientId", std::to_string(this->options.client.id)}
}
)));

Expand Down Expand Up @@ -663,10 +669,4 @@ namespace SSC::IPC {

return output;
}

const Preload createPreload (const PreloadOptions& options) {
auto preload = Preload(options);
preload.compile();
return preload;
}
}
Loading

0 comments on commit 911dd37

Please sign in to comment.