From 0da83d12133cf06d128bded7e431a2ab6ec32508 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Fri, 1 Jul 2016 15:45:42 +0900 Subject: [PATCH] sensor: cascade its recording requests if an app is uninstalled Change-Id: I17f2c453896107721cad74b6136b16f4eaf0ba4d Signed-off-by: Mu-Woong Lee --- src/sensor/ClientInfo.cpp | 38 +++++++++++++++++-- src/sensor/ClientInfo.h | 4 ++ src/sensor/SensorProvider.cpp | 9 +++++ src/sensor/SensorProvider.h | 5 +++ src/sensor/UninstallMonitor.cpp | 66 +++++++++++++++++++++++++++++++++ src/sensor/UninstallMonitor.h | 40 ++++++++++++++++++++ 6 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 src/sensor/UninstallMonitor.cpp create mode 100644 src/sensor/UninstallMonitor.h diff --git a/src/sensor/ClientInfo.cpp b/src/sensor/ClientInfo.cpp index 6a94f01..e1913f7 100644 --- a/src/sensor/ClientInfo.cpp +++ b/src/sensor/ClientInfo.cpp @@ -18,20 +18,23 @@ #include #include #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& 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 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); + } +} diff --git a/src/sensor/ClientInfo.h b/src/sensor/ClientInfo.h index a52cd27..a14dc3c 100644 --- a/src/sensor/ClientInfo.h +++ b/src/sensor/ClientInfo.h @@ -21,6 +21,7 @@ #include #include #include +#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; }; } diff --git a/src/sensor/SensorProvider.cpp b/src/sensor/SensorProvider.cpp index 32f0584..8cc8e1f 100644 --- a/src/sensor/SensorProvider.cpp +++ b/src/sensor/SensorProvider.cpp @@ -22,14 +22,18 @@ using namespace ctx; +std::map 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); +} diff --git a/src/sensor/SensorProvider.h b/src/sensor/SensorProvider.h index 4a1cf5b..601b191 100644 --- a/src/sensor/SensorProvider.h +++ b/src/sensor/SensorProvider.h @@ -17,6 +17,7 @@ #ifndef __CONTEXT_SENSOR_PROVIDER_H__ #define __CONTEXT_SENSOR_PROVIDER_H__ +#include #include #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 __providerMap; }; } diff --git a/src/sensor/UninstallMonitor.cpp b/src/sensor/UninstallMonitor.cpp new file mode 100644 index 0000000..b3dc8a5 --- /dev/null +++ b/src/sensor/UninstallMonitor.cpp @@ -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 index 0000000..8da2fce --- /dev/null +++ b/src/sensor/UninstallMonitor.h @@ -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 +#include + +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__ */ -- 2.34.1