sensor: cascade its recording requests if an app is uninstalled 18/77818/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 1 Jul 2016 06:45:42 +0000 (15:45 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 1 Jul 2016 06:45:42 +0000 (15:45 +0900)
Change-Id: I17f2c453896107721cad74b6136b16f4eaf0ba4d
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/sensor/ClientInfo.cpp
src/sensor/ClientInfo.h
src/sensor/SensorProvider.cpp
src/sensor/SensorProvider.h
src/sensor/UninstallMonitor.cpp [new file with mode: 0644]
src/sensor/UninstallMonitor.h [new file with mode: 0644]

index 6a94f01..e1913f7 100644 (file)
 #include <Types.h>
 #include <SensorRecorderTypes.h>
 #include "TypesInternal.h"
+#include "SensorProvider.h"
 #include "ClientInfo.h"
 
 using namespace ctx;
 
 unsigned int ClientInfo::__refCnt = 0;
 DatabaseManager *ClientInfo::__dbMgr = NULL;
+UninstallMonitor *ClientInfo::__uninstallMonitor = NULL;
 
 ClientInfo::ClientInfo()
 {
-       ++__refCnt;
-
-       if (__dbMgr)
+       if (++__refCnt != 1)
                return;
 
+       __uninstallMonitor = new(std::nothrow) UninstallMonitor();
+       IF_FAIL_VOID_TAG(__uninstallMonitor, _E, "Memory allocation failed");
+
        __dbMgr = new(std::nothrow) DatabaseManager();
        IF_FAIL_VOID_TAG(__dbMgr, _E, "Memory allocation failed");
 
@@ -54,6 +57,9 @@ ClientInfo::~ClientInfo()
 
        delete __dbMgr;
        __dbMgr = NULL;
+
+       delete __uninstallMonitor;
+       __uninstallMonitor = NULL;
 }
 
 int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
@@ -98,7 +104,7 @@ int ClientInfo::get(std::string subject, std::vector<Json>& options)
        IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
        IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
 
-       for (auto jObj : records) {
+       for (Json& jObj : records) {
                if (!jObj.get(NULL, KEY_OPTION, &optStr))
                        continue;
                options.push_back(Json(optStr));
@@ -157,3 +163,27 @@ bool ClientInfo::remove(std::string subject, std::string pkgId)
 
        return ret;
 }
+
+void ClientInfo::purgeClient(std::string pkgId)
+{
+       IF_FAIL_VOID_TAG(__dbMgr, _W, "DB not initialized");
+
+       bool ret;
+       std::string subject;
+       std::vector<Json> records;
+
+       char *query = sqlite3_mprintf(
+                       "SELECT " KEY_SUBJECT " FROM " CLIENT_INFO " WHERE " KEY_PKG_ID "='%q'",
+                       pkgId.c_str());
+
+       ret = __dbMgr->executeSync(query, &records);
+       sqlite3_free(query);
+       IF_FAIL_VOID(ret);
+
+       for (Json& jObj : records) {
+               if (!jObj.get(NULL, KEY_SUBJECT, &subject))
+                       continue;
+               _I("Stop recording '%s' for '%s'", subject.c_str(), pkgId.c_str());
+               SensorProvider::removeClient(subject, pkgId);
+       }
+}
index a52cd27..a14dc3c 100644 (file)
@@ -21,6 +21,7 @@
 #include <vector>
 #include <Json.h>
 #include <DatabaseManager.h>
+#include "UninstallMonitor.h"
 
 namespace ctx {
 
@@ -36,9 +37,12 @@ namespace ctx {
                bool set(std::string subject, std::string pkgId, Json option, int retentionPeriod);
                bool remove(std::string subject, std::string pkgId);
 
+               static void purgeClient(std::string pkgId);
+
        private:
                static unsigned int __refCnt;
                static DatabaseManager *__dbMgr;
+               static UninstallMonitor *__uninstallMonitor;
        };
 }
 
index 32f0584..8cc8e1f 100644 (file)
 
 using namespace ctx;
 
+std::map<std::string, SensorProvider*> SensorProvider::__providerMap;
+
 SensorProvider::SensorProvider(const char *subject) :
        ContextProvider(subject),
        sensorLogger(NULL)
 {
+       __providerMap[subject] = this;
 }
 
 SensorProvider::~SensorProvider()
 {
+       __providerMap.erase(getSubject());
        delete sensorLogger;
 }
 
@@ -155,3 +159,8 @@ int SensorProvider::__removeClient(std::string pkgId)
 
        return ERR_NONE;
 }
+
+void SensorProvider::removeClient(std::string subject, std::string pkgId)
+{
+       __providerMap[subject]->__removeClient(pkgId);
+}
index 4a1cf5b..601b191 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __CONTEXT_SENSOR_PROVIDER_H__
 #define __CONTEXT_SENSOR_PROVIDER_H__
 
+#include <map>
 #include <ContextProvider.h>
 #include "ClientInfo.h"
 #include "SensorLogger.h"
@@ -34,6 +35,8 @@ namespace ctx {
                virtual int read(Json option, Json *requestResult);
                virtual int write(Json data, Json *requestResult);
 
+               static void removeClient(std::string subject, std::string pkgId);
+
        protected:
                virtual Querier* getQuerier(Json option) = 0;
 
@@ -44,6 +47,8 @@ namespace ctx {
                int __removeClient(std::string pkgId);
 
                ClientInfo __clientInfo;
+
+               static std::map<std::string, SensorProvider*> __providerMap;
        };
 }
 
diff --git a/src/sensor/UninstallMonitor.cpp b/src/sensor/UninstallMonitor.cpp
new file mode 100644 (file)
index 0000000..b3dc8a5
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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 "ClientInfo.h"
+#include "UninstallMonitor.h"
+
+using namespace ctx;
+
+UninstallMonitor::UninstallMonitor() :
+       __dbusSignalId(-1),
+       __dbusWatcher(DBusType::SYSTEM)
+{
+       __dbusSignalId = __dbusWatcher.watch(NULL,
+                       "/org/tizen/pkgmgr_status", "org.tizen.pkgmgr_status", "status", this);
+}
+
+UninstallMonitor::~UninstallMonitor()
+{
+       if (__dbusSignalId > 0)
+               __dbusWatcher.unwatch(__dbusSignalId);
+}
+
+void UninstallMonitor::onSignal(const char *sender, const char *path, const char *iface, const char *name, GVariant *param)
+{
+       const gchar *reqId = NULL;
+       const gchar *pkgType = NULL;
+       const gchar *pkgId = NULL;
+       const gchar *key = NULL;
+       const gchar *val = NULL;
+
+       g_variant_get(param, "(&s&s&s&s&s)", &reqId, &pkgType, &pkgId, &key, &val);
+       _D("%s, %s, %s", pkgId, key, val);
+
+       IF_FAIL_VOID_TAG(pkgId && key && val, _E, "Invalid parameter");
+
+       if (STR_EQ(key, "start")) {
+               if (STR_EQ(val, "uninstall")) {
+                       __pkgId = pkgId;
+               } else {
+                       __pkgId.clear();
+               }
+               return;
+       }
+
+       if (__pkgId.empty() || !STR_EQ(key, "end") || !STR_EQ(val, "ok"))
+               return;
+
+       _I("'%s' has been removed", __pkgId.c_str());
+
+       ClientInfo::purgeClient(__pkgId);
+
+       __pkgId.clear();
+}
diff --git a/src/sensor/UninstallMonitor.h b/src/sensor/UninstallMonitor.h
new file mode 100644 (file)
index 0000000..8da2fce
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __CONTEXT_UNINSTALL_MONITOR_H__
+#define __CONTEXT_UNINSTALL_MONITOR_H__
+
+#include <string>
+#include <DBusSignalWatcher.h>
+
+namespace ctx {
+
+       class UninstallMonitor : public IDBusSignalListener {
+       public:
+               UninstallMonitor();
+               ~UninstallMonitor();
+
+               void onSignal(const char *sender, const char *path, const char *iface, const char *name, GVariant *param);
+
+       private:
+               int64_t __dbusSignalId;
+               DBusSignalWatcher __dbusWatcher;
+               std::string __pkgId;
+       };
+
+}
+
+#endif /* __CONTEXT_UNINSTALL_MONITOR_H__ */