Refactor: auto-register services and inline BytecodeService

- add ServiceRegistry and JUICEAGENT_REGISTER_SERVICE macro so each
  service self-registers at static init; initializeAll() no longer
  includes concrete service headers
- move BytecodeService class entirely into its .cpp (no header needed,
  only accessed via IService)
- split ClassFileLoadHook handling into captureBytecodes and
  patchBytecodes listeners, registered in order
This commit is contained in:
xiaozhou233 committed 2026-08-17 18:54:36 +08:00
1 parent 6962c4a477
commit 8e1b8b38e4
4 files changed
+98 -46

No files matched your search

+1 -2
View File
@@ -2,14 +2,13 @@
#include <services/base.hpp>
#include <services/service_manager.hpp>
#include <services/bytecode_service.hpp>
namespace JuiceAgent {
namespace Services {
inline bool initializeAll() {
auto& manager = ServiceManager::getInstance();
manager.registerService<BytecodeService>();
ServiceRegistry::getInstance().instantiateAll(manager);
return manager.initializeAll();
}
@@ -1,24 +0,0 @@
#pragma once
#include <services/base.hpp>
#include <event/event_type.hpp>
namespace JuiceAgent {
namespace Services {
class BytecodeService : public IService {
public:
static BytecodeService& getInstance();
const char* name() const override { return "BytecodeService"; }
bool onInitialize() override;
void onShutdown() override;
private:
void onClassFileLoad(const EventClassFileLoadHook& event);
};
}
}
@@ -3,7 +3,9 @@
#include <services/base.hpp>
#include <algorithm>
#include <functional>
#include <memory>
#include <mutex>
#include <vector>
namespace JuiceAgent {
@@ -28,6 +30,13 @@ public:
return raw;
}
IService* createService(std::unique_ptr<IService> service) {
IService* raw = service.get();
spdlog::debug("[ServiceManager] Registering service: {}", raw->name());
services_.push_back(std::move(service));
return raw;
}
template <typename ServiceT>
ServiceT* getService() const {
const std::type_info& target = typeid(ServiceT);
@@ -67,5 +76,56 @@ private:
std::vector<std::unique_ptr<IService>> services_;
};
// Static self-registration: each service's .cpp uses JUICEAGENT_REGISTER_SERVICE.
class ServiceRegistry {
public:
using Factory = std::function<std::unique_ptr<IService>()>;
static ServiceRegistry& getInstance() {
static ServiceRegistry instance;
return instance;
}
void addFactory(Factory factory) {
std::lock_guard<std::mutex> lock(mutex_);
factories_.push_back(std::move(factory));
}
void instantiateAll(ServiceManager& manager) {
std::lock_guard<std::mutex> lock(mutex_);
for (auto& f : factories_) {
manager.createService(f());
}
}
private:
ServiceRegistry() = default;
std::mutex mutex_;
std::vector<Factory> factories_;
};
template <typename ServiceT>
class ServiceRegistrar {
public:
ServiceRegistrar() {
ServiceRegistry::getInstance().addFactory(
[]() -> std::unique_ptr<IService> {
return std::make_unique<ServiceT>();
}
);
}
};
}
}
#define JUICEAGENT_SERVICE_CONCAT_IMPL(a, b) a##b
#define JUICEAGENT_SERVICE_CONCAT(a, b) JUICEAGENT_SERVICE_CONCAT_IMPL(a, b)
// Put at file scope in the service's .cpp:
// JUICEAGENT_REGISTER_SERVICE(juiceagent::services::MyService)
#define JUICEAGENT_REGISTER_SERVICE(ServiceT) \
namespace { \
const ::JuiceAgent::Services::ServiceRegistrar<ServiceT> \
JUICEAGENT_SERVICE_CONCAT(_juiceagent_service_registrar_, __LINE__); \
}
+37 -20
View File
@@ -1,32 +1,49 @@
#include <services/bytecode_service.hpp>
#include <services/base.hpp>
#include <services/service_manager.hpp>
#include <event/event_type.hpp>
#include <JuiceAgent/Logger.hpp>
namespace JuiceAgent {
namespace Services {
BytecodeService& BytecodeService::getInstance() {
static BytecodeService instance;
return instance;
}
class BytecodeService : public IService {
public:
const char* name() const override { return "BytecodeService"; }
bool BytecodeService::onInitialize() {
listen<BytecodeService, EventClassFileLoadHook>(
EventId::ClassFileLoadHook,
&BytecodeService::onClassFileLoad
);
bool onInitialize() override {
listen<BytecodeService, EventClassFileLoadHook>(
EventId::ClassFileLoadHook,
&BytecodeService::captureBytecodes
);
spdlog::info("[BytecodeService] Initialized");
return true;
}
listen<BytecodeService, EventClassFileLoadHook>(
EventId::ClassFileLoadHook,
&BytecodeService::patchBytecodes
);
void BytecodeService::onShutdown() {
spdlog::info("[BytecodeService] Shutdown");
}
spdlog::info("[BytecodeService] Initialized");
return true;
}
void BytecodeService::onClassFileLoad(const EventClassFileLoadHook& event) {
spdlog::trace("[BytecodeService] ClassFileLoadHook: {}",
event.name ? event.name : "<null>");
}
void onShutdown() override {
spdlog::info("[BytecodeService] Shutdown");
}
private:
void captureBytecodes(const EventClassFileLoadHook& e) {
// TODO: Capture original bytes
spdlog::trace("[BytecodeService] Capture original bytes: {}",
e.name ? e.name : "<null>");
}
void patchBytecodes(const EventClassFileLoadHook& e) {
// TODO: Apply patches
spdlog::trace("[BytecodeService] Apply patches: {}",
e.name ? e.name : "<null>");
}
};
}
}
JUICEAGENT_REGISTER_SERVICE(::JuiceAgent::Services::BytecodeService)