aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/module/ModuleManager.cpp
blob: f23185434bfff51e59040cda08de8ab724b3b154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
 * Copyright (C) 2021-2024 Saturneric <[email protected]>
 *
 * This file is part of GpgFrontend.
 *
 * GpgFrontend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GpgFrontend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from
 * the gpg4usb project, which is under GPL-3.0-or-later.
 *
 * All the source code of GpgFrontend was modified and released by
 * Saturneric <[email protected]> starting on May 12, 2021.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include "ModuleManager.h"

#include <memory>
#include <utility>

#include "core/function/GlobalSettingStation.h"
#include "core/function/SecureMemoryAllocator.h"
#include "core/function/basic/GpgFunctionObject.h"
#include "core/model/SettingsObject.h"
#include "core/module/GlobalModuleContext.h"
#include "core/module/GlobalRegisterTable.h"
#include "core/module/Module.h"
#include "core/struct/settings_object/ModuleSO.h"
#include "core/thread/Task.h"
#include "core/thread/TaskRunnerGetter.h"
#include "core/utils/MemoryUtils.h"

namespace GpgFrontend::Module {

class ModuleManager::Impl {
 public:
  Impl()
      : gmc_(GpgFrontend::SecureCreateUniqueObject<GlobalModuleContext>()),
        grt_(GpgFrontend::SecureCreateUniqueObject<GlobalRegisterTable>()) {}

  ~Impl() = default;

  auto LoadAndRegisterModule(const QString& module_library_path,
                             bool integrated_module) -> void {
    // give user ability to give up all modules
    auto disable_loading_all_modules =
        GlobalSettingStation::GetInstance()
            .GetSettings()
            .value("basic/disable_loading_all_modules", false)
            .toBool();
    if (disable_loading_all_modules) return;

    Thread::TaskRunnerGetter::GetInstance()
        .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
        ->PostTask(new Thread::Task(
            [=](GpgFrontend::DataObjectPtr) -> int {
              QLibrary module_library(module_library_path);
              if (!module_library.load()) {
                qCWarning(core) << "module manager failed to load module: "
                                << module_library.fileName()
                                << ", reason: " << module_library.errorString();
                return -1;
              }

              auto module = SecureCreateSharedObject<Module>(module_library);
              if (!module->IsGood()) {
                qCWarning(core) << "module manager failed to load module, "
                                   "reason: illegal module: "
                                << module_library.fileName();
                return -1;
              }

              module->SetGPC(gmc_.get());
              if (!gmc_->RegisterModule(module, integrated_module)) return -1;

              const auto module_id = module->GetModuleIdentifier();
              const auto module_hash = module->GetModuleHash();

              SettingsObject so(QString("module.%1.so").arg(module_id));
              ModuleSO module_so(so);

              // reset module settings if necessary
              if (module_so.module_id != module_id ||
                  module_so.module_hash != module_hash) {
                module_so.module_id = module_id;
                module_so.module_hash = module_hash;
                // auto active integrated module by default
                module_so.auto_activate = integrated_module;
                module_so.set_by_user = false;

                so.Store(module_so.ToJson());
              }

              // if this module need auto active
              if (module_so.auto_activate) {
                if (!gmc_->ActiveModule(module_id)) {
                  return -1;
                }
              }

              return 0;
            },
            __func__, nullptr));
  }

  auto SearchModule(ModuleIdentifier module_id) -> ModulePtr {
    return gmc_->SearchModule(std::move(module_id));
  }

  auto ListAllRegisteredModuleID() -> QList<ModuleIdentifier> {
    return gmc_->ListAllRegisteredModuleID();
  }

  void RegisterModule(const ModulePtr& module) {
    Thread::TaskRunnerGetter::GetInstance()
        .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
        ->PostTask(new Thread::Task(
            [=](GpgFrontend::DataObjectPtr) -> int {
              module->SetGPC(gmc_.get());
              return gmc_->RegisterModule(module, false) ? 0 : -1;
            },
            __func__, nullptr));
  }

  void ListenEvent(const ModuleIdentifier& module_id,
                   const EventIdentifier& event_id) {
    Thread::TaskRunnerGetter::GetInstance()
        .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
        ->PostTask(new Thread::Task(
            [=](const GpgFrontend::DataObjectPtr&) -> int {
              gmc_->ListenEvent(module_id, event_id);
              return 0;
            },
            __func__, nullptr));
  }

  void TriggerEvent(const EventReference& event) {
    Thread::TaskRunnerGetter::GetInstance()
        .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
        ->PostTask(new Thread::Task(
            [=](const GpgFrontend::DataObjectPtr&) -> int {
              gmc_->TriggerEvent(event);
              return 0;
            },
            __func__, nullptr));
  }

  auto SearchEvent(EventTriggerIdentifier trigger_id)
      -> std::optional<EventReference> {
    return gmc_->SearchEvent(std::move(trigger_id));
  }

  auto GetModuleListening(ModuleIdentifier module_id)
      -> QList<EventIdentifier> {
    return gmc_->GetModuleListening(std::move(module_id));
  }

  void ActiveModule(const ModuleIdentifier& identifier) {
    Thread::TaskRunnerGetter::GetInstance()
        .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
        ->PostTask(new Thread::Task(
            [=](const GpgFrontend::DataObjectPtr&) -> int {
              gmc_->ActiveModule(identifier);
              return 0;
            },
            __func__, nullptr));
  }

  void DeactivateModule(const ModuleIdentifier& identifier) {
    Thread::TaskRunnerGetter::GetInstance()
        .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
        ->PostTask(new Thread::Task(
            [=](const GpgFrontend::DataObjectPtr&) -> int {
              gmc_->DeactivateModule(identifier);
              return 0;
            },
            __func__, nullptr));
  }

  auto GetTaskRunner(ModuleIdentifier module_id)
      -> std::optional<TaskRunnerPtr> {
    return gmc_->GetTaskRunner(std::move(module_id));
  }

  auto UpsertRTValue(Namespace n, Key k, std::any v) -> bool {
    return grt_->PublishKV(n, k, v);
  }

  auto RetrieveRTValue(Namespace n, Key k) -> std::optional<std::any> {
    return grt_->LookupKV(n, k);
  }

  auto ListenPublish(QObject* o, Namespace n, Key k, LPCallback c) -> bool {
    return grt_->ListenPublish(o, n, k, c);
  }

  auto ListRTChildKeys(const QString& n, const QString& k) -> std::vector<Key> {
    return grt_->ListChildKeys(n, k);
  }

  auto IsModuleActivated(ModuleIdentifier id) -> bool {
    return gmc_->IsModuleActivated(id);
  }

  auto IsIntegratedModule(ModuleIdentifier id) -> bool {
    return gmc_->IsIntegratedModule(id);
  }

  auto GRT() -> GlobalRegisterTable* { return grt_.get(); }

 private:
  static ModuleMangerPtr global_module_manager;
  SecureUniquePtr<GlobalModuleContext> gmc_;
  SecureUniquePtr<GlobalRegisterTable> grt_;
  QList<QLibrary> module_libraries_;
};

auto IsModuleActivate(ModuleIdentifier id) -> bool {
  return ModuleManager::GetInstance().IsModuleActivated(id);
}

auto UpsertRTValue(const QString& namespace_, const QString& key,
                   const std::any& value) -> bool {
  return ModuleManager::GetInstance().UpsertRTValue(namespace_, key,
                                                    std::any(value));
}

auto ListenRTPublishEvent(QObject* o, Namespace n, Key k,
                          LPCallback c) -> bool {
  return ModuleManager::GetInstance().ListenRTPublish(o, n, k, c);
}

auto ListRTChildKeys(const QString& namespace_,
                     const QString& key) -> std::vector<Key> {
  return ModuleManager::GetInstance().ListRTChildKeys(namespace_, key);
}

ModuleManager::ModuleManager(int channel)
    : SingletonFunctionObject<ModuleManager>(channel),
      p_(SecureCreateUniqueObject<Impl>()) {}

ModuleManager::~ModuleManager() = default;

void ModuleManager::LoadModule(QString module_library_path,
                               bool integrated_module) {
  return p_->LoadAndRegisterModule(module_library_path, integrated_module);
}

auto ModuleManager::SearchModule(ModuleIdentifier module_id) -> ModulePtr {
  return p_->SearchModule(std::move(module_id));
}

void ModuleManager::RegisterModule(ModulePtr module) {
  return p_->RegisterModule(module);
}

void ModuleManager::ListenEvent(ModuleIdentifier module,
                                EventIdentifier event) {
  return p_->ListenEvent(module, event);
}

auto ModuleManager::GetModuleListening(ModuleIdentifier module_id)
    -> QList<EventIdentifier> {
  return p_->GetModuleListening(module_id);
}

void ModuleManager::TriggerEvent(EventReference event) {
  return p_->TriggerEvent(event);
}

auto ModuleManager::SearchEvent(EventTriggerIdentifier trigger_id)
    -> std::optional<EventReference> {
  return p_->SearchEvent(std::move(trigger_id));
}

void ModuleManager::ActiveModule(ModuleIdentifier id) {
  return p_->ActiveModule(id);
}

void ModuleManager::DeactivateModule(ModuleIdentifier id) {
  return p_->DeactivateModule(id);
}

auto ModuleManager::GetTaskRunner(ModuleIdentifier id)
    -> std::optional<TaskRunnerPtr> {
  return p_->GetTaskRunner(std::move(id));
}

auto ModuleManager::UpsertRTValue(Namespace n, Key k, std::any v) -> bool {
  return p_->UpsertRTValue(n, k, v);
}

auto ModuleManager::RetrieveRTValue(Namespace n,
                                    Key k) -> std::optional<std::any> {
  return p_->RetrieveRTValue(n, k);
}

auto ModuleManager::ListenRTPublish(QObject* o, Namespace n, Key k,
                                    LPCallback c) -> bool {
  return p_->ListenPublish(o, n, k, c);
}

auto ModuleManager::ListRTChildKeys(const QString& n,
                                    const QString& k) -> std::vector<Key> {
  return p_->ListRTChildKeys(n, k);
}

auto ModuleManager::IsModuleActivated(ModuleIdentifier id) -> bool {
  return p_->IsModuleActivated(id);
}

auto ModuleManager::IsIntegratedModule(ModuleIdentifier id) -> bool {
  return p_->IsIntegratedModule(id);
}

auto ModuleManager::ListAllRegisteredModuleID() -> QList<ModuleIdentifier> {
  return p_->ListAllRegisteredModuleID();
};

auto ModuleManager::GRT() -> GlobalRegisterTable* { return p_->GRT(); }

}  // namespace GpgFrontend::Module