#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");
delete __dbMgr;
__dbMgr = NULL;
+
+ delete __uninstallMonitor;
+ __uninstallMonitor = NULL;
}
int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
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));
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);
+ }
+}
#include <vector>
#include <Json.h>
#include <DatabaseManager.h>
+#include "UninstallMonitor.h"
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;
};
}
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;
}
return ERR_NONE;
}
+
+void SensorProvider::removeClient(std::string subject, std::string pkgId)
+{
+ __providerMap[subject]->__removeClient(pkgId);
+}
#ifndef __CONTEXT_SENSOR_PROVIDER_H__
#define __CONTEXT_SENSOR_PROVIDER_H__
+#include <map>
#include <ContextProvider.h>
#include "ClientInfo.h"
#include "SensorLogger.h"
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;
int __removeClient(std::string pkgId);
ClientInfo __clientInfo;
+
+ static std::map<std::string, SensorProvider*> __providerMap;
};
}
--- /dev/null
+/*
+ * 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();
+}
--- /dev/null
+/*
+ * 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__ */