aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/module/GlobalModuleContext.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-02-28 16:32:43 +0000
committersaturneric <[email protected]>2024-02-28 16:32:43 +0000
commit12d70e1792a5b1ff08d4b58fb49fb9e58d6551a8 (patch)
tree1b747d6a3a034814104df6f531077de9c03ecd53 /src/core/module/GlobalModuleContext.cpp
parentfeat: add user agent header when doing http request (diff)
downloadGpgFrontend-12d70e1792a5b1ff08d4b58fb49fb9e58d6551a8.tar.gz
GpgFrontend-12d70e1792a5b1ff08d4b58fb49fb9e58d6551a8.zip
feat: upgrade module system
1. load module and resolve symbols at runtime 2. restrict sdk functions and structures to c style 3. add some core api to support it
Diffstat (limited to 'src/core/module/GlobalModuleContext.cpp')
-rw-r--r--src/core/module/GlobalModuleContext.cpp42
1 files changed, 40 insertions, 2 deletions
diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp
index 9bc4f06b..cf3e134b 100644
--- a/src/core/module/GlobalModuleContext.cpp
+++ b/src/core/module/GlobalModuleContext.cpp
@@ -31,6 +31,7 @@
#include <set>
#include <unordered_map>
#include <unordered_set>
+#include <utility>
#include "core/module/Event.h"
#include "core/module/Module.h"
@@ -49,6 +50,18 @@ class GlobalModuleContext::Impl {
acquired_channel_.insert(kGpgFrontendNonAsciiChannel);
}
+ auto SearchModule(ModuleIdentifier module_id) -> ModulePtr {
+ // Search for the module in the register table.
+ auto module_info_opt = search_module_register_table(module_id);
+ if (!module_info_opt.has_value()) {
+ GF_CORE_LOG_ERROR("cannot find module id {} at register table",
+ module_id);
+ return nullptr;
+ }
+
+ return module_info_opt.value()->module;
+ }
+
auto GetChannel(ModuleRawPtr module) -> int {
// Search for the module in the register table.
auto module_info_opt =
@@ -97,7 +110,7 @@ class GlobalModuleContext::Impl {
return false;
}
- if (!module->Register()) {
+ if (module->Register() != 0) {
GF_CORE_LOG_ERROR("register module {} failed",
module->GetModuleIdentifier());
return false;
@@ -222,6 +235,9 @@ class GlobalModuleContext::Impl {
GF_CORE_LOG_DEBUG("event {}'s current listeners size: {}",
event->GetIdentifier(), listeners_set.size());
+ // register trigger id index table
+ module_on_triggering_events_table_[event->GetTriggerIdentifier()] = event;
+
// Iterate through each listener and execute the corresponding module
for (const auto& listener_module_id : listeners_set) {
// Search for the module's information in the registration table
@@ -272,7 +288,17 @@ class GlobalModuleContext::Impl {
return true;
}
- auto IsModuleActivated(const ModuleIdentifier& m_id) const -> bool {
+ auto SearchEvent(const EventTriggerIdentifier& trigger_id)
+ -> std::optional<EventRefrernce> {
+ if (module_on_triggering_events_table_.find(trigger_id) !=
+ module_on_triggering_events_table_.end()) {
+ return module_on_triggering_events_table_[trigger_id];
+ }
+ return {};
+ }
+
+ [[nodiscard]] auto IsModuleActivated(const ModuleIdentifier& m_id) const
+ -> bool {
auto m = search_module_register_table(m_id);
return m.has_value() && m->get()->activate;
}
@@ -290,6 +316,8 @@ class GlobalModuleContext::Impl {
module_register_table_;
std::map<EventIdentifier, std::unordered_set<ModuleIdentifier>>
module_events_table_;
+ std::map<EventTriggerIdentifier, EventRefrernce>
+ module_on_triggering_events_table_;
std::set<int> acquired_channel_;
TaskRunnerPtr default_task_runner_;
@@ -323,6 +351,11 @@ GlobalModuleContext::GlobalModuleContext()
GlobalModuleContext::~GlobalModuleContext() = default;
+auto GlobalModuleContext::SearchModule(ModuleIdentifier module_id)
+ -> ModulePtr {
+ return p_->SearchModule(std::move(module_id));
+}
+
// Function to get the task runner associated with a module.
auto GlobalModuleContext::GetTaskRunner(ModuleRawPtr module)
-> std::optional<TaskRunnerPtr> {
@@ -362,6 +395,11 @@ auto GlobalModuleContext::TriggerEvent(EventRefrernce event) -> bool {
return p_->TriggerEvent(std::move(event));
}
+auto GlobalModuleContext::SearchEvent(EventTriggerIdentifier trigger_id)
+ -> std::optional<EventRefrernce> {
+ return p_->SearchEvent(trigger_id);
+}
+
auto GlobalModuleContext::GetChannel(ModuleRawPtr module) -> int {
return p_->GetChannel(module);
}