Keep single instance of loaders and db manager 85/69685/3
authorKyungwook Tak <k.tak@samsung.com>
Mon, 16 May 2016 10:21:35 +0000 (19:21 +0900)
committerkyungwook tak <k.tak@samsung.com>
Tue, 17 May 2016 08:43:57 +0000 (01:43 -0700)
loaders and db manager instance is created in service layer and being
delivered as reference to logics respectively.

loader global init/deinit in ctor/dtor and can be re-plugged by 'reset'
member function. This will be used for switching engine library later.

Change-Id: I76f6cd2571de0a09a9394df5d0c8252b47fa8270
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
14 files changed:
src/framework/service/cs-loader.cpp
src/framework/service/cs-loader.h
src/framework/service/cs-logic.cpp
src/framework/service/cs-logic.h
src/framework/service/em-logic.cpp
src/framework/service/em-logic.h
src/framework/service/server-service.cpp
src/framework/service/server-service.h
src/framework/service/wp-loader.cpp
src/framework/service/wp-loader.h
src/framework/service/wp-logic.cpp
src/framework/service/wp-logic.h
test/internals/test-cs-loader.cpp
test/internals/test-wp-loader.cpp

index 37edb2e..1602552 100644 (file)
@@ -45,23 +45,9 @@ int getValueCstr(std::string &value,
 
 } // namespace anonymous
 
-int CsLoader::globalInit(const std::string &ro_res_dir,
-                                                const std::string &rw_working_dir)
-{
-       if (ro_res_dir.empty() || rw_working_dir.empty())
-               throw std::invalid_argument("cs loader global init");
-
-       return m_pc.fpGlobalInit(ro_res_dir.c_str(), rw_working_dir.c_str());
-}
-
-int CsLoader::globalDeinit()
-{
-       return m_pc.fpGlobalDeinit();
-}
-
 int CsLoader::contextCreate(csre_cs_context_h &c)
 {
-       return m_pc.fpContextCreate(&c);
+       return this->m_pc.fpContextCreate(&c);
 }
 
 int CsLoader::contextDestroy(csre_cs_context_h c)
@@ -69,7 +55,7 @@ int CsLoader::contextDestroy(csre_cs_context_h c)
        if (c == nullptr)
                throw std::invalid_argument("cs loader context destroy");
 
-       return m_pc.fpContextDestroy(c);
+       return this->m_pc.fpContextDestroy(c);
 }
 
 int CsLoader::scanData(csre_cs_context_h c,
@@ -79,7 +65,7 @@ int CsLoader::scanData(csre_cs_context_h c,
        if (c == nullptr || data.empty() || pdetected == nullptr)
                throw std::invalid_argument("cs loader scan data");
 
-       return m_pc.fpScanData(c, data.data(), data.size(), pdetected);
+       return this->m_pc.fpScanData(c, data.data(), data.size(), pdetected);
 }
 
 int CsLoader::scanFile(csre_cs_context_h c, const std::string &filepath,
@@ -88,7 +74,7 @@ int CsLoader::scanFile(csre_cs_context_h c, const std::string &filepath,
        if (c == nullptr || filepath.empty() || pdetected == nullptr)
                throw std::invalid_argument("cs loader scan file");
 
-       return m_pc.fpScanFile(c, filepath.c_str(), pdetected);
+       return this->m_pc.fpScanFile(c, filepath.c_str(), pdetected);
 }
 
 int CsLoader::scanAppOnCloud(csre_cs_context_h c, const std::string &appdir,
@@ -97,7 +83,7 @@ int CsLoader::scanAppOnCloud(csre_cs_context_h c, const std::string &appdir,
        if (c == nullptr || appdir.empty() || pdetected == nullptr)
                throw std::invalid_argument("cs loader scan app on cloud");
 
-       return m_pc.fpScanAppOnCloud(c, appdir.c_str(), pdetected);
+       return this->m_pc.fpScanAppOnCloud(c, appdir.c_str(), pdetected);
 }
 
 int CsLoader::getSeverity(csre_cs_detected_h d,
@@ -106,7 +92,7 @@ int CsLoader::getSeverity(csre_cs_detected_h d,
        if (d == nullptr || pseverity == nullptr)
                throw std::invalid_argument("cs loader get severity");
 
-       return m_pc.fpGetSeverity(d, pseverity);
+       return this->m_pc.fpGetSeverity(d, pseverity);
 }
 
 int CsLoader::getMalwareName(csre_cs_detected_h d, std::string &value)
@@ -115,7 +101,7 @@ int CsLoader::getMalwareName(csre_cs_detected_h d, std::string &value)
                throw std::invalid_argument("cs loader get malware name");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetMalwareName(d, cvalue);
+               return this->m_pc.fpGetMalwareName(d, cvalue);
        });
 }
 
@@ -125,7 +111,7 @@ int CsLoader::getDetailedUrl(csre_cs_detected_h d, std::string &value)
                throw std::invalid_argument("cs loader get detailed url");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetDetailedUrl(d, cvalue);
+               return this->m_pc.fpGetDetailedUrl(d, cvalue);
        });
 }
 
@@ -134,24 +120,24 @@ int CsLoader::getTimestamp(csre_cs_detected_h d, time_t *timestamp)
        if (d == nullptr || timestamp == nullptr)
                throw std::invalid_argument("cs loader get time stamp");
 
-       return m_pc.fpGetTimestamp(d, timestamp);
+       return this->m_pc.fpGetTimestamp(d, timestamp);
 }
 
 int CsLoader::getErrorString(int ec, std::string &value)
 {
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetErrorString(ec, cvalue);
+               return this->m_pc.fpGetErrorString(ec, cvalue);
        });
 }
 
 int CsLoader::getEngineInfo(csre_cs_engine_h &e)
 {
-       return m_pc.fpGetEngineInfo(&e);
+       return this->m_pc.fpGetEngineInfo(&e);
 }
 
 int CsLoader::destroyEngine(csre_cs_engine_h e)
 {
-       return m_pc.fpDestroyEngine(e);
+       return this->m_pc.fpDestroyEngine(e);
 }
 
 int CsLoader::getEngineApiVersion(csre_cs_engine_h e, std::string &value)
@@ -160,7 +146,7 @@ int CsLoader::getEngineApiVersion(csre_cs_engine_h e, std::string &value)
                throw std::invalid_argument("cs loader get error string");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineApiVersion(e, cvalue);
+               return this->m_pc.fpGetEngineApiVersion(e, cvalue);
        });
 }
 
@@ -170,7 +156,7 @@ int CsLoader::getEngineVendor(csre_cs_engine_h e, std::string &value)
                throw std::invalid_argument("cs loader get engine vendor");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineVendor(e, cvalue);
+               return this->m_pc.fpGetEngineVendor(e, cvalue);
        });
 }
 
@@ -180,7 +166,7 @@ int CsLoader::getEngineName(csre_cs_engine_h e, std::string &value)
                throw std::invalid_argument("cs loader get engine name");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineName(e, cvalue);
+               return this->m_pc.fpGetEngineName(e, cvalue);
        });
 }
 
@@ -190,7 +176,7 @@ int CsLoader::getEngineVersion(csre_cs_engine_h e, std::string &value)
                throw std::invalid_argument("cs loader get engine version");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineVersion(e, cvalue);
+               return this->m_pc.fpGetEngineVersion(e, cvalue);
        });
 }
 
@@ -200,7 +186,7 @@ int CsLoader::getEngineDataVersion(csre_cs_engine_h e, std::string &value)
                throw std::invalid_argument("cs loader get engine version");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineDataVersion(e, cvalue);
+               return this->m_pc.fpGetEngineDataVersion(e, cvalue);
        });
 }
 
@@ -209,7 +195,7 @@ int CsLoader::getEngineLatestUpdateTime(csre_cs_engine_h e, time_t *ptime)
        if (e == nullptr || ptime == nullptr)
                throw std::invalid_argument("cs loader get latest update time");
 
-       return m_pc.fpGetEngineLatestUpdateTime(e, ptime);
+       return this->m_pc.fpGetEngineLatestUpdateTime(e, ptime);
 }
 
 int CsLoader::getEngineActivated(csre_cs_engine_h e,
@@ -218,7 +204,7 @@ int CsLoader::getEngineActivated(csre_cs_engine_h e,
        if (e == nullptr || pactivated == nullptr)
                throw std::invalid_argument("cs loader get engine activated");
 
-       return m_pc.fpGetEngineActivated(e, pactivated);
+       return this->m_pc.fpGetEngineActivated(e, pactivated);
 }
 
 int CsLoader::getEngineVendorLogo(csre_cs_engine_h e,
@@ -229,7 +215,7 @@ int CsLoader::getEngineVendorLogo(csre_cs_engine_h e,
 
        unsigned char *cvalue = nullptr;
        unsigned int size = 0;
-       auto retval = m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
+       auto retval = this->m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
 
        if (retval == CSRE_ERROR_NONE && cvalue != nullptr && size != 0)
                value.assign(cvalue, cvalue + size);
@@ -237,8 +223,12 @@ int CsLoader::getEngineVendorLogo(csre_cs_engine_h e,
        return retval;
 }
 
-CsLoader::CsLoader(const std::string &enginePath)
+void CsLoader::init(const std::string &enginePath, const std::string &roResDir,
+                                       const std::string &rwWorkingDir)
 {
+       if (enginePath.empty() || roResDir.empty() || rwWorkingDir.empty())
+               ThrowExc(InternalError, "empty string comes in to loader init");
+
        INFO("Load CS-Engine plugin start. engine path: " << enginePath);
 
        void *handle = dlopen(enginePath.c_str(), RTLD_LAZY);
@@ -247,105 +237,147 @@ CsLoader::CsLoader(const std::string &enginePath)
                ThrowExc(InternalError, "engine dlopen error. path: " << enginePath <<
                                 " errno: " << errno);
 
-       m_pc.dlhandle = handle;
-
-       m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
-                                               "csre_cs_global_initialize"));
-       m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
-                                                 "csre_cs_global_deinitialize"));
-       m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
-                                                  "csre_cs_context_create"));
-       m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
-                                                       "csre_cs_context_destroy"));
-       m_pc.fpScanData = reinterpret_cast<FpScanData>(dlsym(handle,
-                                         "csre_cs_scan_data"));
-       m_pc.fpScanFile = reinterpret_cast<FpScanFile>(dlsym(handle,
-                                         "csre_cs_scan_file"));
-       m_pc.fpScanAppOnCloud = reinterpret_cast<FpScanAppOnCloud>(dlsym(handle,
-                                                       "csre_cs_scan_app_on_cloud"));
-       m_pc.fpGetSeverity = reinterpret_cast<FpGetSeverity>(dlsym(handle,
-                                                "csre_cs_detected_get_severity"));
-       m_pc.fpGetMalwareName = reinterpret_cast<FpGetMalwareName>(dlsym(handle,
-                                                       "csre_cs_detected_get_malware_name"));
-       m_pc.fpGetDetailedUrl = reinterpret_cast<FpGetDetailedUrl>(dlsym(handle,
-                                                       "csre_cs_detected_get_detailed_url"));
-       m_pc.fpGetTimestamp = reinterpret_cast<FpGetTimestamp>(dlsym(handle,
-                                                 "csre_cs_detected_get_timestamp"));
-       m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
-                                                       "csre_cs_get_error_string"));
-       m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
-                                                  "csre_cs_engine_get_info"));
-       m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
-                                                  "csre_cs_engine_destroy"));
-       m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
-                                                                        handle, "csre_cs_engine_get_api_version"));
-       m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
-                                                        "csre_cs_engine_get_vendor"));
-       m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
-                                                  "csre_cs_engine_get_name"));
-       m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
-                                                         "csre_cs_engine_get_version"));
-       m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
-                                                                         handle, "csre_cs_engine_get_data_version"));
-       m_pc.fpGetEngineLatestUpdateTime =
+       this->m_pc.dlhandle = handle;
+
+       this->m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
+                                                         "csre_cs_global_initialize"));
+       this->m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
+                                                               "csre_cs_global_deinitialize"));
+       this->m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
+                                                                "csre_cs_context_create"));
+       this->m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
+                                                                 "csre_cs_context_destroy"));
+       this->m_pc.fpScanData = reinterpret_cast<FpScanData>(dlsym(handle,
+                                                       "csre_cs_scan_data"));
+       this->m_pc.fpScanFile = reinterpret_cast<FpScanFile>(dlsym(handle,
+                                                       "csre_cs_scan_file"));
+       this->m_pc.fpScanAppOnCloud = reinterpret_cast<FpScanAppOnCloud>(dlsym(handle,
+                                                                 "csre_cs_scan_app_on_cloud"));
+       this->m_pc.fpGetSeverity = reinterpret_cast<FpGetSeverity>(dlsym(handle,
+                                                          "csre_cs_detected_get_severity"));
+       this->m_pc.fpGetMalwareName = reinterpret_cast<FpGetMalwareName>(dlsym(handle,
+                                                                 "csre_cs_detected_get_malware_name"));
+       this->m_pc.fpGetDetailedUrl = reinterpret_cast<FpGetDetailedUrl>(dlsym(handle,
+                                                                 "csre_cs_detected_get_detailed_url"));
+       this->m_pc.fpGetTimestamp = reinterpret_cast<FpGetTimestamp>(dlsym(handle,
+                                                               "csre_cs_detected_get_timestamp"));
+       this->m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
+                                                                 "csre_cs_get_error_string"));
+       this->m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
+                                                                "csre_cs_engine_get_info"));
+       this->m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
+                                                                "csre_cs_engine_destroy"));
+       this->m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
+                                                                                  handle, "csre_cs_engine_get_api_version"));
+       this->m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
+                                                                  "csre_cs_engine_get_vendor"));
+       this->m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
+                                                                "csre_cs_engine_get_name"));
+       this->m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
+                                                                       "csre_cs_engine_get_version"));
+       this->m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
+                                                                                       handle, "csre_cs_engine_get_data_version"));
+       this->m_pc.fpGetEngineLatestUpdateTime =
                reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
                                "csre_cs_engine_get_latest_update_time"));
-       m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
-                                                               "csre_cs_engine_get_activated"));
-       m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
-                                                                        handle, "csre_cs_engine_get_vendor_logo"));
-
-       if (m_pc.fpGlobalInit == nullptr || m_pc.fpGlobalDeinit == nullptr ||
-                       m_pc.fpContextCreate == nullptr || m_pc.fpContextDestroy == nullptr ||
-                       m_pc.fpScanData == nullptr || m_pc.fpScanFile == nullptr  ||
-                       m_pc.fpScanAppOnCloud == nullptr || m_pc.fpGetSeverity == nullptr ||
-                       m_pc.fpGetMalwareName == nullptr ||
-                       m_pc.fpGetDetailedUrl == nullptr || m_pc.fpGetTimestamp == nullptr ||
-                       m_pc.fpGetErrorString == nullptr || m_pc.fpGetEngineInfo == nullptr ||
-                       m_pc.fpGetEngineApiVersion == nullptr || m_pc.fpGetEngineVendor == nullptr ||
-                       m_pc.fpGetEngineName == nullptr || m_pc.fpGetEngineVersion == nullptr ||
-                       m_pc.fpGetEngineDataVersion == nullptr ||
-                       m_pc.fpGetEngineLatestUpdateTime == nullptr ||
-                       m_pc.fpGetEngineActivated == nullptr || m_pc.fpGetEngineVendorLogo == nullptr) {
-               dlclose(handle);
+       this->m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
+                                                                         "csre_cs_engine_get_activated"));
+       this->m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
+                                                                                  handle, "csre_cs_engine_get_vendor_logo"));
+
+       if (this->m_pc.fpGlobalInit == nullptr || this->m_pc.fpGlobalDeinit == nullptr ||
+                       this->m_pc.fpContextCreate == nullptr ||
+                       this->m_pc.fpContextDestroy == nullptr ||
+                       this->m_pc.fpScanData == nullptr || this->m_pc.fpScanFile == nullptr  ||
+                       this->m_pc.fpScanAppOnCloud == nullptr ||
+                       this->m_pc.fpGetSeverity == nullptr ||
+                       this->m_pc.fpGetMalwareName == nullptr ||
+                       this->m_pc.fpGetDetailedUrl == nullptr ||
+                       this->m_pc.fpGetTimestamp == nullptr ||
+                       this->m_pc.fpGetErrorString == nullptr ||
+                       this->m_pc.fpGetEngineInfo == nullptr ||
+                       this->m_pc.fpGetEngineApiVersion == nullptr ||
+                       this->m_pc.fpGetEngineVendor == nullptr ||
+                       this->m_pc.fpGetEngineName == nullptr ||
+                       this->m_pc.fpGetEngineVersion == nullptr ||
+                       this->m_pc.fpGetEngineDataVersion == nullptr ||
+                       this->m_pc.fpGetEngineLatestUpdateTime == nullptr ||
+                       this->m_pc.fpGetEngineActivated == nullptr ||
+                       this->m_pc.fpGetEngineVendorLogo == nullptr) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
                ThrowExc(EngineError, "Failed to load funcs from engine library. "
                                 "engine path: " << enginePath << "errno: " << errno);
        }
+
+       auto ret = this->m_pc.fpGlobalInit(roResDir.c_str(), rwWorkingDir.c_str());
+
+       if (ret != CSRE_ERROR_NONE) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
+               toException(ret);
+       }
+}
+
+void CsLoader::reset(const std::string &enginePath, const std::string &roResDir,
+                                        const std::string &rwWorkingDir)
+{
+       // ignore return value
+       this->m_pc.fpGlobalDeinit();
+
+       if (this->m_pc.dlhandle) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
+       }
+
+       this->init(enginePath, roResDir, rwWorkingDir);
+}
+
+CsLoader::CsLoader(const std::string &enginePath, const std::string &roResDir,
+                                  const std::string &rwWorkingDir)
+{
+       this->init(enginePath, roResDir, rwWorkingDir);
 }
 
 CsLoader::~CsLoader()
 {
-       dlclose(m_pc.dlhandle);
+       // ignore return value
+       this->m_pc.fpGlobalDeinit();
+
+       if (this->m_pc.dlhandle) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
+       }
 }
 
 CsEngineContext::CsEngineContext(CsLoader &loader) : m_loader(loader), m_context(nullptr)
 {
-       toException(m_loader.contextCreate(m_context));
+       toException(this->m_loader.contextCreate(this->m_context));
 }
 
 CsEngineContext::~CsEngineContext()
 {
-       toException(m_loader.contextDestroy(m_context));
+       toException(this->m_loader.contextDestroy(this->m_context));
 }
 
 csre_cs_context_h &CsEngineContext::get(void)
 {
-       return m_context;
+       return this->m_context;
 }
 
 CsEngineInfo::CsEngineInfo(CsLoader &loader) : m_loader(loader), m_info(nullptr)
 {
-       toException(m_loader.getEngineInfo(m_info));
+       toException(this->m_loader.getEngineInfo(this->m_info));
 }
 
 CsEngineInfo::~CsEngineInfo()
 {
-       toException(m_loader.destroyEngine(m_info));
+       toException(this->m_loader.destroyEngine(this->m_info));
 }
 
 csre_cs_engine_h &CsEngineInfo::get(void)
 {
-       return m_info;
+       return this->m_info;
 }
 
 }
index d70dd4d..590b88d 100644 (file)
@@ -32,10 +32,13 @@ namespace Csr {
 
 class CsLoader {
 public:
-       CsLoader(const std::string &);
+       CsLoader(const std::string &enginePath, const std::string &roResDir,
+                        const std::string &rwWorkingDir);
        virtual ~CsLoader();
-       int globalInit(const std::string &, const std::string &);
-       int globalDeinit();
+
+       void reset(const std::string &enginePath, const std::string &roResDir,
+                          const std::string &rwWorkingDir);
+
        int contextCreate(csre_cs_context_h &);
        int contextDestroy(csre_cs_context_h);
        int scanData(csre_cs_context_h, const std::vector<unsigned char> &,
@@ -117,6 +120,8 @@ private:
                FpGetEngineVendorLogo fpGetEngineVendorLogo;
        };
 
+       void init(const std::string &, const std::string &, const std::string &);
+
        PluginContext m_pc;
 };
 
index 5ad6edb..a47b901 100644 (file)
@@ -52,26 +52,15 @@ void setCoreUsage(const csr_cs_core_usage_e &cu)
 
 } // namespace anonymous
 
-CsLogic::CsLogic() :
-       m_loader(new CsLoader(CS_ENGINE_PATH)),
-       m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE))
+CsLogic::CsLogic(CsLoader &loader, Db::Manager &db) : m_loader(loader), m_db(db)
 {
-       // TODO: Provide engine-specific res/working dirs
-       toException(m_loader->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
-                                                                        SAMPLE_ENGINE_RW_WORKING_DIR));
-
-       CsEngineInfo csEngineInfo(*m_loader);
-       toException(m_loader->getEngineDataVersion(csEngineInfo.get(), m_dataVersion));
+       CsEngineInfo csEngineInfo(this->m_loader);
+       toException(this->m_loader.getEngineDataVersion(csEngineInfo.get(),
+                               this->m_dataVersion));
 }
 
 CsLogic::~CsLogic()
 {
-       try {
-               toException(m_loader->globalDeinit());
-       } catch (const Exception &e) {
-               ERROR("ignore all custom exceptions in logic dtor: " << e.error() <<
-                         " " << e.what());
-       }
 }
 
 RawBuffer CsLogic::scanData(const CsContext &context, const RawBuffer &data)
@@ -80,12 +69,12 @@ RawBuffer CsLogic::scanData(const CsContext &context, const RawBuffer &data)
 
        setCoreUsage(context.coreUsage);
 
-       CsEngineContext engineContext(*m_loader);
+       CsEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
        csre_cs_detected_h result;
 
-       toException(m_loader->scanData(c, data, &result));
+       toException(this->m_loader.scanData(c, data, &result));
 
        // detected handle is null if it's safe
        if (result == nullptr)
@@ -108,11 +97,11 @@ RawBuffer CsLogic::scanAppOnCloud(const CsContext &context,
                                                                  const std::string &pkgPath,
                                                                  const std::string &pkgId)
 {
-       CsEngineContext engineContext(*m_loader);
+       CsEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
        csre_cs_detected_h result;
-       toException(m_loader->scanAppOnCloud(c, pkgPath, &result));
+       toException(this->m_loader.scanAppOnCloud(c, pkgPath, &result));
 
        if (!result)
                return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
@@ -129,19 +118,19 @@ CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::strin
 {
        auto starttime = time(nullptr);
 
-       CsEngineContext engineContext(*this->m_loader);
+       CsEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
        // traverse files in app and take which is more danger than riskiest.
        auto visitor = FsVisitor::create(
-                       pkgPath,
-                       this->m_db->getLastScanTime(pkgPath, this->m_dataVersion));
+                                          pkgPath,
+                                          this->m_db.getLastScanTime(pkgPath, this->m_dataVersion));
 
        CsDetectedPtr riskiest;
 
        while (auto file = visitor->next()) {
                csre_cs_detected_h result;
-               toException(this->m_loader->scanFile(c, file->getPath(), &result));
+               toException(this->m_loader.scanFile(c, file->getPath(), &result));
 
                if (!result)
                        continue;
@@ -151,7 +140,7 @@ CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::strin
                auto candidate = convert(result, file->getPath());
                candidate.isApp = true;
                candidate.pkgId = pkgId;
-               this->m_db->insertDetectedMalware(candidate, this->m_dataVersion);
+               this->m_db.insertDetectedMalware(candidate, this->m_dataVersion);
 
                if (!riskiest)
                        riskiest.reset(new CsDetected(std::move(candidate)));
@@ -159,7 +148,7 @@ CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::strin
                        *riskiest = std::move(candidate);
        }
 
-       this->m_db->insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
+       this->m_db.insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
 
        if (riskiest) {
                INFO("Riskiest malware selected in pkg: " << pkgPath);
@@ -186,25 +175,25 @@ RawBuffer CsLogic::scanApp(const CsContext &context, const std::string &path)
                return this->scanAppOnCloud(context, pkgPath, pkgId);
 
        auto riskiest = this->scanAppDelta(pkgPath, pkgId);
-       auto history = this->m_db->getDetectedMalware(pkgPath);
+       auto history = this->m_db.getDetectedMalware(pkgPath);
 
        if (riskiest && history) {
                if (*riskiest > *history) {
                        INFO("new malware found and more risky! history should be updated.");
-                       this->m_db->insertDetectedMalware(*riskiest, this->m_dataVersion);
+                       this->m_db.insertDetectedMalware(*riskiest, this->m_dataVersion);
                } else {
                        INFO("new malware is found but not riskier than history. history reusable.");
                        history->response = history->isIgnored
-                                       ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
+                                                               ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
                        return this->handleUserResponse(*history);
                }
        } else if (riskiest && !history) {
                INFO("new malware found and no history exist! history should be inserted.");
-               this->m_db->insertDetectedMalware(*riskiest, this->m_dataVersion);
+               this->m_db.insertDetectedMalware(*riskiest, this->m_dataVersion);
        } else if (!riskiest && history) {
                INFO("no malware found and history exist! history reusable.");
                history->response = history->isIgnored
-                               ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
+                                                       ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
                return this->handleUserResponse(*history);
        } else {
                INFO("no malware found and no history exist! it's clean!");
@@ -219,11 +208,11 @@ RawBuffer CsLogic::scanApp(const CsContext &context, const std::string &path)
 RawBuffer CsLogic::scanFileWithoutDelta(const CsContext &context,
                                                                                const std::string &filepath, FilePtr &&fileptr)
 {
-       CsEngineContext engineContext(*m_loader);
+       CsEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
        csre_cs_detected_h result;
-       toException(m_loader->scanFile(c, filepath, &result));
+       toException(this->m_loader.scanFile(c, filepath, &result));
 
        // detected handle is null if it's safe
        if (result == nullptr)
@@ -231,7 +220,7 @@ RawBuffer CsLogic::scanFileWithoutDelta(const CsContext &context,
 
        auto d = convert(result, filepath);
 
-       m_db->insertDetectedMalware(d, m_dataVersion);
+       this->m_db.insertDetectedMalware(d, this->m_dataVersion);
 
        d.response = getUserResponse(context, d);
        return handleUserResponse(d, std::forward<FilePtr>(fileptr));
@@ -248,7 +237,7 @@ RawBuffer CsLogic::scanFile(const CsContext &context, const std::string &filepat
 
        DEBUG("Scan request on file: " << filepath);
 
-       auto history = m_db->getDetectedMalware(filepath);
+       auto history = this->m_db.getDetectedMalware(filepath);
 
        FilePtr fileptr;
 
@@ -263,7 +252,7 @@ RawBuffer CsLogic::scanFile(const CsContext &context, const std::string &filepat
        // OR there's no history at all.
        if (fileptr) {
                if (history)
-                       m_db->deleteDetectedMalware(filepath);
+                       this->m_db.deleteDetectedMalware(filepath);
 
                if (fileptr->isDir())
                        ThrowExc(FileSystemError, "file type shouldn't be directory: " << filepath);
@@ -276,7 +265,7 @@ RawBuffer CsLogic::scanFile(const CsContext &context, const std::string &filepat
        DEBUG("Usable scan history exist on file: " << filepath);
 
        history->response = history->isIgnored
-                       ? CSR_CS_IGNORE : getUserResponse(context, *history);
+                                               ? CSR_CS_IGNORE : getUserResponse(context, *history);
        return handleUserResponse(*history);
 
        EXCEPTION_GUARD_CLOSER(ret)
@@ -316,7 +305,7 @@ RawBuffer CsLogic::getScannableFiles(const std::string &dir)
 {
        EXCEPTION_GUARD_START
 
-       auto lastScanTime = m_db->getLastScanTime(dir, m_dataVersion);
+       auto lastScanTime = this->m_db.getLastScanTime(dir, this->m_dataVersion);
 
        auto visitor = FsVisitor::create(dir, lastScanTime);
 
@@ -335,17 +324,18 @@ RawBuffer CsLogic::getScannableFiles(const std::string &dir)
 
        if (lastScanTime != -1) {
                // for case: scan history exist and not modified.
-               for (auto &row : m_db->getDetectedMalwares(dir)) {
+               for (auto &row : this->m_db.getDetectedMalwares(dir)) {
                        try {
                                auto fileptr = File::create(row->targetName);
+
                                if (fileptr->isInApp())
                                        fileset.insert(fileptr->getAppPkgPath());
                                else
                                        fileset.insert(fileptr->getPath());
                        } catch (const FileDoNotExist &) {
-                               m_db->deleteDetectedMalware(row->targetName);
+                               this->m_db.deleteDetectedMalware(row->targetName);
                        } catch (const FileSystemError &) {
-                               m_db->deleteDetectedMalware(row->targetName);
+                               this->m_db.deleteDetectedMalware(row->targetName);
                        }
                }
        }
@@ -354,7 +344,7 @@ RawBuffer CsLogic::getScannableFiles(const std::string &dir)
        // to set scan time early is safe because file which is modified between
        // scan start time and end time will be traversed by FsVisitor and re-scanned
        // being compared to start time as modified since.
-       m_db->insertLastScanTime(dir, time(nullptr), m_dataVersion);
+       this->m_db.insertLastScanTime(dir, time(nullptr), this->m_dataVersion);
 
        return BinaryQueue::Serialize(CSR_ERROR_NONE, fileset).pop();
 
@@ -369,7 +359,7 @@ RawBuffer CsLogic::judgeStatus(const std::string &filepath, csr_cs_action_e acti
 {
        EXCEPTION_GUARD_START
 
-       auto history = m_db->getDetectedMalware(filepath);
+       auto history = this->m_db.getDetectedMalware(filepath);
 
        if (!history) {
                ERROR("Target to be judged doesn't exist in db. name: " << filepath);
@@ -380,7 +370,7 @@ RawBuffer CsLogic::judgeStatus(const std::string &filepath, csr_cs_action_e acti
 
        if (fileptr) {
                ERROR("Target modified since db delta inserted. name: " << filepath);
-               m_db->deleteDetectedMalware(filepath);
+               this->m_db.deleteDetectedMalware(filepath);
 
                // TODO: is it okay to just refresh db and return success?
                return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
@@ -402,15 +392,15 @@ RawBuffer CsLogic::judgeStatus(const std::string &filepath, csr_cs_action_e acti
                                 "in same path: " << filepath);
                }
 
-               m_db->deleteDetectedMalware(filepath);
+               this->m_db.deleteDetectedMalware(filepath);
                break;
 
        case CSR_CS_ACTION_IGNORE:
-               m_db->setDetectedMalwareIgnored(filepath, true);
+               this->m_db.setDetectedMalwareIgnored(filepath, true);
                break;
 
        case CSR_CS_ACTION_UNIGNORE:
-               m_db->setDetectedMalwareIgnored(filepath, false);
+               this->m_db.setDetectedMalwareIgnored(filepath, false);
                break;
 
        default:
@@ -431,7 +421,7 @@ RawBuffer CsLogic::getDetected(const std::string &filepath)
 {
        EXCEPTION_GUARD_START
 
-       auto row = m_db->getDetectedMalware(filepath);
+       auto row = this->m_db.getDetectedMalware(filepath);
 
        if (row)
                return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
@@ -452,7 +442,7 @@ RawBuffer CsLogic::getDetectedList(const StrSet &dirSet)
        Db::RowShPtrs rows;
        std::for_each(dirSet.begin(), dirSet.end(),
        [this, &rows](const std::string & dir) {
-               for (auto &row : m_db->getDetectedMalwares(dir))
+               for (auto &row : this->m_db.getDetectedMalwares(dir))
                        rows.emplace_back(std::move(row));
        });
 
@@ -473,7 +463,7 @@ RawBuffer CsLogic::getIgnored(const std::string &filepath)
 {
        EXCEPTION_GUARD_START
 
-       auto row = m_db->getDetectedMalware(filepath);
+       auto row = this->m_db.getDetectedMalware(filepath);
 
        if (row && row->isIgnored)
                return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
@@ -494,7 +484,7 @@ RawBuffer CsLogic::getIgnoredList(const StrSet &dirSet)
        Db::RowShPtrs rows;
        std::for_each(dirSet.begin(), dirSet.end(),
        [this, &rows](const std::string & dir) {
-               for (auto &row : m_db->getDetectedMalwares(dir))
+               for (auto &row : this->m_db.getDetectedMalwares(dir))
                        if (row->isIgnored)
                                rows.emplace_back(std::move(row));
        });
@@ -515,7 +505,7 @@ RawBuffer CsLogic::handleUserResponse(const CsDetected &d, FilePtr &&fileptr)
 {
        switch (d.response) {
        case CSR_CS_IGNORE:
-               m_db->setDetectedMalwareIgnored(d.targetName, true);
+               this->m_db.setDetectedMalwareIgnored(d.targetName, true);
                break;
 
        case CSR_CS_REMOVE:
@@ -537,7 +527,7 @@ RawBuffer CsLogic::handleUserResponse(const CsDetected &d, FilePtr &&fileptr)
                        WARN("File type is changed. it's considered as different file: " << d.targetName);
                }
 
-               m_db->deleteDetectedMalware(d.targetName);
+               this->m_db.deleteDetectedMalware(d.targetName);
                break;
 
        case CSR_CS_SKIP:
@@ -599,10 +589,10 @@ CsDetected CsLogic::convert(csre_cs_detected_h &result, const std::string &targe
 
        csre_cs_severity_level_e eseverity = CSRE_CS_SEVERITY_LOW;
 
-       toException(m_loader->getSeverity(result, &eseverity));
-       toException(m_loader->getMalwareName(result, d.malwareName));
-       toException(m_loader->getDetailedUrl(result, d.detailedUrl));
-       toException(m_loader->getTimestamp(result, &d.ts));
+       toException(this->m_loader.getSeverity(result, &eseverity));
+       toException(this->m_loader.getMalwareName(result, d.malwareName));
+       toException(this->m_loader.getDetailedUrl(result, d.detailedUrl));
+       toException(this->m_loader.getTimestamp(result, &d.ts));
 
        d.severity = Csr::convert(eseverity);
 
index 2cd599c..9b27275 100644 (file)
@@ -22,7 +22,6 @@
 #pragma once
 
 #include <string>
-#include <memory>
 
 #include "common/types.h"
 #include "common/cs-context.h"
@@ -38,7 +37,7 @@ namespace Csr {
 
 class CsLogic : public Logic {
 public:
-       CsLogic();
+       CsLogic(CsLoader &loader, Db::Manager &db);
        virtual ~CsLogic();
 
        RawBuffer scanData(const CsContext &context, const RawBuffer &data);
@@ -65,8 +64,8 @@ private:
        static csr_cs_user_response_e getUserResponse(const CsContext &,
                        const CsDetected &);
 
-       std::unique_ptr<CsLoader> m_loader;
-       std::unique_ptr<Db::Manager> m_db;
+       CsLoader &m_loader;
+       Db::Manager &m_db;
 
        std::string m_dataVersion;
 };
index e00fafc..8c53618 100644 (file)
 
 namespace Csr {
 
-// TODO: make loaders to singleton for not being loaded multiple time
-//       make db manager to singleton
-EmLogic::EmLogic() :
-       m_cs(new CsLoader(CS_ENGINE_PATH)),
-       m_wp(new WpLoader(WP_ENGINE_PATH)),
-       m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE))
+EmLogic::EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db) :
+       m_cs(cs), m_wp(wp), m_db(db)
 {
-       // TODO: Provide engine-specific res/working dirs
-       toException(m_cs->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
-                                                                SAMPLE_ENGINE_RW_WORKING_DIR));
-
-       toException(m_wp->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
-                                                                SAMPLE_ENGINE_RW_WORKING_DIR));
 }
 
 EmLogic::~EmLogic()
 {
-       try {
-               toException(m_cs->globalDeinit());
-               toException(m_wp->globalDeinit());
-       } catch (const Exception &e) {
-               ERROR("ignore all custom exceptions in logic dtor: " << e.error() <<
-                         " " << e.what());
-       }
 }
 
-// TODO: make parent class of cs-loader and wp-loader for
-// using engine-manager related engine APIs samely
 RawBuffer EmLogic::getEngineName(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
        if (context.engineId == CSR_ENGINE_CS) {
-               CsEngineInfo engineInfo(*m_cs);
+               CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_cs->getEngineName(c, value));
+               toException(this->m_cs.getEngineName(c, value));
 
                EmString emString;
                emString.value = std::move(value);
                return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
        } else {
-               WpEngineInfo engineInfo(*m_wp);
+               WpEngineInfo engineInfo(this->m_wp);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_wp->getEngineName(c, value));
+               toException(this->m_wp.getEngineName(c, value));
 
                EmString emString;
                emString.value = std::move(value);
@@ -96,21 +77,21 @@ RawBuffer EmLogic::getEngineVendor(const EmContext &context)
        EXCEPTION_GUARD_START
 
        if (context.engineId == CSR_ENGINE_CS) {
-               CsEngineInfo engineInfo(*m_cs);
+               CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_cs->getEngineVendor(c, value));
+               toException(this->m_cs.getEngineVendor(c, value));
 
                EmString emString;
                emString.value = std::move(value);
                return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
        } else {
-               WpEngineInfo engineInfo(*m_wp);
+               WpEngineInfo engineInfo(this->m_wp);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_wp->getEngineVendor(c, value));
+               toException(this->m_wp.getEngineVendor(c, value));
 
                EmString emString;
                emString.value = std::move(value);
@@ -129,21 +110,21 @@ RawBuffer EmLogic::getEngineVersion(const EmContext &context)
        EXCEPTION_GUARD_START
 
        if (context.engineId == CSR_ENGINE_CS) {
-               CsEngineInfo engineInfo(*m_cs);
+               CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_cs->getEngineVersion(c, value));
+               toException(this->m_cs.getEngineVersion(c, value));
 
                EmString emString;
                emString.value = std::move(value);
                return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
        } else {
-               WpEngineInfo engineInfo(*m_wp);
+               WpEngineInfo engineInfo(this->m_wp);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_wp->getEngineVersion(c, value));
+               toException(this->m_wp.getEngineVersion(c, value));
 
                EmString emString;
                emString.value = std::move(value);
@@ -162,21 +143,21 @@ RawBuffer EmLogic::getEngineDataVersion(const EmContext &context)
        EXCEPTION_GUARD_START
 
        if (context.engineId == CSR_ENGINE_CS) {
-               CsEngineInfo engineInfo(*m_cs);
+               CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_cs->getEngineDataVersion(c, value));
+               toException(this->m_cs.getEngineDataVersion(c, value));
 
                EmString emString;
                emString.value = std::move(value);
                return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
        } else {
-               WpEngineInfo engineInfo(*m_wp);
+               WpEngineInfo engineInfo(this->m_wp);
                auto &c = engineInfo.get();
 
                std::string value;
-               toException(m_wp->getEngineDataVersion(c, value));
+               toException(this->m_wp.getEngineDataVersion(c, value));
 
                EmString emString;
                emString.value = std::move(value);
@@ -195,19 +176,19 @@ RawBuffer EmLogic::getEngineUpdatedTime(const EmContext &context)
        EXCEPTION_GUARD_START
 
        if (context.engineId == CSR_ENGINE_CS) {
-               CsEngineInfo engineInfo(*m_cs);
+               CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
 
                time_t value;
-               toException(m_cs->getEngineLatestUpdateTime(c, &value));
+               toException(this->m_cs.getEngineLatestUpdateTime(c, &value));
 
                return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
        } else {
-               WpEngineInfo engineInfo(*m_wp);
+               WpEngineInfo engineInfo(this->m_wp);
                auto &c = engineInfo.get();
 
                time_t value;
-               toException(m_wp->getEngineLatestUpdateTime(c, &value));
+               toException(this->m_wp.getEngineLatestUpdateTime(c, &value));
 
                return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
        }
@@ -226,11 +207,11 @@ RawBuffer EmLogic::getEngineActivated(const EmContext &context)
        csr_activated_e activated = CSR_NOT_ACTIVATED;
 
        if (context.engineId == CSR_ENGINE_CS) {
-               CsEngineInfo engineInfo(*m_cs);
+               CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
 
                csre_cs_activated_e value;
-               toException(m_cs->getEngineActivated(c, &value));
+               toException(this->m_cs.getEngineActivated(c, &value));
 
                if (value == CSRE_CS_ACTIVATED)
                        activated = CSR_ACTIVATED;
@@ -240,11 +221,11 @@ RawBuffer EmLogic::getEngineActivated(const EmContext &context)
                        ThrowExc(EngineError, "Invalid returned activated val: " <<
                                         static_cast<int>(value));
        } else {
-               WpEngineInfo engineInfo(*m_wp);
+               WpEngineInfo engineInfo(this->m_wp);
                auto &c = engineInfo.get();
 
                csre_wp_activated_e value;
-               toException(m_wp->getEngineActivated(c, &value));
+               toException(this->m_wp.getEngineActivated(c, &value));
 
                if (value == CSRE_WP_ACTIVATED)
                        activated = CSR_ACTIVATED;
@@ -268,7 +249,7 @@ RawBuffer EmLogic::getEngineState(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
-       auto state = m_db->getEngineState(static_cast<int>(context.engineId));
+       auto state = this->m_db.getEngineState(static_cast<int>(context.engineId));
 
        if (state == -1)
                ThrowExc(DbFailed, "No engine state exist...");
@@ -286,7 +267,7 @@ RawBuffer EmLogic::setEngineState(const EmContext &context, csr_state_e state)
 {
        EXCEPTION_GUARD_START
 
-       m_db->setEngineState(static_cast<int>(context.engineId), static_cast<int>(state));
+       this->m_db.setEngineState(static_cast<int>(context.engineId), static_cast<int>(state));
 
        return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
 
index 8262824..841551d 100644 (file)
@@ -36,7 +36,7 @@ namespace Csr {
 
 class EmLogic : public Logic {
 public:
-       EmLogic();
+       EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db);
        virtual ~EmLogic();
 
        RawBuffer getEngineName(const EmContext &context);
@@ -49,9 +49,9 @@ public:
        RawBuffer setEngineState(const EmContext &context, csr_state_e state);
 
 private:
-       std::unique_ptr<CsLoader> m_cs;
-       std::unique_ptr<WpLoader> m_wp;
-       std::unique_ptr<Db::Manager> m_db;
+       CsLoader &m_cs;
+       WpLoader &m_wp;
+       Db::Manager &m_db;
 };
 
 }
index af1dd88..ab566d8 100644 (file)
@@ -53,7 +53,15 @@ inline CommandId extractCommandId(BinaryQueue &q)
 
 }
 
-ServerService::ServerService() : Service(), m_workqueue(2, 10)
+ServerService::ServerService() :
+       Service(),
+       m_workqueue(2, 10),
+       m_cs(new CsLoader(CS_ENGINE_PATH, SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR)),
+       m_wp(new WpLoader(WP_ENGINE_PATH, SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR)),
+       m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE)),
+       m_cslogic(*m_cs, *m_db),
+       m_wplogic(*m_wp),
+       m_emlogic(*m_cs, *m_wp, *m_db)
 {
        this->add(SockId::CS);
        this->add(SockId::WP);
index 98d59d1..0d3dca8 100644 (file)
  */
 #pragma once
 
+#include <memory>
+
 #include "common/service.h"
 #include "common/types.h"
 #include "service/thread-pool.h"
 #include "service/cs-logic.h"
 #include "service/wp-logic.h"
 #include "service/em-logic.h"
+#include "service/cs-loader.h"
+#include "service/wp-loader.h"
+#include "db/manager.h"
 
 namespace Csr {
 
@@ -44,9 +49,14 @@ private:
 
        ThreadPool m_workqueue;
 
+       std::unique_ptr<CsLoader> m_cs;
+       std::unique_ptr<WpLoader> m_wp;
+       std::unique_ptr<Db::Manager> m_db;
+
        CsLogic m_cslogic;
        WpLogic m_wplogic;
        EmLogic m_emlogic;
+
 };
 
 }
index 10b95d0..2d1228d 100644 (file)
@@ -45,23 +45,9 @@ int getValueCstr(std::string &value,
 
 } // namespace anonymous
 
-int WpLoader::globalInit(const std::string &ro_res_dir,
-                                                const std::string &rw_working_dir)
-{
-       if (ro_res_dir.empty() || rw_working_dir.empty())
-               throw std::invalid_argument("wp loader global init");
-
-       return m_pc.fpGlobalInit(ro_res_dir.c_str(), rw_working_dir.c_str());
-}
-
-int WpLoader::globalDeinit()
-{
-       return m_pc.fpGlobalDeinit();
-}
-
 int WpLoader::contextCreate(csre_wp_context_h &c)
 {
-       return m_pc.fpContextCreate(&c);
+       return this->m_pc.fpContextCreate(&c);
 }
 
 int WpLoader::contextDestroy(csre_wp_context_h c)
@@ -69,7 +55,7 @@ int WpLoader::contextDestroy(csre_wp_context_h c)
        if (c == nullptr)
                throw std::invalid_argument("wp loader context destroy");
 
-       return m_pc.fpContextDestroy(c);
+       return this->m_pc.fpContextDestroy(c);
 }
 
 int WpLoader::checkUrl(csre_wp_context_h c, const std::string &url,
@@ -78,7 +64,7 @@ int WpLoader::checkUrl(csre_wp_context_h c, const std::string &url,
        if (c == nullptr || url.empty() || presult == nullptr)
                throw std::invalid_argument("wp loader check url error");
 
-       return m_pc.fpCheckUrl(c, (const char *)url.c_str(), presult);
+       return this->m_pc.fpCheckUrl(c, (const char *)url.c_str(), presult);
 }
 
 int WpLoader::getRiskLevel(csre_wp_check_result_h r,
@@ -87,7 +73,7 @@ int WpLoader::getRiskLevel(csre_wp_check_result_h r,
        if (r == nullptr || plevel == nullptr)
                throw std::invalid_argument("wp loader Get Risk Level error");
 
-       return m_pc.fpGetRiskLevel(r, plevel);
+       return this->m_pc.fpGetRiskLevel(r, plevel);
 }
 
 int WpLoader::getDetailedUrl(csre_wp_check_result_h r, std::string &value)
@@ -96,7 +82,7 @@ int WpLoader::getDetailedUrl(csre_wp_check_result_h r, std::string &value)
                throw std::invalid_argument("wp loader get detailed url error");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetDetailedUrl(r, cvalue);
+               return this->m_pc.fpGetDetailedUrl(r, cvalue);
        });
 }
 
@@ -104,19 +90,19 @@ int WpLoader::getDetailedUrl(csre_wp_check_result_h r, std::string &value)
 int WpLoader::getErrorString(int ec, std::string &value)
 {
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetErrorString(ec, cvalue);
+               return this->m_pc.fpGetErrorString(ec, cvalue);
        });
 }
 
 
 int WpLoader::getEngineInfo(csre_wp_engine_h &e)
 {
-       return m_pc.fpGetEngineInfo(&e);
+       return this->m_pc.fpGetEngineInfo(&e);
 }
 
 int WpLoader::destroyEngine(csre_wp_engine_h e)
 {
-       return m_pc.fpDestroyEngine(e);
+       return this->m_pc.fpDestroyEngine(e);
 }
 
 int WpLoader::getEngineApiVersion(csre_wp_engine_h e, std::string &value)
@@ -125,7 +111,7 @@ int WpLoader::getEngineApiVersion(csre_wp_engine_h e, std::string &value)
                throw std::invalid_argument("wp loader get error string");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineApiVersion(e, cvalue);
+               return this->m_pc.fpGetEngineApiVersion(e, cvalue);
        });
 }
 
@@ -135,7 +121,7 @@ int WpLoader::getEngineVendor(csre_wp_engine_h e, std::string &value)
                throw std::invalid_argument("wp loader get engine vendor");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineVendor(e, cvalue);
+               return this->m_pc.fpGetEngineVendor(e, cvalue);
        });
 }
 
@@ -145,7 +131,7 @@ int WpLoader::getEngineName(csre_wp_engine_h e, std::string &value)
                throw std::invalid_argument("wp loader get engine name");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineName(e, cvalue);
+               return this->m_pc.fpGetEngineName(e, cvalue);
        });
 }
 
@@ -155,7 +141,7 @@ int WpLoader::getEngineVersion(csre_wp_engine_h e, std::string &value)
                throw std::invalid_argument("wp loader get engine version");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineVersion(e, cvalue);
+               return this->m_pc.fpGetEngineVersion(e, cvalue);
        });
 }
 
@@ -165,7 +151,7 @@ int WpLoader::getEngineDataVersion(csre_wp_engine_h e, std::string &value)
                throw std::invalid_argument("wp loader get engine version");
 
        return getValueCstr(value, [&](const char **cvalue) {
-               return m_pc.fpGetEngineDataVersion(e, cvalue);
+               return this->m_pc.fpGetEngineDataVersion(e, cvalue);
        });
 }
 
@@ -174,7 +160,7 @@ int WpLoader::getEngineLatestUpdateTime(csre_wp_engine_h e, time_t *ptime)
        if (e == nullptr || ptime == nullptr)
                throw std::invalid_argument("wp loader get latest update time");
 
-       return m_pc.fpGetEngineLatestUpdateTime(e, ptime);
+       return this->m_pc.fpGetEngineLatestUpdateTime(e, ptime);
 }
 
 int WpLoader::getEngineActivated(csre_wp_engine_h e,
@@ -183,7 +169,7 @@ int WpLoader::getEngineActivated(csre_wp_engine_h e,
        if (e == nullptr || pactivated == nullptr)
                throw std::invalid_argument("wp loader get engine activated");
 
-       return m_pc.fpGetEngineActivated(e, pactivated);
+       return this->m_pc.fpGetEngineActivated(e, pactivated);
 }
 
 int WpLoader::getEngineVendorLogo(csre_wp_engine_h e,
@@ -194,7 +180,7 @@ int WpLoader::getEngineVendorLogo(csre_wp_engine_h e,
 
        unsigned char *cvalue = nullptr;
        unsigned int size = 0;
-       auto retval = m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
+       auto retval = this->m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
 
        if (retval == CSRE_ERROR_NONE && cvalue != nullptr && size != 0)
                value.assign(cvalue, cvalue + size);
@@ -202,9 +188,11 @@ int WpLoader::getEngineVendorLogo(csre_wp_engine_h e,
        return retval;
 }
 
-WpLoader::WpLoader(const std::string &enginePath)
+void WpLoader::init(const std::string &enginePath, const std::string &roResDir,
+                                       const std::string &rwWorkingDir)
 {
-       INFO("Load wp-Engine plugin start. engine path: " << enginePath);
+       if (enginePath.empty() || roResDir.empty() || rwWorkingDir.empty())
+               ThrowExc(InternalError, "empty string comes in to loader init");
 
        void *handle = dlopen(enginePath.c_str(), RTLD_LAZY);
 
@@ -212,97 +200,118 @@ WpLoader::WpLoader(const std::string &enginePath)
                ThrowExc(InternalError, "engine dlopen error. path: " << enginePath <<
                                 " errno: " << errno);
 
-       m_pc.dlhandle = handle;
-
-       m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
-                                               "csre_wp_global_initialize"));
-       m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
-                                                 "csre_wp_global_deinitialize"));
-       m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
-                                                  "csre_wp_context_create"));
-       m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
-                                                       "csre_wp_context_destroy"));
-       m_pc.fpCheckUrl = reinterpret_cast<FpCheckUrl>(dlsym(handle,
-                                         "csre_wp_check_url"));
-       m_pc.fpGetRiskLevel = reinterpret_cast<FpGetRiskLevel>(dlsym(handle,
-                                                 "csre_wp_result_get_risk_level"));
-       m_pc.fpGetDetailedUrl = reinterpret_cast<FPGetDetailedUrl>(dlsym(handle,
-                                                       "csre_wp_result_get_detailed_url"));
-       m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
-                                                       "csre_wp_get_error_string"));
-       m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
-                                                  "csre_wp_engine_get_info"));
-       m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
-                                                  "csre_wp_engine_destroy"));
-       m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
-                                                                        handle, "csre_wp_engine_get_api_version"));
-       m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
-                                                        "csre_wp_engine_get_vendor"));
-       m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
-                                                  "csre_wp_engine_get_name"));
-       m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
-                                                         "csre_wp_engine_get_version"));
-       m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
-                                                                         handle, "csre_wp_engine_get_data_version"));
-       m_pc.fpGetEngineLatestUpdateTime =
+       this->m_pc.dlhandle = handle;
+
+       this->m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
+                                                         "csre_wp_global_initialize"));
+       this->m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
+                                                               "csre_wp_global_deinitialize"));
+       this->m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
+                                                                "csre_wp_context_create"));
+       this->m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
+                                                                 "csre_wp_context_destroy"));
+       this->m_pc.fpCheckUrl = reinterpret_cast<FpCheckUrl>(dlsym(handle,
+                                                       "csre_wp_check_url"));
+       this->m_pc.fpGetRiskLevel = reinterpret_cast<FpGetRiskLevel>(dlsym(handle,
+                                                               "csre_wp_result_get_risk_level"));
+       this->m_pc.fpGetDetailedUrl = reinterpret_cast<FPGetDetailedUrl>(dlsym(handle,
+                                                                 "csre_wp_result_get_detailed_url"));
+       this->m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
+                                                                 "csre_wp_get_error_string"));
+       this->m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
+                                                                "csre_wp_engine_get_info"));
+       this->m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
+                                                                "csre_wp_engine_destroy"));
+       this->m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
+                                                                                  handle, "csre_wp_engine_get_api_version"));
+       this->m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
+                                                                  "csre_wp_engine_get_vendor"));
+       this->m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
+                                                                "csre_wp_engine_get_name"));
+       this->m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
+                                                                       "csre_wp_engine_get_version"));
+       this->m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
+                                                                                       handle, "csre_wp_engine_get_data_version"));
+       this->m_pc.fpGetEngineLatestUpdateTime =
                reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
                                "csre_wp_engine_get_latest_update_time"));
-       m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
-                                                               "csre_wp_engine_get_activated"));
-       m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
-                                                                        handle, "csre_wp_engine_get_vendor_logo"));
-
-       if (m_pc.fpGlobalInit == nullptr || m_pc.fpGlobalDeinit == nullptr ||
-                       m_pc.fpContextCreate == nullptr || m_pc.fpContextDestroy == nullptr ||
-                       m_pc.fpCheckUrl == nullptr || m_pc.fpGetRiskLevel == nullptr ||
-                       m_pc.fpGetDetailedUrl == nullptr || m_pc.fpGetErrorString == nullptr ||
-                       m_pc.fpGetEngineInfo == nullptr || m_pc.fpDestroyEngine == nullptr ||
-                       m_pc.fpGetEngineApiVersion == nullptr || m_pc.fpGetEngineVendor == nullptr ||
-                       m_pc.fpGetEngineName == nullptr || m_pc.fpGetEngineVersion == nullptr ||
-                       m_pc.fpGetEngineDataVersion == nullptr ||
-                       m_pc.fpGetEngineLatestUpdateTime == nullptr ||
-                       m_pc.fpGetEngineActivated == nullptr || m_pc.fpGetEngineVendorLogo == nullptr) {
-               dlclose(handle);
+       this->m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
+                                                                         "csre_wp_engine_get_activated"));
+       this->m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
+                                                                                  handle, "csre_wp_engine_get_vendor_logo"));
+
+       if (this->m_pc.fpGlobalInit == nullptr || this->m_pc.fpGlobalDeinit == nullptr ||
+                       this->m_pc.fpContextCreate == nullptr || this->m_pc.fpContextDestroy == nullptr ||
+                       this->m_pc.fpCheckUrl == nullptr || this->m_pc.fpGetRiskLevel == nullptr ||
+                       this->m_pc.fpGetDetailedUrl == nullptr || this->m_pc.fpGetErrorString == nullptr ||
+                       this->m_pc.fpGetEngineInfo == nullptr || this->m_pc.fpDestroyEngine == nullptr ||
+                       this->m_pc.fpGetEngineApiVersion == nullptr || this->m_pc.fpGetEngineVendor == nullptr ||
+                       this->m_pc.fpGetEngineName == nullptr || this->m_pc.fpGetEngineVersion == nullptr ||
+                       this->m_pc.fpGetEngineDataVersion == nullptr ||
+                       this->m_pc.fpGetEngineLatestUpdateTime == nullptr ||
+                       this->m_pc.fpGetEngineActivated == nullptr || this->m_pc.fpGetEngineVendorLogo == nullptr) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
                ThrowExc(EngineError, "Failed to load funcs from engine library. "
                                 "engine path: " << enginePath << "errno: " << errno);
        }
+
+       auto ret = this->m_pc.fpGlobalInit(roResDir.c_str(), rwWorkingDir.c_str());
+
+       if (ret != CSRE_ERROR_NONE) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
+               toException(ret);
+       }
+}
+
+WpLoader::WpLoader(const std::string &enginePath, const std::string &roResDir,
+                                  const std::string &rwWorkingDir)
+{
+       this->init(enginePath, roResDir, rwWorkingDir);
 }
 
 WpLoader::~WpLoader()
 {
-       dlclose(m_pc.dlhandle);
+       // ignore return value
+       this->m_pc.fpGlobalDeinit();
+
+       if (this->m_pc.dlhandle) {
+               dlclose(this->m_pc.dlhandle);
+               this->m_pc.dlhandle = nullptr;
+       }
 }
 
 WpEngineContext::WpEngineContext(WpLoader &loader) :
        m_loader(loader), m_context(nullptr)
 {
-       toException(m_loader.contextCreate(m_context));
+       toException(this->m_loader.contextCreate(this->m_context));
 }
 
 WpEngineContext::~WpEngineContext()
 {
-       toException(m_loader.contextDestroy(m_context));
+       toException(this->m_loader.contextDestroy(this->m_context));
 }
 
 csre_wp_context_h &WpEngineContext::get(void)
 {
-       return m_context;
+       return this->m_context;
 }
 
 WpEngineInfo::WpEngineInfo(WpLoader &loader) :
        m_loader(loader), m_info(nullptr)
 {
-       toException(m_loader.getEngineInfo(m_info));
+       toException(this->m_loader.getEngineInfo(this->m_info));
 }
 
 WpEngineInfo::~WpEngineInfo()
 {
-       toException(m_loader.destroyEngine(m_info));
+       toException(this->m_loader.destroyEngine(this->m_info));
 }
 
 csre_wp_engine_h &WpEngineInfo::get(void)
 {
-       return m_info;
+       return this->m_info;
 }
 
 }
index 2810e15..4ad0153 100644 (file)
@@ -32,10 +32,13 @@ namespace Csr {
 
 class WpLoader {
 public:
-       WpLoader(const std::string &);
+       WpLoader(const std::string &enginePath, const std::string &roResDir,
+                        const std::string &rwWorkingDir);
        virtual ~WpLoader();
-       int globalInit(const std::string &, const std::string &);
-       int globalDeinit();
+
+       void reset(const std::string &enginePath, const std::string &roResDir,
+                          const std::string &rwWorkingDir);
+
        int contextCreate(csre_wp_context_h &);
        int contextDestroy(csre_wp_context_h);
        int checkUrl(csre_wp_context_h handle, const std::string &,
@@ -99,6 +102,8 @@ private:
                FpGetEngineVendorLogo fpGetEngineVendorLogo;
        };
 
+       void init(const std::string &, const std::string &, const std::string &);
+
        PluginContext m_pc;
 };
 
index 84a6a2d..a6729ce 100644 (file)
 
 namespace Csr {
 
-WpLogic::WpLogic() : m_loader(new WpLoader(WP_ENGINE_PATH))
+WpLogic::WpLogic(WpLoader &loader) : m_loader(loader)
 {
-       // TODO: Provide engine-specific res/working dirs
-       toException(m_loader->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
-                                                                        SAMPLE_ENGINE_RW_WORKING_DIR));
-
-       WpEngineInfo wpEngineInfo(*m_loader);
-       toException(m_loader->getEngineDataVersion(wpEngineInfo.get(), m_dataVersion));
+       WpEngineInfo wpEngineInfo(this->m_loader);
+       toException(this->m_loader.getEngineDataVersion(wpEngineInfo.get(),
+                               this->m_dataVersion));
 }
 
 WpLogic::~WpLogic()
 {
-       try {
-               toException(m_loader->globalDeinit());
-       } catch (const Exception &e) {
-               ERROR("ignore all custom exceptions in logic dtor: " << e.error() <<
-                         " " << e.what());
-       }
 }
 
 RawBuffer WpLogic::checkUrl(const WpContext &context, const std::string &url)
 {
        EXCEPTION_GUARD_START
 
-       WpEngineContext engineContext(*m_loader);
+       WpEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
        csre_wp_check_result_h result;
-       toException(m_loader->checkUrl(c, url.c_str(), &result));
+       toException(this->m_loader.checkUrl(c, url.c_str(), &result));
 
        auto wr = convert(result);
 
@@ -97,9 +88,8 @@ RawBuffer WpLogic::checkUrl(const WpContext &context, const std::string &url)
        EXCEPTION_GUARD_END
 }
 
-csr_wp_user_response_e WpLogic::getUserResponse(const WpContext &c,
-                                                                                               const std::string &url,
-                                                                                               const WpResult &wr)
+csr_wp_user_response_e WpLogic::getUserResponse(const WpContext &c, const std::string &url,
+               const WpResult &wr)
 {
        if (c.askUser == CSR_WP_NOT_ASK_USER)
                return CSR_WP_NO_ASK_USER;
@@ -126,8 +116,8 @@ WpResult WpLogic::convert(csre_wp_check_result_h &r)
        WpResult wr;
        csre_wp_risk_level_e elevel;
 
-       toException(m_loader->getDetailedUrl(r, wr.detailedUrl));
-       toException(m_loader->getRiskLevel(r, &elevel));
+       toException(this->m_loader.getDetailedUrl(r, wr.detailedUrl));
+       toException(this->m_loader.getRiskLevel(r, &elevel));
        wr.riskLevel = Csr::convert(elevel);
 
        return wr;
index dc58792..1a9ad69 100644 (file)
@@ -22,7 +22,6 @@
 #pragma once
 
 #include <string>
-#include <memory>
 
 #include "common/types.h"
 #include "common/wp-context.h"
@@ -34,7 +33,7 @@ namespace Csr {
 
 class WpLogic : public Logic {
 public:
-       WpLogic();
+       WpLogic(WpLoader &loader);
        virtual ~WpLogic();
 
        RawBuffer checkUrl(const WpContext &context, const std::string &url);
@@ -43,11 +42,10 @@ private:
        WpResult convert(csre_wp_check_result_h &result);
 
        static csr_wp_user_response_e getUserResponse(const WpContext &context,
-                                                                                                 const std::string &url,
-                                                                                                 const WpResult &result);
-
-       std::unique_ptr<WpLoader> m_loader;
+                       const std::string &url,
+                       const WpResult &result);
 
+       WpLoader &m_loader;
        std::string m_dataVersion;
 };
 
index 3e7840c..7ce5552 100644 (file)
@@ -57,16 +57,19 @@ inline void checkDetected(Csr::CsLoader &loader,
 
        std::string malware_name;
        ASSERT_IF(loader.getMalwareName(detected, malware_name), CSRE_ERROR_NONE);
+
        if (expected_malware_name != nullptr)
                ASSERT_IF(malware_name, expected_malware_name);
 
        std::string detailed_url;
        ASSERT_IF(loader.getDetailedUrl(detected, detailed_url), CSRE_ERROR_NONE);
+
        if (expected_detailed_url != nullptr)
                ASSERT_IF(detailed_url, expected_detailed_url);
 
        time_t timestamp;
        ASSERT_IF(loader.getTimestamp(detected, &timestamp), CSRE_ERROR_NONE);
+
        if (expected_timestamp != 0)
                ASSERT_IF(timestamp, expected_timestamp);
 
@@ -91,18 +94,17 @@ struct Handle {
 
 template <>
 struct Handle<csre_cs_context_h> {
-       Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so")
+       Handle() :
+               loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so",
+                          SAMPLE_ENGINE_RO_RES_DIR,
+                          SAMPLE_ENGINE_RW_WORKING_DIR)
        {
-               ASSERT_IF(
-                       loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
-                       CSRE_ERROR_NONE);
                ASSERT_IF(loader.contextCreate(context), CSRE_ERROR_NONE);
        }
 
        ~Handle()
        {
                ASSERT_IF(loader.contextDestroy(context), CSRE_ERROR_NONE);
-               ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
        }
 
        Csr::CsLoader loader;
@@ -111,18 +113,17 @@ struct Handle<csre_cs_context_h> {
 
 template <>
 struct Handle<csre_cs_engine_h> {
-       Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so")
+       Handle() :
+               loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so",
+                          SAMPLE_ENGINE_RO_RES_DIR,
+                          SAMPLE_ENGINE_RW_WORKING_DIR)
        {
-               ASSERT_IF(
-                       loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
-                       CSRE_ERROR_NONE);
                ASSERT_IF(loader.getEngineInfo(context), CSRE_ERROR_NONE);
        }
 
        ~Handle()
        {
                ASSERT_IF(loader.destroyEngine(context), CSRE_ERROR_NONE);
-               ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
        }
 
        Csr::CsLoader loader;
index 58cee25..8737e59 100755 (executable)
@@ -88,18 +88,17 @@ struct Handle {
 
 template <>
 struct Handle<csre_wp_context_h> {
-       Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so")
+       Handle() :
+               loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so",
+                          SAMPLE_ENGINE_RO_RES_DIR,
+                          SAMPLE_ENGINE_RW_WORKING_DIR)
        {
-               ASSERT_IF(
-                       loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
-                       CSRE_ERROR_NONE);
                ASSERT_IF(loader.contextCreate(context), CSRE_ERROR_NONE);
        }
 
        ~Handle()
        {
                ASSERT_IF(loader.contextDestroy(context), CSRE_ERROR_NONE);
-               ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
        }
 
        Csr::WpLoader loader;
@@ -108,18 +107,17 @@ struct Handle<csre_wp_context_h> {
 
 template <>
 struct Handle<csre_wp_engine_h> {
-       Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so")
+       Handle() :
+               loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so",
+                          SAMPLE_ENGINE_RO_RES_DIR,
+                          SAMPLE_ENGINE_RW_WORKING_DIR)
        {
-               ASSERT_IF(
-                       loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
-                       CSRE_ERROR_NONE);
                ASSERT_IF(loader.getEngineInfo(context), CSRE_ERROR_NONE);
        }
 
        ~Handle()
        {
                ASSERT_IF(loader.destroyEngine(context), CSRE_ERROR_NONE);
-               ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
        }
 
        Csr::WpLoader loader;