Move result list from context class to handle 31/67431/1
authorKyungwook Tak <k.tak@samsung.com>
Tue, 26 Apr 2016 11:43:34 +0000 (20:43 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Tue, 26 Apr 2016 11:44:53 +0000 (20:44 +0900)
Change-Id: I47b3688b2d5e3170985d23613c6f18521c6d1ee3
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/CMakeLists.txt
src/framework/client/async-logic.cpp
src/framework/client/async-logic.h
src/framework/client/content-screening.cpp
src/framework/client/handle-ext.cpp
src/framework/client/handle-ext.h
src/framework/client/handle.cpp
src/framework/client/handle.h
src/framework/client/web-protection.cpp
src/framework/common/icontext.cpp [deleted file]
src/framework/common/icontext.h

index ef7eafc..e2c3766 100644 (file)
@@ -33,7 +33,6 @@ SET(${TARGET_CSR_COMMON}_SRCS
        common/wp-context.cpp
        common/wp-result.cpp
        common/kvp-container.cpp
-       common/icontext.cpp
        common/dispatcher.cpp
        common/mainloop.cpp
        common/service.cpp
index 20c6d40..1662e1e 100644 (file)
 namespace Csr {
 namespace Client {
 
-AsyncLogic::AsyncLogic(ContextShPtr &context, const Callback &cb,
-                                          void *userdata, const std::function<bool()> &isStopped) :
-       m_origCtx(context),
+AsyncLogic::AsyncLogic(HandleExt *handle, void *userdata,
+                                          const std::function<bool()> &isStopped) :
+       m_handle(handle),
        m_ctx(new CsContext),
-       m_cb(cb),
+       m_cb(handle->m_cb),
        m_userdata(userdata),
        m_isStopped(isStopped),
        m_dispatcher(new Dispatcher("/tmp/." SERVICE_NAME ".socket"))
@@ -46,37 +46,30 @@ AsyncLogic::AsyncLogic(ContextShPtr &context, const Callback &cb,
 
 AsyncLogic::~AsyncLogic()
 {
-       DEBUG("AsyncLogic dtor. Results num in "
-                 "mother ctx[" << m_origCtx->size() << "] "
-                 "and here[" << m_ctx->size() << "]");
-
-       for (auto &resultPtr : m_ctx->m_results)
-               m_origCtx->add(std::move(resultPtr));
-
-       DEBUG("Integrated mother ctx results num: " << m_origCtx->size());
+       for (auto &resultPtr : m_results)
+               m_handle->add(std::move(resultPtr));
 }
 
-std::pair<Callback::Id, Task> AsyncLogic::scanDirs(const std::shared_ptr<StrSet>
-               &dirs)
+AsyncLogic::Ending AsyncLogic::scanDirs(const std::shared_ptr<StrSet> &dirs)
 {
        // TODO: canonicalize dirs. (e.g. Can omit subdirectory it there is
        //       parent directory in set)
-       std::pair<Callback::Id, Task> t(Callback::Id::OnCompleted, [this] {
+       Ending e(Callback::Id::OnCompleted, [this] {
                if (m_cb.onCompleted)
                        m_cb.onCompleted(this->m_userdata);
        });
 
        for (const auto &dir : *dirs) {
-               t = scanDir(dir);
+               e = scanDir(dir);
 
-               if (t.first != Callback::Id::OnCompleted)
-                       return t;
+               if (e.first != Callback::Id::OnCompleted)
+                       return e;
        }
 
-       return t;
+       return e;
 }
 
-std::pair<Callback::Id, Task> AsyncLogic::scanDir(const std::string &dir)
+AsyncLogic::Ending AsyncLogic::scanDir(const std::string &dir)
 {
        // For in case of there's already detected malware for dir
        auto retResults =
@@ -98,7 +91,7 @@ std::pair<Callback::Id, Task> AsyncLogic::scanDir(const std::string &dir)
 
        // Register already detected malwares to context to be freed with context.
        for (auto r : retResults.second) {
-               add(r);
+               m_results.emplace_back(r);
 
                if (m_cb.onDetected)
                        m_cb.onDetected(m_userdata, reinterpret_cast<csr_cs_detected_h>(r));
@@ -125,8 +118,7 @@ std::pair<Callback::Id, Task> AsyncLogic::scanDir(const std::string &dir)
        return task;
 }
 
-std::pair<Callback::Id, Task> AsyncLogic::scanFiles(const
-               std::shared_ptr<StrSet> &fileSet)
+AsyncLogic::Ending AsyncLogic::scanFiles(const std::shared_ptr<StrSet> &fileSet)
 {
        for (const auto &file : *fileSet) {
                if (m_isStopped()) {
@@ -164,7 +156,7 @@ std::pair<Callback::Id, Task> AsyncLogic::scanFiles(const
 
                // malware detected!
                INFO("[Detected] file[" << file << "]");
-               add(ret.second);
+               m_results.emplace_back(ret.second);
 
                if (m_cb.onDetected)
                        m_cb.onDetected(m_userdata, reinterpret_cast<csr_cs_detected_h>(ret.second));
@@ -178,10 +170,5 @@ std::pair<Callback::Id, Task> AsyncLogic::scanFiles(const
        });
 }
 
-void AsyncLogic::add(IResult *r)
-{
-       m_ctx->add(r);
-}
-
-}
-}
+} // namespace Client
+} // namespace Csr
index 8d0a8d7..de49106 100644 (file)
 #include "common/cs-context.h"
 #include "common/dispatcher.h"
 #include "client/callback.h"
+#include "client/handle-ext.h"
 
 namespace Csr {
 namespace Client {
 
 class AsyncLogic {
 public:
-       AsyncLogic(ContextShPtr &context, const Callback &cb,
-                          void *userdata,
+       using Ending = std::pair<Callback::Id, Task>;
+
+       AsyncLogic(HandleExt *handle, void *userdata,
                           const std::function<bool()> &isStopped);
        virtual ~AsyncLogic();
 
-       std::pair<Callback::Id, Task> scanFiles(const std::shared_ptr<StrSet> &files);
-       std::pair<Callback::Id, Task> scanDir(const std::string &dir);
-       std::pair<Callback::Id, Task> scanDirs(const std::shared_ptr<StrSet> &dirs);
+       Ending scanFiles(const std::shared_ptr<StrSet> &files);
+       Ending scanDir(const std::string &dir);
+       Ending scanDirs(const std::shared_ptr<StrSet> &dirs);
 
        void stop(void);
 
@@ -49,12 +51,10 @@ private:
        template<typename T>
        void copyKvp(CsContext::Key);
 
-       void add(IResult *);
-
-       ContextShPtr &m_origCtx; // for registering results for auto-release
+       Handle *m_handle; // for registering results for auto-release
 
-       // TODO: append it to handle context when destroyed
        ContextPtr m_ctx;
+       std::vector<ResultPtr> m_results;
 
        Callback m_cb;
        void *m_userdata;
@@ -68,7 +68,7 @@ void AsyncLogic::copyKvp(CsContext::Key key)
 {
        T value;
 
-       m_origCtx->get(static_cast<int>(key), value);
+       m_handle->getContext()->get(static_cast<int>(key), value);
        m_ctx->set(static_cast<int>(key), value);
 }
 
index e0d2785..b60e662 100644 (file)
@@ -224,7 +224,7 @@ int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data,
                return CSR_ERROR_UNKNOWN; // deserialization logic error
 
        if (ret.second->hasValue()) {
-               hExt->add(ret.second);
+               hExt->add(ResultPtr(ret.second));
                *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
        } else {
                *pdetected = nullptr;
@@ -261,7 +261,7 @@ int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path,
                return CSR_ERROR_UNKNOWN; // deserialization logic error
 
        if (ret.second->hasValue()) {
-               hExt->add(ret.second);
+               hExt->add(ResultPtr(ret.second));
                *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
        } else {
                *pdetected = nullptr;
@@ -384,8 +384,7 @@ int csr_cs_scan_files_async(csr_cs_context_h handle, const char *file_paths[],
        }
 
        hExt->dispatchAsync([hExt, user_data, fileSet] {
-               Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data,
-               [&hExt] { return hExt->isStopped(); });
+               Client::AsyncLogic l(hExt, user_data, [&hExt] { return hExt->isStopped(); });
 
                l.scanFiles(fileSet).second();
        });
@@ -407,8 +406,7 @@ int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path,
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
 
        hExt->dispatchAsync([hExt, user_data, dir_path] {
-               Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data,
-               [&hExt] { return hExt->isStopped(); });
+               Client::AsyncLogic l(hExt, user_data, [&hExt] { return hExt->isStopped(); });
 
                l.scanDir(dir_path).second();
        });
@@ -439,8 +437,7 @@ int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char *dir_paths[],
        }
 
        hExt->dispatchAsync([hExt, user_data, dirSet] {
-               Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data,
-               [&hExt] { return hExt->isStopped(); });
+               Client::AsyncLogic l(hExt, user_data, [&hExt] { return hExt->isStopped(); });
 
                l.scanDirs(dirSet).second();
        });
@@ -662,7 +659,7 @@ int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path,
                return CSR_ERROR_UNKNOWN; // deserialization logic error
 
        if (ret.second->hasValue()) {
-               hExt->add(ret.second);
+               hExt->add(ResultPtr(ret.second));
                *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
        } else {
                *pdetected = nullptr;
@@ -754,7 +751,7 @@ int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path,
                return CSR_ERROR_UNKNOWN; // deserialization logic error
 
        if (ret.second->hasValue()) {
-               hExt->add(ret.second);
+               hExt->add(ResultPtr(ret.second));
                *pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
        } else {
                *pdetected = nullptr;
index f06305f..7852520 100644 (file)
@@ -108,6 +108,7 @@ void HandleExt::dispatchAsync(const Task &f)
 
                DEBUG("client async thread done! tid: " << std::this_thread::get_id());
        });
+
        {
                std::lock_guard<std::mutex> l(m_mutex);
                m_workerMap.emplace(t.get_id(), std::move(t));
@@ -138,5 +139,17 @@ HandleExt::Worker &HandleExt::Worker::operator=(HandleExt::Worker &&other)
        return *this;
 }
 
+void HandleExt::add(ResultPtr &&ptr)
+{
+       std::lock_guard<std::mutex> l(m_resultsMutex);
+       m_results.emplace_back(std::forward<ResultPtr>(ptr));
+}
+
+void HandleExt::add(ResultListPtr &&ptr)
+{
+       std::lock_guard<std::mutex> l(m_resultsMutex);
+       m_resultLists.emplace_back(std::forward<ResultListPtr>(ptr));
+}
+
 } // namespace Client
 } // namespace Csr
index 387e6fc..f4c1f24 100644 (file)
@@ -48,6 +48,9 @@ public:
 
        Callback m_cb; // TODO: to refine..
 
+       virtual void add(ResultPtr &&) override;
+       virtual void add(ResultListPtr &&) override;
+
 private:
        struct Worker {
                std::atomic<bool> isDone;
@@ -71,6 +74,7 @@ private:
 
        std::atomic<bool> m_stop;
        std::mutex m_mutex;
+       std::mutex m_resultsMutex;
        std::map<std::thread::id, Worker> m_workerMap;
 };
 
index ee4a5e1..52d11e6 100644 (file)
@@ -28,7 +28,7 @@ namespace Csr {
 namespace Client {
 
 Handle::Handle(ContextShPtr &&context) :
-       m_ctx(std::move(context))
+       m_ctx(std::forward<ContextShPtr>(context))
 {
        if (!m_ctx)
                throw std::logic_error("context shouldn't be null");
@@ -43,20 +43,14 @@ ContextShPtr &Handle::getContext() noexcept
        return m_ctx;
 }
 
-void Handle::add(IResult *result)
+void Handle::add(ResultPtr &&ptr)
 {
-       if (result == nullptr)
-               throw std::logic_error("result shouldn't be null");
-
-       m_ctx->add(result);
+       m_results.emplace_back(std::forward<ResultPtr>(ptr));
 }
 
-void Handle::add(ResultListPtr &&resultListPtr)
+void Handle::add(ResultListPtr &&ptr)
 {
-       if (resultListPtr == nullptr)
-               throw std::logic_error("result list pointer shouldn't be null");
-
-       m_ctx->add(std::forward<ResultListPtr>(resultListPtr));
+       m_resultLists.emplace_back(std::forward<ResultListPtr>(ptr));
 }
 
 } // namespace Client
index 88eecf8..e9e1614 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <utility>
 #include <memory>
+#include <vector>
 
 #include "common/icontext.h"
 #include "common/dispatcher.h"
@@ -38,11 +39,16 @@ public:
        template<typename Type, typename ...Args>
        Type dispatch(Args &&...);
 
-       void add(IResult *);
-       void add(ResultListPtr &&);
+       virtual void add(ResultPtr &&);
+       virtual void add(ResultListPtr &&);
 
        ContextShPtr &getContext(void) noexcept;
 
+protected:
+       // for destroying with context
+       std::vector<ResultPtr> m_results;
+       std::vector<ResultListPtr> m_resultLists;
+
 private:
        std::unique_ptr<Dispatcher> m_dispatcher;
        ContextShPtr m_ctx;
index 0dd8049..4f2de9d 100644 (file)
@@ -118,7 +118,7 @@ int csr_wp_check_url(csr_wp_context_h handle, const char *url,
                return ret.first;
        }
 
-       h->add(ret.second);
+       h->add(ResultPtr(ret.second));
        *presult = reinterpret_cast<csr_wp_check_result_h>(ret.second);
 
        return CSR_ERROR_NONE;
diff --git a/src/framework/common/icontext.cpp b/src/framework/common/icontext.cpp
deleted file mode 100644 (file)
index ac31011..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-/*
- * @file        icontext.cpp
- * @author      Kyungwook Tak (k.tak@samsung.com)
- * @version     1.0
- * @brief       Abstract class of context for both of cs / wp
- */
-#include "common/icontext.h"
-
-#include <stdexcept>
-#include <utility>
-
-#include "common/audit/logger.h"
-
-namespace Csr {
-
-IContext::IContext()
-{
-}
-
-IContext::~IContext()
-{
-}
-
-void IContext::add(ResultPtr &&item)
-{
-       std::lock_guard<std::mutex> l(m_mutex);
-       m_results.emplace_back(std::forward<ResultPtr>(item));
-}
-
-void IContext::add(IResult *item)
-{
-       std::lock_guard<std::mutex> l(m_mutex);
-       m_results.emplace_back(item);
-}
-
-void IContext::add(ResultListPtr &&item)
-{
-       std::lock_guard<std::mutex> l(m_mutex);
-       m_resultLists.emplace_back(std::forward<ResultListPtr>(item));
-}
-
-size_t IContext::size() const
-{
-       std::lock_guard<std::mutex> l(m_mutex);
-       return m_results.size();
-}
-
-} // namespace Csr
index a291860..8c58d31 100644 (file)
@@ -21,9 +21,7 @@
  */
 #pragma once
 
-#include <vector>
 #include <memory>
-#include <mutex>
 
 #include "common/dispatcher.h"
 #include "common/serialization.h"
@@ -38,24 +36,13 @@ using ContextShPtr = std::shared_ptr<IContext>;
 
 class IContext : public ISerializable, public KvpContainer {
 public:
-       IContext();
-       virtual ~IContext();
+       IContext() {}
+       virtual ~IContext() {}
 
        IContext(IContext &&) = delete;
        IContext &operator=(IContext &&) = delete;
        IContext(const IContext &) = delete;
        IContext &operator=(const IContext &) = delete;
-
-       void add(ResultPtr &&);
-       void add(IResult *);
-       void add(ResultListPtr &&);
-       size_t size(void) const;
-       // for destroying with context
-       std::vector<ResultPtr> m_results;
-       std::vector<ResultListPtr> m_resultLists;
-
-private:
-       mutable std::mutex m_mutex;
 };
 
 } // namespace Csr