* limitations under the License.
*/
-#include <ServiceBase.h>
+#include <Tuple.h>
#include <MethodCall.h>
+#include "ContextStoreService.h"
+#include "Store.h"
+#include "StoreManager.h"
#include "ContextStoreClient.h"
using namespace ctx;
void ContextStoreClient::onMethodCalled(MethodCall* methodCall)
{
- // 'methodCall' should be deleted.
+ try {
+ __verifyUid(methodCall->getUid());
+ std::string uri = __getStoreUri(methodCall->getParam());
+ Store* store = __getStore(uri);
+
+ if (methodCall->getMethodName() == METHOD_GET_ACCESS) {
+ __getAccess(*store, *methodCall);
+ } else if (methodCall->getMethodName() == METHOD_INSERT) {
+ __insert(*store, *methodCall);
+ } else if (methodCall->getMethodName() == METHOD_RETRIEVE) {
+ __retrieve(*store, *methodCall);
+ } else if (methodCall->getMethodName() == METHOD_REMOVE) {
+ __remove(*store, *methodCall);
+ }
+ } catch (int error) {
+ methodCall->reply(error);
+ }
+
delete methodCall;
}
void ContextStoreClient::onDisconnected()
{
}
+
+void ContextStoreClient::__verifyUid(uid_t uid)
+{
+ if (!isSystemUid(uid) && uid != ServiceBase::getActiveUser()) {
+ _E("Invalid Uid: %u != %u (ActiveUser)", uid, ServiceBase::getActiveUser());
+ throw static_cast<int>(E_ACCESS);
+ }
+}
+
+std::string ContextStoreClient::__getStoreUri(GVariant* param)
+{
+ const char* uri = NULL;
+ g_variant_get_child(param, IDX_URI, "&s", &uri);
+ if (!uri) {
+ _E("Invalid URI");
+ throw static_cast<int>(E_PARAM);
+ }
+ return uri;
+}
+
+StoreManager& ContextStoreClient::__getStoreManager()
+{
+ return static_cast<ContextStoreService&>(getHostService()).getStoreManager();
+}
+
+Store* ContextStoreClient::__getStore(const std::string& uri)
+{
+ Store* store = NULL;
+
+ if (isSystem()) {
+ store = __getStoreManager().getSystemStore(uri);
+ } else {
+ store = __getStoreManager().getUserStore(uri);
+ }
+
+ if (!store) {
+ _W("Getting store failed");
+ throw static_cast<int>(E_PARAM);
+ }
+
+ return store;
+}
+
+void ContextStoreClient::__getAccess(Store& store, MethodCall& methodCall)
+{
+ if (!store.permitted(*this))
+ throw static_cast<int>(E_ACCESS);
+
+ methodCall.reply(g_variant_new("(s)", store.getPath().c_str()));
+}
+
+void ContextStoreClient::__insert(Store& store, MethodCall& methodCall)
+{
+ const char* uri = NULL;
+ const char* cols = NULL;
+ GVariant* vals = NULL;
+
+ g_variant_get(methodCall.getParam(), "(&s&sv)", &uri, &cols, &vals);
+ if (!cols || !vals) {
+ if (vals) g_variant_unref(vals);
+ throw static_cast<int>(E_PARAM);
+ }
+
+ std::vector<Tuple*> tuples = Tuple::buildFrom(vals);
+ if (tuples.empty()) {
+ throw static_cast<int>(E_PARAM);
+ }
+
+ methodCall.reply(store.insert(*this, cols, tuples));
+}
+
+void ContextStoreClient::__retrieve(Store& store, MethodCall& methodCall)
+{
+ const char* uri = NULL;
+ const char* projection = NULL;
+ const char* selection = NULL;
+ const char* sortOrder = NULL;
+ uint32_t limit = 0;
+
+ g_variant_get(methodCall.getParam(), "(&s&s&s&su)", &uri, &projection, &selection, &sortOrder, &limit);
+ if (!projection || !selection || !sortOrder)
+ throw static_cast<int>(E_PARAM);
+
+ std::vector<Tuple*> tuples;
+ int error = store.retrieve(*this, projection, selection, sortOrder, limit, &tuples);
+ if (error != E_NONE)
+ throw error;
+
+ methodCall.reply(g_variant_new("(v)", Tuple::toGVariant(tuples)));
+}
+
+void ContextStoreClient::__remove(Store& store, MethodCall& methodCall)
+{
+ const char* uri = NULL;
+ const char* selection = NULL;
+
+ g_variant_get(methodCall.getParam(), "(&s&s)", &uri, &selection);
+ if (!selection)
+ throw static_cast<int>(E_PARAM);
+
+ methodCall.reply(store.remove(*this, selection));
+}
#include <ContextStoreTypesPrivate.h>
#include <ContextStoreService.h>
#include "ContextStoreClient.h"
+#include "StoreManager.h"
+#include "DatabaseManager.h"
+#include "SchemaLoader.h"
using namespace ctx;
ContextStoreService::ContextStoreService(GDBusConnection* conn) :
- ServiceBase(conn, CTX_CONTEXT_STORE, CTX_CONTEXT_STORE_SPEC)
+ ServiceBase(conn, CTX_CONTEXT_STORE, CTX_CONTEXT_STORE_SPEC),
+ __storeManager(NULL)
{
}
bool ContextStoreService::prepare()
{
- /* Service-specific initialization tasks */
+ if (!DatabaseManager::openSystem())
+ return false;
+
+ SystemSchemaLoader schemaLoader;
+ schemaLoader.load();
return true;
}
void ContextStoreService::cleanup()
{
+ delete __storeManager;
+ __storeManager = NULL;
+
+ DatabaseManager::closeUser();
+ DatabaseManager::closeSystem();
}
ClientBase* ContextStoreService::createClient(const std::string& busName)
void ContextStoreService::onUserActivated()
{
+ if (!DatabaseManager::openUser(getActiveUser()))
+ return;
+
+ UserSchemaLoader schemaLoader;
+ schemaLoader.load();
}
void ContextStoreService::onUserDeactivated()
{
+ DatabaseManager::closeUser();
+}
+
+StoreManager& ContextStoreService::getStoreManager()
+{
+ if (__storeManager)
+ return *__storeManager;
+
+ __storeManager = new(std::nothrow) StoreManager();
+
+ while (__storeManager == NULL) {
+ _W("Memory allocation failed");
+ __storeManager = new(std::nothrow) StoreManager();
+ }
+
+ return *__storeManager;
}