apply essential handler
authorYoungjae Shin <yj99.shin@samsung.com>
Tue, 4 Feb 2020 05:02:46 +0000 (14:02 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 18 Mar 2020 08:53:50 +0000 (17:53 +0900)
common/log.h
supervisor/Action.h
supervisor/EssentialHandler.cpp [new file with mode: 0644]
supervisor/EssentialHandler.h [new file with mode: 0644]
supervisor/ModeCareTaker.cpp
supervisor/ModeCareTaker.h
supervisor/ModeManager.cpp
supervisor/ModeManager.h
supervisor/Supervisor.cpp
unittest/CMakeLists.txt

index b5639866ae8e8148985ddb38ab5840a878a04092..e6b17469ee4c0abaaa765a61b85cfbe784d98780 100644 (file)
 #define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg)
 
 #else /* MDS_DEBUGGING */
-#define API_CALL
-#define API_END
-#define FN_CALL
-#define FN_END
 #define DBG(fmt, arg...)
 #define WARN(fmt, arg...)
 #define ERR(fmt, arg...)
index e39bfe15ff09b156d68dd2a2b202882955b58902..480d9b08952e9f30c2971b5ad86654474fb6549a 100644 (file)
@@ -79,7 +79,7 @@ private:
        std::string privilege;
        bool stopOnErr;
        ActionRestrict restriction;
-       bool locked;
+       bool locked; //Another action(same rule) is restricted(LOCK), It is assigned by ConflictManager
        std::list<ActionObserver*> observers;
 };
 
diff --git a/supervisor/EssentialHandler.cpp b/supervisor/EssentialHandler.cpp
new file mode 100644 (file)
index 0000000..5170df5
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2019-2020 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.
+ */
+#include "EssentialHandler.h"
+
+#include <glib.h>
+#include "mdss.h"
+#include "ModesEx.h"
+
+MODES_NAMESPACE_USE;
+
+ModeManager *EssentialHandler::modeMgr = nullptr;
+
+void EssentialHandler::setModeManager(ModeManager *mgr)
+{
+       modeMgr = mgr;
+}
+
+void EssentialHandler::undoHandler(const std::string &modeName)
+{
+       DBG("undo idler(%s) is added", modeName.c_str());
+
+       g_idle_add(undoIdler, new std::string(modeName));
+}
+
+gboolean EssentialHandler::undoIdler(gpointer data)
+{
+       RETV_IF(NULL == data, G_SOURCE_REMOVE);
+       RETV_IF(nullptr == modeMgr, G_SOURCE_REMOVE);
+
+       std::string &modeName = *(std::string*)data;
+
+       int ret = modeMgr->undoMode(modeName);
+       if (MODES_ERROR_NONE != ret)
+               ERR("undoMode(%s) Fail(%d)", modeName.c_str(), ret);
+
+       delete &modeName;
+       return G_SOURCE_REMOVE;
+}
diff --git a/supervisor/EssentialHandler.h b/supervisor/EssentialHandler.h
new file mode 100644 (file)
index 0000000..ba4a57a
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2019-2020 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.
+ */
+#pragma once
+
+#include <string>
+#include <glib.h>
+#include "mdss.h"
+#include "ModeManager.h"
+
+MODES_NAMESPACE_BEGIN
+
+class EssentialHandler {
+public:
+       static void setModeManager(ModeManager *mgr);
+       static void undoHandler(const std::string &modeName);
+private:
+       static gboolean undoIdler(gpointer data);
+       static ModeManager *modeMgr;
+};
+
+MODES_NAMESPACE_END
index 01ceee5193b161d31dbb3767aef5b002f4f8d2fd..76c0675e32efbad55f6caef0a4bbf697ede00c89 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "mdss.h"
 #include "XMLGenerator.h"
+#include "EssentialHandler.h"
 
 MODES_NAMESPACE_USE;
 
@@ -41,6 +42,9 @@ void ModeCareTaker::pushMode(const Mode &mode)
        savedModes.insert(std::pair<std::string, Mode>(mode.getName(), mode));
        exclusive = (Mode::MODE_EXCLUSIVE == mode.getModeType());
 
+       if (mode.hasEssential())
+               handleEssentialAction(mode);
+
        XMLGenerator gen;
        gen.makeUndoInfoXML(undoDir + "/tizen_" + mode.getName() + MODES_UNDO_FILE_SUFFIX, mode);
 }
@@ -57,6 +61,13 @@ int ModeCareTaker::popMode(const std::string &name, Mode &mode)
        if (0 != remove(filePath.c_str()))
                ERR("remove(%s) Fail(%s)", filePath.c_str(), strerror(errno));
 
+       for (auto it = essentialMap.begin(); it != essentialMap.end(); ) {
+               if (it->second == name)
+                       it = essentialMap.erase(it);
+               else
+                       ++it;
+       }
+
        mode = found->second;
        if (Mode::MODE_EXCLUSIVE == mode.getModeType())
                exclusive = false;
@@ -92,7 +103,7 @@ bool ModeCareTaker::findLockedAction(const std::string &ruleName)
                for (auto actionIt = actionList.begin(); actionIt != actionList.end(); actionIt++)
                        if ((*actionIt)->getRuleName() == ruleName
                                        && (*actionIt)->getRestrict() == Action::LOCK) {
-                               ERR("%s is restricted", ruleName.c_str());
+                               ERR("%s is Locked", ruleName.c_str());
                                return true;
                        }
        }
@@ -113,3 +124,28 @@ bool ModeCareTaker::checkConflictAction(const Mode &mode)
        }
        return stopOnErr;
 }
+
+void ModeCareTaker::handleEssentialAction(const Mode &mode)
+{
+       DBG("Mode has Essential Action(%s)", mode.getName().c_str());
+
+       std::list<std::shared_ptr<Action>> actionList = mode.getActionList();
+       for (auto it = actionList.begin(); it != actionList.end(); it++) {
+               if ((*it)->getRestrict() == Action::ESSENTIAL) {
+                       DBG("Essential Action(%s)", (*it)->getRuleName().c_str());
+                       essentialMap.insert(std::make_pair((*it)->getRuleName(), mode.getName()));
+                       (*it)->attachObserver(this);
+               }
+       }
+}
+
+void ModeCareTaker::update(const std::string &rule)
+{
+       auto found = essentialMap.find(rule);
+       if (essentialMap.end() == found) {
+               ERR("No Mode related with essential rule(%s)", rule.c_str());
+               return;
+       }
+
+       EssentialHandler::undoHandler(found->second);
+}
index 1510152763f822ab0408437a3987a33a868053fd..c5ed7fec89c3351ea3d2e72ce7be298c896b0f3e 100644 (file)
 #include <string>
 #include "mdss.h"
 #include "Mode.h"
+#include "ActionObserver.h"
 
 MODES_NAMESPACE_BEGIN
 
-class ModeCareTaker {
+class ModeCareTaker : public ActionObserver {
 public:
        ModeCareTaker();
        ~ModeCareTaker() = default;
@@ -34,10 +35,12 @@ public:
        bool isSavedMode(const std::string &name);
        bool isExclusive();
        bool checkConflictAction(const Mode &mode);
+       void update(const std::string &rule);
 private:
-       void handleEssentialAction();
+       void handleEssentialAction(const Mode &mode);
        bool findLockedAction(const std::string &ruleName);
        std::map<std::string, Mode> savedModes;
+       std::multimap<std::string, std::string> essentialMap;
        std::string undoDir;
        bool exclusive;
 };
index d2c185ecdad246f561b43a2f94f51c018aeab5ce..359fb7432e39fdb2b52207794f9b118226ace909 100644 (file)
@@ -49,7 +49,6 @@ void ModeManager::setOptions(const std::set<string> &modeDirs, const string &xsd
 void ModeManager::init()
 {
        modeMap.clear();
-       essentialMap.clear();
 
        for (auto it = modeDirList.begin(); it != modeDirList.end(); ++it) {
                makeModeMap(*it);
@@ -80,19 +79,6 @@ void ModeManager::addModeAttributes(ModeParser *parser, const string &path)
        DBG("[%d] modeName : %s, modePath : %s, hidden : %s", modeMap.size(), modeName.c_str(), path.c_str(), hidden? "true": "false");
 }
 
-bool ModeManager::handleEssentialAction(const Mode &mode)
-{
-       bool stopOnErr = false;
-       std::list<std::shared_ptr<Action>> actionList = mode.getActionList();
-       for (auto it = actionList.begin(); it != actionList.end(); it++) {
-               if ((*it)->getRestrict() == Action::ESSENTIAL) {
-                       essentialMap.insert(std::make_pair(mode.getName(), (*it)->getRuleName()));
-                       (*it)->attachObserver(this);
-               }
-       }
-       return stopOnErr;
-}
-
 int ModeManager::applyMode(const string &modeName, ClientPrivilege &priv, bool isTest)
 {
        auto found = modeMap.find(modeName);
@@ -140,8 +126,6 @@ int ModeManager::applyMode(const string &modeName, ClientPrivilege &priv, bool i
                                return ret;
                        }
                        careTaker.pushMode(mode);
-                       if (mode.hasEssential())
-                               handleEssentialAction(mode);
                }
        } catch (ModesEx &e) {
                //Ususally, it is reached by Invalid Mode File
@@ -180,12 +164,6 @@ int ModeManager::undoMode(const string &modeName)
                return ret;
        }
        mode.undo();
-       for (auto it = essentialMap.begin(); it != essentialMap.end(); ) {
-               if (it->second == modeName)
-                       it = essentialMap.erase(it);
-               else
-                       ++it;
-       }
        notifyObservers(modeName, ModeObserver::OFF);
 
        return MODES_ERROR_NONE;
@@ -222,17 +200,6 @@ void ModeManager::notifyObservers(const string &modeName, ModeObserver::ModeStat
                (*it)->update(modeName, state);
 }
 
-void ModeManager::update(const std::string &rule)
-{
-       auto found = essentialMap.find(rule);
-       if (essentialMap.end() == found) {
-               ERR("No Mode related with essential rule(%s)", rule.c_str());
-               throw ModesEx(ModesEx::NO_DATA);
-       }
-
-       undoMode(found->second);
-}
-
 bool ModeManager::makeModeMap(const string &dirPath)
 {
        DBG("dirPath : [%s]", dirPath.c_str());
index 29971d70fbcdd6d32dc8ed887a3e7ee9cc8c6478..82f4adb38b6ef234c1f5f545280ff6f09c3c9493 100644 (file)
@@ -23,7 +23,6 @@
 #include "RuleManager.h"
 #include "ModeCareTaker.h"
 #include "ModeObserver.h"
-#include "ActionObserver.h"
 #include "ClientPrivilege.h"
 
 
@@ -34,7 +33,7 @@
 
 MODES_NAMESPACE_BEGIN
 
-class ModeManager : public ActionObserver {
+class ModeManager {
 public:
        ModeManager(RuleManager &rMgr);
 
@@ -47,7 +46,6 @@ public:
        std::list<std::tuple<std::string, int>> getModes();
        void attachObserver(ModeObserver *obs);
        void detachObserver(ModeObserver *obs);
-       void update(const std::string &rule);
 private:
        bool handleEssentialAction(const Mode &mode);
        void notifyObservers(const std::string &modeName, ModeObserver::ModeState state);
@@ -57,7 +55,6 @@ private:
        ModeParser* getModeParser(const std::string &path);
 
        std::map<std::string, std::pair<std::string, bool>> modeMap;
-       std::map<std::string, std::string> essentialMap;
        std::set<std::string> modeDirList;
        std::string undoDir;
        std::string modeSyntaxFile;
index 90366e15cd8246a1033a1252a33aa0fd075311a1..93add799307d1e71ff5e94a28662659b14473a20 100644 (file)
@@ -19,6 +19,7 @@
 #include "ModesEx.h"
 #include "ModeManager.h"
 #include "RequestHandler.h"
+#include "EssentialHandler.h"
 #include "ArgumentParser.h"
 
 MODES_NAMESPACE_USE;
@@ -32,6 +33,7 @@ void Supervisor::init()
 {
        RequestHandler::setModeManager(&modeMgr);
        RequestHandler::setRuleManager(&ruleMgr);
+       EssentialHandler::setModeManager(&modeMgr);
 
        try {
                clientConn.startDbus();
index 88d3d5b2284b56c932cb543a941983a085f87c0c..335073b6867d0385c8addcd36ffb3cc7ea62e441 100644 (file)
@@ -44,6 +44,7 @@ FILE(GLOB GTEST_MODEMGR_SRCS
                ${SUPERVISOR_DIR}/ModeManager.cpp
                ${SUPERVISOR_DIR}/ConflictManager.cpp
                ${SUPERVISOR_DIR}/ModeCareTaker.cpp
+               ${SUPERVISOR_DIR}/EssentialHandler.cpp
                ${SUPERVISOR_DIR}/ModeXMLParser.cpp
                ${SUPERVISOR_DIR}/UndoInfoParser.cpp
                ${SUPERVISOR_DIR}/Action.cpp
@@ -120,6 +121,9 @@ FILE(GLOB GTEST_CONFLICT_SRCS
                ${SUPERVISOR_DIR}/ConflictManager.cpp
                ${SUPERVISOR_DIR}/Mode.cpp
                ${SUPERVISOR_DIR}/ModeCareTaker.cpp
+               ${SUPERVISOR_DIR}/EssentialHandler.cpp
+               ${SUPERVISOR_DIR}/ModeManager.cpp
+               ${SUPERVISOR_DIR}/UndoInfoParser.cpp
                ${SUPERVISOR_DIR}/XMLGenerator.cpp
                ${SUPERVISOR_DIR}/Action.cpp
                ${SUPERVISOR_DIR}/ModeXMLParser.cpp