Refactor off-line mode detection in client library 59/34459/5
authorRafal Krypa <r.krypa@samsung.com>
Tue, 27 Jan 2015 15:28:48 +0000 (16:28 +0100)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 5 Feb 2015 15:34:27 +0000 (07:34 -0800)
Extract the detection into separate class for easy re-use in client library.
The detection method will get additional logic soon, so having it in one
place will be useful.

Change-Id: I561b582eb044bf8f6aa71f090d790c00b7bb3273
Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
src/client/CMakeLists.txt
src/client/client-offline.cpp [new file with mode: 0644]
src/client/client-security-manager.cpp
src/client/include/client-offline.h [new file with mode: 0644]

index 077b393..4868c54 100644 (file)
@@ -22,6 +22,7 @@ INCLUDE_DIRECTORIES(
 SET(CLIENT_SOURCES
     ${CLIENT_PATH}/client-security-manager.cpp
     ${CLIENT_PATH}/client-common.cpp
+    ${CLIENT_PATH}/client-offline.cpp
     )
 
 ADD_LIBRARY(${TARGET_CLIENT} SHARED ${CLIENT_SOURCES})
diff --git a/src/client/client-offline.cpp b/src/client/client-offline.cpp
new file mode 100644 (file)
index 0000000..115051a
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Rafal Krypa <r.krypa@samsung.com>
+ *
+ *  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        client-offline.cpp
+ * @author      Rafal Krypa <r.krypa@samsung.com>
+ * @version     1.0
+ * @brief       Helper class for client "off-line" mode detection
+ */
+
+#include <dpl/log/log.h>
+#include "client-offline.h"
+
+namespace SecurityManager {
+
+ClientOffline::ClientOffline()
+{
+    offlineMode = false;
+    serviceLock = nullptr;
+    try {
+        serviceLock = new SecurityManager::FileLocker(SecurityManager::SERVICE_LOCK_FILE, false);
+        offlineMode = serviceLock->Locked();
+    } catch (...) {
+        /* Ignore exceptions, assume on-line */
+    }
+
+    if (offlineMode)
+        LogInfo("Working in off-line mode.");
+    else
+        LogInfo("Working in on-line mode.");
+}
+
+ClientOffline::~ClientOffline()
+{
+    if (serviceLock != nullptr)
+        delete serviceLock;
+}
+
+bool ClientOffline::isOffline(void)
+{
+    return offlineMode;
+}
+
+} // namespace SecurityManager
index a11d4c4..f99dd89 100644 (file)
@@ -44,9 +44,9 @@
 #include <client-common.h>
 #include <protocols.h>
 #include <service_impl.h>
-#include <file-lock.h>
-
 #include <security-manager.h>
+#include <client-offline.h>
+
 
 /**
  * Mapping of lib_retcode error codes to theirs strings equivalents
@@ -161,19 +161,11 @@ int security_manager_app_install(const app_inst_req *p_req)
         if (p_req->appId.empty() || p_req->pkgId.empty())
             return SECURITY_MANAGER_ERROR_REQ_NOT_COMPLETE;
 
-        bool offlineMode;
         int retval;
-
-        try {
-            SecurityManager::FileLocker serviceLock(SecurityManager::SERVICE_LOCK_FILE);
-            if ((offlineMode = serviceLock.Locked())) {
-                LogInfo("Working in offline mode.");
-                retval = SecurityManager::ServiceImpl::appInstall(*p_req, geteuid());
-            }
-        } catch (const SecurityManager::FileLocker::Exception::Base &e) {
-            offlineMode = false;
-        }
-        if (!offlineMode) {
+        ClientOffline offlineMode;
+        if (offlineMode.isOffline()) {
+            retval = SecurityManager::ServiceImpl::appInstall(*p_req, geteuid());
+        } else {
             MessageBuffer send, recv;
 
             //put data into buffer
@@ -555,22 +547,16 @@ SECURITY_MANAGER_API
 int security_manager_user_add(const user_req *p_req)
 {
     using namespace SecurityManager;
-    bool offlineMode;
-    MessageBuffer send, recv;
     if (!p_req)
         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
+
     return try_catch([&] {
         int retval;
-        try {
-            SecurityManager::FileLocker serviceLock(SecurityManager::SERVICE_LOCK_FILE);
-            if ((offlineMode = serviceLock.Locked())) {
-                LogInfo("Working in offline mode.");
-                retval = SecurityManager::ServiceImpl::userAdd(p_req->uid, p_req->utype, geteuid());
-            }
-        } catch (const SecurityManager::FileLocker::Exception::Base &e) {
-            offlineMode = false;
-        }
-        if (!offlineMode) {
+        ClientOffline offlineMode;
+        if (offlineMode.isOffline()) {
+            retval = SecurityManager::ServiceImpl::userAdd(p_req->uid, p_req->utype, geteuid());
+        } else {
+            MessageBuffer send, recv;
             //server is working
 
             //put data into buffer
diff --git a/src/client/include/client-offline.h b/src/client/include/client-offline.h
new file mode 100644 (file)
index 0000000..3f2e421
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Rafal Krypa <r.krypa@samsung.com>
+ *
+ *  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        client-common.h
+ * @author      Rafal Krypa <r.krypa@samsung.com>
+ * @version     1.0
+ * @brief       Helper class for client "off-line" mode detection
+ */
+
+#ifndef _SECURITY_MANAGER_OFFLINE_
+#define _SECURITY_MANAGER_OFFLINE_
+
+#include <file-lock.h>
+
+namespace SecurityManager {
+
+class ClientOffline {
+public:
+    ClientOffline();
+    ~ClientOffline();
+    bool isOffline(void);
+
+private:
+    bool offlineMode;
+    SecurityManager::FileLocker *serviceLock;
+};
+
+} // namespace SecurityManager
+
+#endif // _SECURITY_MANAGER_OFFLINE_