From a02279f1cf0cef36d0d13d3e213c1c15b05e35ef Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Fri, 3 Mar 2017 20:05:37 +0900 Subject: [PATCH 01/16] Update the service code skeleton w.r.t. the change of the active user monitoring scheme Change-Id: Iad79d4539e2ea668954acf1343296e6a01fef286 Signed-off-by: Mu-Woong Lee --- include/ContextStoreService.h | 7 ++++++- src/server-dummy/ContextStoreService.cpp | 15 ++++++++++++--- src/server/ContextStoreService.cpp | 15 ++++++++++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/ContextStoreService.h b/include/ContextStoreService.h index a599af9..ca384dc 100644 --- a/include/ContextStoreService.h +++ b/include/ContextStoreService.h @@ -29,11 +29,16 @@ namespace ctx { ContextStoreService(GDBusConnection* conn); ~ContextStoreService(); + bool isUserService(); + protected: bool prepare(); void cleanup(); - void onUserActivated(uid_t uid); + ClientBase* createClient(const std::string& busName); + + void onUserActivated(); + void onUserDeactivated(); }; } diff --git a/src/server-dummy/ContextStoreService.cpp b/src/server-dummy/ContextStoreService.cpp index 3c66f49..d5ef3b2 100644 --- a/src/server-dummy/ContextStoreService.cpp +++ b/src/server-dummy/ContextStoreService.cpp @@ -29,16 +29,17 @@ ContextStoreService::~ContextStoreService() { } -bool ContextStoreService::prepare() +bool ContextStoreService::isUserService() { return false; } -void ContextStoreService::cleanup() +bool ContextStoreService::prepare() { + return false; } -void ContextStoreService::onUserActivated(uid_t uid) +void ContextStoreService::cleanup() { } @@ -46,3 +47,11 @@ ClientBase* ContextStoreService::createClient(const std::string& busName) { return NULL; } + +void ContextStoreService::onUserActivated() +{ +} + +void ContextStoreService::onUserDeactivated() +{ +} diff --git a/src/server/ContextStoreService.cpp b/src/server/ContextStoreService.cpp index e435fd0..c8dce21 100644 --- a/src/server/ContextStoreService.cpp +++ b/src/server/ContextStoreService.cpp @@ -29,6 +29,11 @@ ContextStoreService::~ContextStoreService() { } +bool ContextStoreService::isUserService() +{ + return false; +} + bool ContextStoreService::prepare() { /* Service-specific initialization tasks */ @@ -39,11 +44,15 @@ void ContextStoreService::cleanup() { } -void ContextStoreService::onUserActivated(uid_t uid) +ClientBase* ContextStoreService::createClient(const std::string& busName) +{ + return new(std::nothrow) ContextStoreClient(this, busName); +} + +void ContextStoreService::onUserActivated() { } -ClientBase* ContextStoreService::createClient(const std::string& busName) +void ContextStoreService::onUserDeactivated() { - return new(std::nothrow) ContextStoreClient(this, busName); } -- 2.7.4 From 2f8d2a5d198c923b9453407b53b2631a625527eb Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Thu, 23 Feb 2017 22:54:34 +0900 Subject: [PATCH 02/16] Define ContextStore C++ internal API (skeleton) Change-Id: I3b2f701932d8d27e21013b29bf8e20103b2b9c40 Signed-off-by: Mu-Woong Lee --- include/ContextStore.h | 26 +++++--- include/ContextStoreManager.h | 37 +++++++++++ include/ContextStoreSearchQuery.h | 52 +++++++++++++++ src/client-dummy/ContextStore.cpp | 26 +++++++- src/client-dummy/ContextStoreManager.cpp | 38 +++++++++++ src/client-dummy/ContextStoreSearchQuery.cpp | 65 ++++++++++++++++++ src/client/AppManagedLocalStore.cpp | 58 ++++++++++++++++ src/client/AppManagedLocalStore.h | 44 +++++++++++++ src/client/AppManagedSharedStore.cpp | 49 ++++++++++++++ src/client/AppManagedSharedStore.h | 42 ++++++++++++ src/client/AppManagedStore.cpp | 34 ++++++++++ src/client/AppManagedStore.h | 40 ++++++++++++ src/client/ContextStore.cpp | 31 +++++---- src/client/ContextStoreManager.cpp | 98 ++++++++++++++++++++++++++++ src/client/ContextStoreSearchQuery.cpp | 66 +++++++++++++++++++ src/client/PlatformManagedStore.cpp | 64 ++++++++++++++++++ src/client/PlatformManagedStore.h | 47 +++++++++++++ src/client/Schema.cpp | 27 ++++++++ src/client/Schema.h | 37 +++++++++++ src/client/SchemaConf.cpp | 38 +++++++++++ src/client/SchemaConf.h | 38 +++++++++++ src/shared/ContextStoreTypesPrivate.h | 1 + 22 files changed, 936 insertions(+), 22 deletions(-) create mode 100644 include/ContextStoreManager.h create mode 100644 include/ContextStoreSearchQuery.h create mode 100644 src/client-dummy/ContextStoreManager.cpp create mode 100644 src/client-dummy/ContextStoreSearchQuery.cpp create mode 100644 src/client/AppManagedLocalStore.cpp create mode 100644 src/client/AppManagedLocalStore.h create mode 100644 src/client/AppManagedSharedStore.cpp create mode 100644 src/client/AppManagedSharedStore.h create mode 100644 src/client/AppManagedStore.cpp create mode 100644 src/client/AppManagedStore.h create mode 100644 src/client/ContextStoreManager.cpp create mode 100644 src/client/ContextStoreSearchQuery.cpp create mode 100644 src/client/PlatformManagedStore.cpp create mode 100644 src/client/PlatformManagedStore.h create mode 100644 src/client/Schema.cpp create mode 100644 src/client/Schema.h create mode 100644 src/client/SchemaConf.cpp create mode 100644 src/client/SchemaConf.h diff --git a/include/ContextStore.h b/include/ContextStore.h index e68578a..cfe97d1 100644 --- a/include/ContextStore.h +++ b/include/ContextStore.h @@ -17,18 +17,26 @@ #ifndef __CONTEXT_STORE_H__ #define __CONTEXT_STORE_H__ +#include +#include #include +#include -#ifdef __cplusplus -extern "C" -{ -#endif +namespace ctx { -/* TEST CODE */ -int context_store_test(); + class EXPORT_API ContextStore { + public: + virtual ~ContextStore(); + + virtual int insert(const std::vector& columns, Tuple& record); + virtual int insert(const std::vector& columns, std::vector& records); + virtual int retrieve(const ContextStoreSearchQuery& query, std::vector& result); + virtual int remove(const std::string& selection); + + protected: + ContextStore(); + }; -#ifdef __cplusplus } -#endif -#endif +#endif /* __CONTEXT_STORE_H__ */ diff --git a/include/ContextStoreManager.h b/include/ContextStoreManager.h new file mode 100644 index 0000000..4fbdcdc --- /dev/null +++ b/include/ContextStoreManager.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2017 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_STORE_MANAGER_H__ +#define __CONTEXT_STORE_MANAGER_H__ + +#include +#include +#include + +namespace ctx { + + class EXPORT_API ContextStoreManager { + public: + ContextStoreManager(); + ~ContextStoreManager(); + + int getStore(const std::string& uri, ContextStore** contextStore); + int removeStore(const std::string& uri); + }; + +} + +#endif /* __CONTEXT_STORE_MANAGER_H__ */ diff --git a/include/ContextStoreSearchQuery.h b/include/ContextStoreSearchQuery.h new file mode 100644 index 0000000..2c0d187 --- /dev/null +++ b/include/ContextStoreSearchQuery.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2017 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_STORE_SEARCH_QUERY_H__ +#define __CONTEXT_STORE_SEARCH_QUERY_H__ + +#include +#include + +namespace ctx { + + class EXPORT_API ContextStoreSearchQuery { + public: + ContextStoreSearchQuery(); + ContextStoreSearchQuery( + const std::string& projection, + const std::string& selection, + const std::string& sortOrder, + unsigned int limit); + + ~ContextStoreSearchQuery(); + + ContextStoreSearchQuery& setProjection(const std::string& projection); + ContextStoreSearchQuery& setSelection(const std::string& selection); + ContextStoreSearchQuery& setSortOrder(const std::string& sortOrder); + ContextStoreSearchQuery& setLimit(unsigned int limit); + + bool valid(); + + private: + std::string __projection; + std::string __selection; + std::string __sortOrder; + unsigned int __limit; + }; + +} + +#endif /* __CONTEXT_STORE_SEARCH_QUERY_H__ */ diff --git a/src/client-dummy/ContextStore.cpp b/src/client-dummy/ContextStore.cpp index c526a34..47eca13 100644 --- a/src/client-dummy/ContextStore.cpp +++ b/src/client-dummy/ContextStore.cpp @@ -19,8 +19,30 @@ using namespace ctx; -/* TODO: Remove this test code */ -EXPORT_API int context_store_test() +ContextStore::ContextStore() +{ +} + +ContextStore::~ContextStore() +{ +} + +int ContextStore::insert(const std::vector& columns, Tuple& record) +{ + return E_SUPPORT; +} + +int ContextStore::insert(const std::vector& columns, std::vector& records) +{ + return E_SUPPORT; +} + +int ContextStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +{ + return E_SUPPORT; +} + +int ContextStore::remove(const std::string& selection) { return E_SUPPORT; } diff --git a/src/client-dummy/ContextStoreManager.cpp b/src/client-dummy/ContextStoreManager.cpp new file mode 100644 index 0000000..f19a6dc --- /dev/null +++ b/src/client-dummy/ContextStoreManager.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 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 +#include + +using namespace ctx; + +ContextStoreManager::ContextStoreManager() +{ +} + +ContextStoreManager::~ContextStoreManager() +{ +} + +int ContextStoreManager::getStore(const std::string& uri, ContextStore** store) +{ + return E_SUPPORT; +} + +int ContextStoreManager::removeStore(const std::string& uri) +{ + return E_SUPPORT; +} diff --git a/src/client-dummy/ContextStoreSearchQuery.cpp b/src/client-dummy/ContextStoreSearchQuery.cpp new file mode 100644 index 0000000..25f60d4 --- /dev/null +++ b/src/client-dummy/ContextStoreSearchQuery.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2017 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 +#include + +using namespace ctx; + +ContextStoreSearchQuery::ContextStoreSearchQuery() : + __limit(0) +{ +} + +ContextStoreSearchQuery::ContextStoreSearchQuery(const std::string& projection, + const std::string& selection, const std::string& sortOrder, + unsigned int limit) : + __limit(0) +{ +} + +ContextStoreSearchQuery::~ContextStoreSearchQuery() +{ +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setProjection(const std::string& projection) +{ + __projection = projection; + return *this; +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setSelection(const std::string& selection) +{ + __selection = selection; + return *this; +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setSortOrder(const std::string& sortOrder) +{ + __sortOrder = sortOrder; + return *this; +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setLimit(unsigned int limit) +{ + __limit = limit; + return *this; +} + +bool ContextStoreSearchQuery::valid() +{ + return false; +} diff --git a/src/client/AppManagedLocalStore.cpp b/src/client/AppManagedLocalStore.cpp new file mode 100644 index 0000000..9480c4f --- /dev/null +++ b/src/client/AppManagedLocalStore.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2017 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 "AppManagedLocalStore.h" + +using namespace ctx; + +AppManagedLocalStore::AppManagedLocalStore(ServiceProxy* proxy) : + AppManagedStore(proxy) +{ +} + +AppManagedLocalStore::~AppManagedLocalStore() +{ +} + +int AppManagedLocalStore::init(Schema& schema) +{ + //TODO + return E_SUPPORT; +} + +int AppManagedLocalStore::insert(const std::vector& columns, Tuple& record) +{ + //TODO + return E_SUPPORT; +} + +int AppManagedLocalStore::insert(const std::vector& columns, std::vector& records) +{ + //TODO + return E_SUPPORT; +} + +int AppManagedLocalStore::remove(const std::string& selection) +{ + //TODO + return E_SUPPORT; +} + +int AppManagedLocalStore::destroy(const std::string& uri, ServiceProxy* proxy) +{ + //TODO + return E_NONE; +} diff --git a/src/client/AppManagedLocalStore.h b/src/client/AppManagedLocalStore.h new file mode 100644 index 0000000..8ae4cc1 --- /dev/null +++ b/src/client/AppManagedLocalStore.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017 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_STORE_APP_MANAGED_LOCAL_STORE_H__ +#define __CONTEXT_STORE_APP_MANAGED_LOCAL_STORE_H__ + +#include +#include +#include +#include "AppManagedStore.h" +#include "Schema.h" + +namespace ctx { + + class AppManagedLocalStore : public AppManagedStore { + public: + AppManagedLocalStore(ServiceProxy* proxy); + ~AppManagedLocalStore(); + + int init(Schema& schema); + + int insert(const std::vector& columns, Tuple& record); + int insert(const std::vector& columns, std::vector& records); + int remove(const std::string& selection); + + static int destroy(const std::string& uri, ServiceProxy* proxy); + }; + +} + +#endif /* __CONTEXT_STORE_APP_MANAGED_LOCAL_STORE_H__ */ diff --git a/src/client/AppManagedSharedStore.cpp b/src/client/AppManagedSharedStore.cpp new file mode 100644 index 0000000..0943d6b --- /dev/null +++ b/src/client/AppManagedSharedStore.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 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 "AppManagedSharedStore.h" + +using namespace ctx; + +AppManagedSharedStore::AppManagedSharedStore(ServiceProxy* proxy) : + AppManagedStore(proxy) +{ +} + +AppManagedSharedStore::~AppManagedSharedStore() +{ +} + +int AppManagedSharedStore::init(Schema& schema) +{ + //TODO + return E_SUPPORT; +} + +int AppManagedSharedStore::insert(const std::vector& columns, Tuple& record) +{ + return E_PRIVIL; +} + +int AppManagedSharedStore::insert(const std::vector& columns, std::vector& records) +{ + return E_PRIVIL; +} + +int AppManagedSharedStore::remove(const std::string& selection) +{ + return E_PRIVIL; +} diff --git a/src/client/AppManagedSharedStore.h b/src/client/AppManagedSharedStore.h new file mode 100644 index 0000000..26168ba --- /dev/null +++ b/src/client/AppManagedSharedStore.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017 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_STORE_APP_MANAGED_SHARED_STORE_H__ +#define __CONTEXT_STORE_APP_MANAGED_SHARED_STORE_H__ + +#include +#include +#include +#include "AppManagedStore.h" +#include "Schema.h" + +namespace ctx { + + class AppManagedSharedStore : public AppManagedStore { + public: + AppManagedSharedStore(ServiceProxy* proxy); + ~AppManagedSharedStore(); + + int init(Schema& schema); + + int insert(const std::vector& columns, Tuple& record); + int insert(const std::vector& columns, std::vector& records); + int remove(const std::string& selection); + }; + +} + +#endif /* __CONTEXT_STORE_APP_MANAGED_SHARED_STORE_H__ */ diff --git a/src/client/AppManagedStore.cpp b/src/client/AppManagedStore.cpp new file mode 100644 index 0000000..3ba01fd --- /dev/null +++ b/src/client/AppManagedStore.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2017 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 "AppManagedStore.h" + +using namespace ctx; + +AppManagedStore::AppManagedStore(ServiceProxy* proxy) : + __proxy(proxy) +{ +} + +AppManagedStore::~AppManagedStore() +{ +} + +int AppManagedStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +{ + // TODO + return E_SUPPORT; +} diff --git a/src/client/AppManagedStore.h b/src/client/AppManagedStore.h new file mode 100644 index 0000000..6fd1035 --- /dev/null +++ b/src/client/AppManagedStore.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 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_STORE_APP_MANAGED_STORE_H__ +#define __CONTEXT_STORE_APP_MANAGED_STORE_H__ + +#include +#include +#include +#include "Schema.h" + +namespace ctx { + + class AppManagedStore : public ContextStore { + public: + AppManagedStore(ServiceProxy* proxy); + virtual ~AppManagedStore(); + + int retrieve(const ContextStoreSearchQuery& query, std::vector& result); + + private: + ServiceProxy* __proxy; + }; + +} + +#endif /* __CONTEXT_STORE_APP_MANAGED_STORE_H__ */ diff --git a/src/client/ContextStore.cpp b/src/client/ContextStore.cpp index 051a48d..47eca13 100644 --- a/src/client/ContextStore.cpp +++ b/src/client/ContextStore.cpp @@ -15,25 +15,34 @@ */ #include -#include #include using namespace ctx; -/* TODO: Remove this test code */ -EXPORT_API int context_store_test() +ContextStore::ContextStore() { - // ServiceProxy object can be maintained as a static object. - ServiceProxy proxy(CTX_CONTEXT_STORE); +} - GVariant* output = NULL; +ContextStore::~ContextStore() +{ +} - int err = proxy.call("test", g_variant_new("(i)", 100), &output); +int ContextStore::insert(const std::vector& columns, Tuple& record) +{ + return E_SUPPORT; +} - _I(YELLOW("Return: %d (%s)"), err, CTX_ERROR_STR(err)); +int ContextStore::insert(const std::vector& columns, std::vector& records) +{ + return E_SUPPORT; +} - if (output) - g_variant_unref(output); +int ContextStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +{ + return E_SUPPORT; +} - return err; +int ContextStore::remove(const std::string& selection) +{ + return E_SUPPORT; } diff --git a/src/client/ContextStoreManager.cpp b/src/client/ContextStoreManager.cpp new file mode 100644 index 0000000..692c582 --- /dev/null +++ b/src/client/ContextStoreManager.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2017 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 +#include +#include +#include +#include +#include "SchemaConf.h" +#include "AppManagedLocalStore.h" +#include "AppManagedSharedStore.h" +#include "PlatformManagedStore.h" + +using namespace ctx; + +static SchemaConf __schemaConf; + +static ServiceProxy* __getServiceProxy() +{ + static ServiceProxy proxy(CTX_CONTEXT_STORE); + return &proxy; +} + +template +static ContextStore* __createStore(Schema& schema, int* error) +{ + StoreType* store = new(std::nothrow) StoreType(__getServiceProxy()); + + if (!store) { + *error = E_NO_MEM; + _E("Memory allocation failed"); + return NULL; + } + + *error = store->init(schema); + + if (*error != E_NONE) { + delete store; + return NULL; + } + + return store; +} + +ContextStoreManager::ContextStoreManager() +{ +} + +ContextStoreManager::~ContextStoreManager() +{ +} + +int ContextStoreManager::getStore(const std::string& uri, ContextStore** store) +{ + IF_FAIL_RETURN(store, E_PARAM); + IF_FAIL_RETURN(__schemaConf.init(), E_FAILED); + + Schema& schema = __schemaConf.getSchema(uri); + + int error = E_NONE; + ContextStore* _store = NULL; + + _store = __createStore(schema, &error); + + if (error == E_PARAM) + _store = __createStore(schema, &error); + + if (error == E_PARAM) + _store = __createStore(schema, &error); + + IF_FAIL_RETURN(_store, error); + + *store = _store; + return E_NONE; +} + +int ContextStoreManager::removeStore(const std::string& uri) +{ + int error = AppManagedLocalStore::destroy(uri, __getServiceProxy()); + + if (error == E_PARAM) + error = PlatformManagedStore::destroy(uri, __getServiceProxy()); + + return error; +} diff --git a/src/client/ContextStoreSearchQuery.cpp b/src/client/ContextStoreSearchQuery.cpp new file mode 100644 index 0000000..995f92d --- /dev/null +++ b/src/client/ContextStoreSearchQuery.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2017 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 +#include + +using namespace ctx; + +ContextStoreSearchQuery::ContextStoreSearchQuery() : + __limit(0) +{ +} + +ContextStoreSearchQuery::ContextStoreSearchQuery(const std::string& projection, + const std::string& selection, const std::string& sortOrder, + unsigned int limit) : + __limit(0) +{ +} + +ContextStoreSearchQuery::~ContextStoreSearchQuery() +{ +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setProjection(const std::string& projection) +{ + __projection = projection; + return *this; +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setSelection(const std::string& selection) +{ + __selection = selection; + return *this; +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setSortOrder(const std::string& sortOrder) +{ + __sortOrder = sortOrder; + return *this; +} + +ContextStoreSearchQuery& ContextStoreSearchQuery::setLimit(unsigned int limit) +{ + __limit = limit; + return *this; +} + +bool ContextStoreSearchQuery::valid() +{ + // TODO + return true; +} diff --git a/src/client/PlatformManagedStore.cpp b/src/client/PlatformManagedStore.cpp new file mode 100644 index 0000000..bf856c8 --- /dev/null +++ b/src/client/PlatformManagedStore.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017 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 "PlatformManagedStore.h" + +using namespace ctx; + +PlatformManagedStore::PlatformManagedStore(ServiceProxy* proxy) : + __proxy(proxy) +{ +} + +PlatformManagedStore::~PlatformManagedStore() +{ +} + +int PlatformManagedStore::init(Schema& schema) +{ + // TODO + return E_SUPPORT; +} + +int PlatformManagedStore::insert(const std::vector& columns, Tuple& record) +{ + // TODO + return E_SUPPORT; +} + +int PlatformManagedStore::insert(const std::vector& columns, std::vector& records) +{ + // TODO + return E_SUPPORT; +} + +int PlatformManagedStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +{ + // TODO + return E_SUPPORT; +} + +int PlatformManagedStore::remove(const std::string& selection) +{ + // TODO + return E_SUPPORT; +} + +int PlatformManagedStore::destroy(const std::string& uri, ServiceProxy* proxy) +{ + // TODO + return E_NONE; +} diff --git a/src/client/PlatformManagedStore.h b/src/client/PlatformManagedStore.h new file mode 100644 index 0000000..990f951 --- /dev/null +++ b/src/client/PlatformManagedStore.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017 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_STORE_PLATFORM_MANAGED_STORE_H__ +#define __CONTEXT_STORE_PLATFORM_MANAGED_STORE_H__ + +#include +#include +#include +#include "Schema.h" + +namespace ctx { + + class PlatformManagedStore : public ContextStore { + public: + PlatformManagedStore(ServiceProxy* proxy); + ~PlatformManagedStore(); + + int init(Schema& schema); + + int insert(const std::vector& columns, Tuple& record); + int insert(const std::vector& columns, std::vector& records); + int retrieve(const ContextStoreSearchQuery& query, std::vector& result); + int remove(const std::string& selection); + + static int destroy(const std::string& uri, ServiceProxy* proxy); + + private: + ServiceProxy* __proxy; + }; + +} + +#endif /* __CONTEXT_STORE_PLATFORM_MANAGED_STORE_H__ */ diff --git a/src/client/Schema.cpp b/src/client/Schema.cpp new file mode 100644 index 0000000..cfbe505 --- /dev/null +++ b/src/client/Schema.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017 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 "Schema.h" + +using namespace ctx; + +Schema::Schema() +{ +} + +Schema::~Schema() +{ +} diff --git a/src/client/Schema.h b/src/client/Schema.h new file mode 100644 index 0000000..1e94e3b --- /dev/null +++ b/src/client/Schema.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2017 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_STORE_SCHEMA_H__ +#define __CONTEXT_STORE_SCHEMA_H__ + +#include +#include + +namespace ctx { + + class Schema { + public: + ~Schema(); + + private: + Schema(); + + friend class SchemaConf; + }; + +} + +#endif /* __CONTEXT_STORE_SCHEMA_H__ */ diff --git a/src/client/SchemaConf.cpp b/src/client/SchemaConf.cpp new file mode 100644 index 0000000..bccebdf --- /dev/null +++ b/src/client/SchemaConf.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 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 "SchemaConf.h" + +using namespace ctx; + +SchemaConf::SchemaConf() +{ +} + +SchemaConf::~SchemaConf() +{ +} + +bool SchemaConf::init() +{ + return true; +} + +Schema& SchemaConf::getSchema(const std::string& uri) +{ + static Schema dummy; + return dummy; +} diff --git a/src/client/SchemaConf.h b/src/client/SchemaConf.h new file mode 100644 index 0000000..3bb0674 --- /dev/null +++ b/src/client/SchemaConf.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 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_STORE_SCHEMA_CONF_H__ +#define __CONTEXT_STORE_SCHEMA_CONF_H__ + +#include +#include +#include "Schema.h" + +namespace ctx { + + class SchemaConf { + public: + SchemaConf(); + ~SchemaConf(); + + bool init(); + + Schema& getSchema(const std::string& uri); + }; + +} + +#endif /* __CONTEXT_STORE_SCHEMA_CONF_H__ */ diff --git a/src/shared/ContextStoreTypesPrivate.h b/src/shared/ContextStoreTypesPrivate.h index abcbbe0..5322d5f 100644 --- a/src/shared/ContextStoreTypesPrivate.h +++ b/src/shared/ContextStoreTypesPrivate.h @@ -19,6 +19,7 @@ #include #include +#include #define CTX_CONTEXT_STORE "ContextStore" #define CTX_CONTEXT_STORE_SPEC \ -- 2.7.4 From 7a9009659080dac70bfa21772b01923f883e2016 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Wed, 8 Mar 2017 17:15:37 +0900 Subject: [PATCH 03/16] Remove API features managing databases in the application directories Not supported in Tizen 4.0. Change-Id: Icf003353317c9da1f8c4a14c14a4165ba5447c66 Signed-off-by: Mu-Woong Lee --- include/ContextStore.h | 2 +- packaging/context-store.spec | 3 +- src/client-dummy/ContextStore.cpp | 2 +- src/client/AppManagedLocalStore.cpp | 58 ------------------------------------ src/client/AppManagedLocalStore.h | 44 --------------------------- src/client/AppManagedSharedStore.cpp | 49 ------------------------------ src/client/AppManagedSharedStore.h | 42 -------------------------- src/client/AppManagedStore.cpp | 34 --------------------- src/client/AppManagedStore.h | 40 ------------------------- src/client/ContextStore.cpp | 2 +- src/client/ContextStoreManager.cpp | 35 ++++++++-------------- src/client/PlatformManagedStore.cpp | 4 +-- src/client/PlatformManagedStore.h | 5 ++-- src/client/Schema.cpp | 27 ----------------- src/client/Schema.h | 37 ----------------------- src/client/SchemaConf.cpp | 38 ----------------------- src/client/SchemaConf.h | 38 ----------------------- 17 files changed, 21 insertions(+), 439 deletions(-) delete mode 100644 src/client/AppManagedLocalStore.cpp delete mode 100644 src/client/AppManagedLocalStore.h delete mode 100644 src/client/AppManagedSharedStore.cpp delete mode 100644 src/client/AppManagedSharedStore.h delete mode 100644 src/client/AppManagedStore.cpp delete mode 100644 src/client/AppManagedStore.h delete mode 100644 src/client/Schema.cpp delete mode 100644 src/client/Schema.h delete mode 100644 src/client/SchemaConf.cpp delete mode 100644 src/client/SchemaConf.h diff --git a/include/ContextStore.h b/include/ContextStore.h index cfe97d1..bd1222d 100644 --- a/include/ContextStore.h +++ b/include/ContextStore.h @@ -28,7 +28,7 @@ namespace ctx { public: virtual ~ContextStore(); - virtual int insert(const std::vector& columns, Tuple& record); + virtual int insert(const std::vector& columns, Tuple* record); virtual int insert(const std::vector& columns, std::vector& records); virtual int retrieve(const ContextStoreSearchQuery& query, std::vector& result); virtual int remove(const std::string& selection); diff --git a/packaging/context-store.spec b/packaging/context-store.spec index 235067e..573a430 100644 --- a/packaging/context-store.spec +++ b/packaging/context-store.spec @@ -9,10 +9,9 @@ Source0: %{name}-%{version}.tar.gz BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(gio-2.0) -BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(libxml-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(capi-base-common) -BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(context-common-server) BuildRequires: pkgconfig(context-common-client) Provides: %{name}-profile_tv = %{version}-%{release} diff --git a/src/client-dummy/ContextStore.cpp b/src/client-dummy/ContextStore.cpp index 47eca13..dd2b1d2 100644 --- a/src/client-dummy/ContextStore.cpp +++ b/src/client-dummy/ContextStore.cpp @@ -27,7 +27,7 @@ ContextStore::~ContextStore() { } -int ContextStore::insert(const std::vector& columns, Tuple& record) +int ContextStore::insert(const std::vector& columns, Tuple* record) { return E_SUPPORT; } diff --git a/src/client/AppManagedLocalStore.cpp b/src/client/AppManagedLocalStore.cpp deleted file mode 100644 index 9480c4f..0000000 --- a/src/client/AppManagedLocalStore.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2017 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 "AppManagedLocalStore.h" - -using namespace ctx; - -AppManagedLocalStore::AppManagedLocalStore(ServiceProxy* proxy) : - AppManagedStore(proxy) -{ -} - -AppManagedLocalStore::~AppManagedLocalStore() -{ -} - -int AppManagedLocalStore::init(Schema& schema) -{ - //TODO - return E_SUPPORT; -} - -int AppManagedLocalStore::insert(const std::vector& columns, Tuple& record) -{ - //TODO - return E_SUPPORT; -} - -int AppManagedLocalStore::insert(const std::vector& columns, std::vector& records) -{ - //TODO - return E_SUPPORT; -} - -int AppManagedLocalStore::remove(const std::string& selection) -{ - //TODO - return E_SUPPORT; -} - -int AppManagedLocalStore::destroy(const std::string& uri, ServiceProxy* proxy) -{ - //TODO - return E_NONE; -} diff --git a/src/client/AppManagedLocalStore.h b/src/client/AppManagedLocalStore.h deleted file mode 100644 index 8ae4cc1..0000000 --- a/src/client/AppManagedLocalStore.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2017 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_STORE_APP_MANAGED_LOCAL_STORE_H__ -#define __CONTEXT_STORE_APP_MANAGED_LOCAL_STORE_H__ - -#include -#include -#include -#include "AppManagedStore.h" -#include "Schema.h" - -namespace ctx { - - class AppManagedLocalStore : public AppManagedStore { - public: - AppManagedLocalStore(ServiceProxy* proxy); - ~AppManagedLocalStore(); - - int init(Schema& schema); - - int insert(const std::vector& columns, Tuple& record); - int insert(const std::vector& columns, std::vector& records); - int remove(const std::string& selection); - - static int destroy(const std::string& uri, ServiceProxy* proxy); - }; - -} - -#endif /* __CONTEXT_STORE_APP_MANAGED_LOCAL_STORE_H__ */ diff --git a/src/client/AppManagedSharedStore.cpp b/src/client/AppManagedSharedStore.cpp deleted file mode 100644 index 0943d6b..0000000 --- a/src/client/AppManagedSharedStore.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2017 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 "AppManagedSharedStore.h" - -using namespace ctx; - -AppManagedSharedStore::AppManagedSharedStore(ServiceProxy* proxy) : - AppManagedStore(proxy) -{ -} - -AppManagedSharedStore::~AppManagedSharedStore() -{ -} - -int AppManagedSharedStore::init(Schema& schema) -{ - //TODO - return E_SUPPORT; -} - -int AppManagedSharedStore::insert(const std::vector& columns, Tuple& record) -{ - return E_PRIVIL; -} - -int AppManagedSharedStore::insert(const std::vector& columns, std::vector& records) -{ - return E_PRIVIL; -} - -int AppManagedSharedStore::remove(const std::string& selection) -{ - return E_PRIVIL; -} diff --git a/src/client/AppManagedSharedStore.h b/src/client/AppManagedSharedStore.h deleted file mode 100644 index 26168ba..0000000 --- a/src/client/AppManagedSharedStore.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2017 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_STORE_APP_MANAGED_SHARED_STORE_H__ -#define __CONTEXT_STORE_APP_MANAGED_SHARED_STORE_H__ - -#include -#include -#include -#include "AppManagedStore.h" -#include "Schema.h" - -namespace ctx { - - class AppManagedSharedStore : public AppManagedStore { - public: - AppManagedSharedStore(ServiceProxy* proxy); - ~AppManagedSharedStore(); - - int init(Schema& schema); - - int insert(const std::vector& columns, Tuple& record); - int insert(const std::vector& columns, std::vector& records); - int remove(const std::string& selection); - }; - -} - -#endif /* __CONTEXT_STORE_APP_MANAGED_SHARED_STORE_H__ */ diff --git a/src/client/AppManagedStore.cpp b/src/client/AppManagedStore.cpp deleted file mode 100644 index 3ba01fd..0000000 --- a/src/client/AppManagedStore.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2017 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 "AppManagedStore.h" - -using namespace ctx; - -AppManagedStore::AppManagedStore(ServiceProxy* proxy) : - __proxy(proxy) -{ -} - -AppManagedStore::~AppManagedStore() -{ -} - -int AppManagedStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) -{ - // TODO - return E_SUPPORT; -} diff --git a/src/client/AppManagedStore.h b/src/client/AppManagedStore.h deleted file mode 100644 index 6fd1035..0000000 --- a/src/client/AppManagedStore.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2017 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_STORE_APP_MANAGED_STORE_H__ -#define __CONTEXT_STORE_APP_MANAGED_STORE_H__ - -#include -#include -#include -#include "Schema.h" - -namespace ctx { - - class AppManagedStore : public ContextStore { - public: - AppManagedStore(ServiceProxy* proxy); - virtual ~AppManagedStore(); - - int retrieve(const ContextStoreSearchQuery& query, std::vector& result); - - private: - ServiceProxy* __proxy; - }; - -} - -#endif /* __CONTEXT_STORE_APP_MANAGED_STORE_H__ */ diff --git a/src/client/ContextStore.cpp b/src/client/ContextStore.cpp index 47eca13..dd2b1d2 100644 --- a/src/client/ContextStore.cpp +++ b/src/client/ContextStore.cpp @@ -27,7 +27,7 @@ ContextStore::~ContextStore() { } -int ContextStore::insert(const std::vector& columns, Tuple& record) +int ContextStore::insert(const std::vector& columns, Tuple* record) { return E_SUPPORT; } diff --git a/src/client/ContextStoreManager.cpp b/src/client/ContextStoreManager.cpp index 692c582..69679f4 100644 --- a/src/client/ContextStoreManager.cpp +++ b/src/client/ContextStoreManager.cpp @@ -19,15 +19,10 @@ #include #include #include -#include "SchemaConf.h" -#include "AppManagedLocalStore.h" -#include "AppManagedSharedStore.h" #include "PlatformManagedStore.h" using namespace ctx; -static SchemaConf __schemaConf; - static ServiceProxy* __getServiceProxy() { static ServiceProxy proxy(CTX_CONTEXT_STORE); @@ -35,19 +30,20 @@ static ServiceProxy* __getServiceProxy() } template -static ContextStore* __createStore(Schema& schema, int* error) +static ContextStore* __createStore(const std::string& uri, int* error) { StoreType* store = new(std::nothrow) StoreType(__getServiceProxy()); if (!store) { *error = E_NO_MEM; - _E("Memory allocation failed"); + _E("%s", CTX_ERROR_STR(*error)); return NULL; } - *error = store->init(schema); + *error = store->init(uri); if (*error != E_NONE) { + _E("%s", CTX_ERROR_STR(*error)); delete store; return NULL; } @@ -66,21 +62,12 @@ ContextStoreManager::~ContextStoreManager() int ContextStoreManager::getStore(const std::string& uri, ContextStore** store) { IF_FAIL_RETURN(store, E_PARAM); - IF_FAIL_RETURN(__schemaConf.init(), E_FAILED); - - Schema& schema = __schemaConf.getSchema(uri); + _D("Getting the store %s", uri.c_str()); int error = E_NONE; ContextStore* _store = NULL; - _store = __createStore(schema, &error); - - if (error == E_PARAM) - _store = __createStore(schema, &error); - - if (error == E_PARAM) - _store = __createStore(schema, &error); - + _store = __createStore(uri, &error); IF_FAIL_RETURN(_store, error); *store = _store; @@ -89,10 +76,14 @@ int ContextStoreManager::getStore(const std::string& uri, ContextStore** store) int ContextStoreManager::removeStore(const std::string& uri) { - int error = AppManagedLocalStore::destroy(uri, __getServiceProxy()); + _D("Dropping the store %s", uri.c_str()); + + int error = E_NONE; + + error = PlatformManagedStore::destroy(uri, __getServiceProxy()); - if (error == E_PARAM) - error = PlatformManagedStore::destroy(uri, __getServiceProxy()); + if (error != E_NONE) + _E("%s", CTX_ERROR_STR(error)); return error; } diff --git a/src/client/PlatformManagedStore.cpp b/src/client/PlatformManagedStore.cpp index bf856c8..575d29e 100644 --- a/src/client/PlatformManagedStore.cpp +++ b/src/client/PlatformManagedStore.cpp @@ -27,13 +27,13 @@ PlatformManagedStore::~PlatformManagedStore() { } -int PlatformManagedStore::init(Schema& schema) +int PlatformManagedStore::init(const std::string& uri) { // TODO return E_SUPPORT; } -int PlatformManagedStore::insert(const std::vector& columns, Tuple& record) +int PlatformManagedStore::insert(const std::vector& columns, Tuple* record) { // TODO return E_SUPPORT; diff --git a/src/client/PlatformManagedStore.h b/src/client/PlatformManagedStore.h index 990f951..fbc7bcd 100644 --- a/src/client/PlatformManagedStore.h +++ b/src/client/PlatformManagedStore.h @@ -20,7 +20,6 @@ #include #include #include -#include "Schema.h" namespace ctx { @@ -29,9 +28,9 @@ namespace ctx { PlatformManagedStore(ServiceProxy* proxy); ~PlatformManagedStore(); - int init(Schema& schema); + int init(const std::string& uri); - int insert(const std::vector& columns, Tuple& record); + int insert(const std::vector& columns, Tuple* record); int insert(const std::vector& columns, std::vector& records); int retrieve(const ContextStoreSearchQuery& query, std::vector& result); int remove(const std::string& selection); diff --git a/src/client/Schema.cpp b/src/client/Schema.cpp deleted file mode 100644 index cfbe505..0000000 --- a/src/client/Schema.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2017 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 "Schema.h" - -using namespace ctx; - -Schema::Schema() -{ -} - -Schema::~Schema() -{ -} diff --git a/src/client/Schema.h b/src/client/Schema.h deleted file mode 100644 index 1e94e3b..0000000 --- a/src/client/Schema.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2017 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_STORE_SCHEMA_H__ -#define __CONTEXT_STORE_SCHEMA_H__ - -#include -#include - -namespace ctx { - - class Schema { - public: - ~Schema(); - - private: - Schema(); - - friend class SchemaConf; - }; - -} - -#endif /* __CONTEXT_STORE_SCHEMA_H__ */ diff --git a/src/client/SchemaConf.cpp b/src/client/SchemaConf.cpp deleted file mode 100644 index bccebdf..0000000 --- a/src/client/SchemaConf.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2017 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 "SchemaConf.h" - -using namespace ctx; - -SchemaConf::SchemaConf() -{ -} - -SchemaConf::~SchemaConf() -{ -} - -bool SchemaConf::init() -{ - return true; -} - -Schema& SchemaConf::getSchema(const std::string& uri) -{ - static Schema dummy; - return dummy; -} diff --git a/src/client/SchemaConf.h b/src/client/SchemaConf.h deleted file mode 100644 index 3bb0674..0000000 --- a/src/client/SchemaConf.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2017 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_STORE_SCHEMA_CONF_H__ -#define __CONTEXT_STORE_SCHEMA_CONF_H__ - -#include -#include -#include "Schema.h" - -namespace ctx { - - class SchemaConf { - public: - SchemaConf(); - ~SchemaConf(); - - bool init(); - - Schema& getSchema(const std::string& uri); - }; - -} - -#endif /* __CONTEXT_STORE_SCHEMA_CONF_H__ */ -- 2.7.4 From ec7cc34ddb4cf5265761b4d1bdc66f4d1b7d628f Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Wed, 8 Mar 2017 21:39:12 +0900 Subject: [PATCH 04/16] Implement APIs for platform-managed stores Change-Id: Ifa8e2f3fd0fab6f9f807e39696932aea73467d15 Signed-off-by: Mu-Woong Lee --- include/ContextStore.h | 4 +-- include/ContextStoreManager.h | 1 - include/ContextStoreSearchQuery.h | 7 +++- src/client-dummy/ContextStore.cpp | 4 +-- src/client-dummy/ContextStoreManager.cpp | 5 --- src/client-dummy/ContextStoreSearchQuery.cpp | 22 +++++++++++- src/client/ContextStore.cpp | 4 +-- src/client/ContextStoreManager.cpp | 23 ++---------- src/client/ContextStoreSearchQuery.cpp | 22 +++++++++++- src/client/PlatformManagedStore.cpp | 53 ++++++++++++++++++---------- src/client/PlatformManagedStore.h | 9 ++--- src/shared/ContextStoreTypesPrivate.h | 30 ++++++++++++++-- 12 files changed, 124 insertions(+), 60 deletions(-) diff --git a/include/ContextStore.h b/include/ContextStore.h index bd1222d..214c988 100644 --- a/include/ContextStore.h +++ b/include/ContextStore.h @@ -28,8 +28,8 @@ namespace ctx { public: virtual ~ContextStore(); - virtual int insert(const std::vector& columns, Tuple* record); - virtual int insert(const std::vector& columns, std::vector& records); + virtual int insert(const std::string& columns, Tuple* record); + virtual int insert(const std::string& columns, std::vector& records); virtual int retrieve(const ContextStoreSearchQuery& query, std::vector& result); virtual int remove(const std::string& selection); diff --git a/include/ContextStoreManager.h b/include/ContextStoreManager.h index 4fbdcdc..60f2a3e 100644 --- a/include/ContextStoreManager.h +++ b/include/ContextStoreManager.h @@ -29,7 +29,6 @@ namespace ctx { ~ContextStoreManager(); int getStore(const std::string& uri, ContextStore** contextStore); - int removeStore(const std::string& uri); }; } diff --git a/include/ContextStoreSearchQuery.h b/include/ContextStoreSearchQuery.h index 2c0d187..c279676 100644 --- a/include/ContextStoreSearchQuery.h +++ b/include/ContextStoreSearchQuery.h @@ -38,7 +38,12 @@ namespace ctx { ContextStoreSearchQuery& setSortOrder(const std::string& sortOrder); ContextStoreSearchQuery& setLimit(unsigned int limit); - bool valid(); + const std::string& getProjection() const; + const std::string& getSelection() const; + const std::string& getSortOrder() const; + unsigned int getLimit() const; + + bool valid() const; private: std::string __projection; diff --git a/src/client-dummy/ContextStore.cpp b/src/client-dummy/ContextStore.cpp index dd2b1d2..3ac2af0 100644 --- a/src/client-dummy/ContextStore.cpp +++ b/src/client-dummy/ContextStore.cpp @@ -27,12 +27,12 @@ ContextStore::~ContextStore() { } -int ContextStore::insert(const std::vector& columns, Tuple* record) +int ContextStore::insert(const std::string& columns, Tuple* record) { return E_SUPPORT; } -int ContextStore::insert(const std::vector& columns, std::vector& records) +int ContextStore::insert(const std::string& columns, std::vector& records) { return E_SUPPORT; } diff --git a/src/client-dummy/ContextStoreManager.cpp b/src/client-dummy/ContextStoreManager.cpp index f19a6dc..9178735 100644 --- a/src/client-dummy/ContextStoreManager.cpp +++ b/src/client-dummy/ContextStoreManager.cpp @@ -31,8 +31,3 @@ int ContextStoreManager::getStore(const std::string& uri, ContextStore** store) { return E_SUPPORT; } - -int ContextStoreManager::removeStore(const std::string& uri) -{ - return E_SUPPORT; -} diff --git a/src/client-dummy/ContextStoreSearchQuery.cpp b/src/client-dummy/ContextStoreSearchQuery.cpp index 25f60d4..bda66fe 100644 --- a/src/client-dummy/ContextStoreSearchQuery.cpp +++ b/src/client-dummy/ContextStoreSearchQuery.cpp @@ -59,7 +59,27 @@ ContextStoreSearchQuery& ContextStoreSearchQuery::setLimit(unsigned int limit) return *this; } -bool ContextStoreSearchQuery::valid() +const std::string& ContextStoreSearchQuery::getProjection() const +{ + return __projection; +} + +const std::string& ContextStoreSearchQuery::getSelection() const +{ + return __selection; +} + +const std::string& ContextStoreSearchQuery::getSortOrder() const +{ + return __sortOrder; +} + +unsigned int ContextStoreSearchQuery::getLimit() const +{ + return __limit; +} + +bool ContextStoreSearchQuery::valid() const { return false; } diff --git a/src/client/ContextStore.cpp b/src/client/ContextStore.cpp index dd2b1d2..3ac2af0 100644 --- a/src/client/ContextStore.cpp +++ b/src/client/ContextStore.cpp @@ -27,12 +27,12 @@ ContextStore::~ContextStore() { } -int ContextStore::insert(const std::vector& columns, Tuple* record) +int ContextStore::insert(const std::string& columns, Tuple* record) { return E_SUPPORT; } -int ContextStore::insert(const std::vector& columns, std::vector& records) +int ContextStore::insert(const std::string& columns, std::vector& records) { return E_SUPPORT; } diff --git a/src/client/ContextStoreManager.cpp b/src/client/ContextStoreManager.cpp index 69679f4..9dcca7c 100644 --- a/src/client/ContextStoreManager.cpp +++ b/src/client/ContextStoreManager.cpp @@ -15,8 +15,6 @@ */ #include -#include -#include #include #include #include "PlatformManagedStore.h" @@ -29,10 +27,9 @@ static ServiceProxy* __getServiceProxy() return &proxy; } -template -static ContextStore* __createStore(const std::string& uri, int* error) +static ContextStore* __createPlatformManagedStore(const std::string& uri, int* error) { - StoreType* store = new(std::nothrow) StoreType(__getServiceProxy()); + PlatformManagedStore* store = new(std::nothrow) PlatformManagedStore(__getServiceProxy()); if (!store) { *error = E_NO_MEM; @@ -67,23 +64,9 @@ int ContextStoreManager::getStore(const std::string& uri, ContextStore** store) int error = E_NONE; ContextStore* _store = NULL; - _store = __createStore(uri, &error); + _store = __createPlatformManagedStore(uri, &error); IF_FAIL_RETURN(_store, error); *store = _store; return E_NONE; } - -int ContextStoreManager::removeStore(const std::string& uri) -{ - _D("Dropping the store %s", uri.c_str()); - - int error = E_NONE; - - error = PlatformManagedStore::destroy(uri, __getServiceProxy()); - - if (error != E_NONE) - _E("%s", CTX_ERROR_STR(error)); - - return error; -} diff --git a/src/client/ContextStoreSearchQuery.cpp b/src/client/ContextStoreSearchQuery.cpp index 995f92d..d401725 100644 --- a/src/client/ContextStoreSearchQuery.cpp +++ b/src/client/ContextStoreSearchQuery.cpp @@ -59,7 +59,27 @@ ContextStoreSearchQuery& ContextStoreSearchQuery::setLimit(unsigned int limit) return *this; } -bool ContextStoreSearchQuery::valid() +const std::string& ContextStoreSearchQuery::getProjection() const +{ + return __projection; +} + +const std::string& ContextStoreSearchQuery::getSelection() const +{ + return __selection; +} + +const std::string& ContextStoreSearchQuery::getSortOrder() const +{ + return __sortOrder; +} + +unsigned int ContextStoreSearchQuery::getLimit() const +{ + return __limit; +} + +bool ContextStoreSearchQuery::valid() const { // TODO return true; diff --git a/src/client/PlatformManagedStore.cpp b/src/client/PlatformManagedStore.cpp index 575d29e..1c0c8f8 100644 --- a/src/client/PlatformManagedStore.cpp +++ b/src/client/PlatformManagedStore.cpp @@ -29,36 +29,53 @@ PlatformManagedStore::~PlatformManagedStore() int PlatformManagedStore::init(const std::string& uri) { - // TODO - return E_SUPPORT; + int error = __proxy->call(METHOD_GET_ACCESS, g_variant_new("(s)", uri.c_str())); + __uri = uri; + return error; } -int PlatformManagedStore::insert(const std::vector& columns, Tuple* record) +int PlatformManagedStore::insert(const std::string& columns, Tuple* record) { - // TODO - return E_SUPPORT; + if (columns.empty() || record == NULL) { + delete record; + return E_PARAM; + } + + GVariant* param = g_variant_new("(ssv)", __uri.c_str(), columns.c_str(), Tuple::toGVariant(record)); + return __proxy->call(METHOD_INSERT, param); } -int PlatformManagedStore::insert(const std::vector& columns, std::vector& records) +int PlatformManagedStore::insert(const std::string& columns, std::vector& records) { - // TODO - return E_SUPPORT; + if (columns.empty() || records.empty()) { + for (auto& tuple : records) { + delete tuple; + } + records.clear(); + return E_PARAM; + } + + GVariant* param = g_variant_new("(ssv)", __uri.c_str(), columns.c_str(), Tuple::toGVariant(records)); + return __proxy->call(METHOD_INSERT, param); } int PlatformManagedStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) { - // TODO - return E_SUPPORT; -} + IF_FAIL_RETURN(query.valid(), E_PARAM); -int PlatformManagedStore::remove(const std::string& selection) -{ - // TODO - return E_SUPPORT; + GVariant* param = g_variant_new("(ssssu)", __uri.c_str(), + query.getProjection().c_str(), query.getSelection().c_str(), + query.getSortOrder().c_str(), static_cast(query.getLimit())); + + GVariant* outParam = NULL; + int error = __proxy->call(METHOD_RETRIEVE, param, &outParam); + IF_FAIL_RETURN_TAG(error == E_NONE, error, _E, "%s", CTX_ERROR_STR(error)); + + result = Tuple::buildFrom(g_variant_get_child_value(outParam, IDX_RETRIEVE_RESULT)); + return E_NONE; } -int PlatformManagedStore::destroy(const std::string& uri, ServiceProxy* proxy) +int PlatformManagedStore::remove(const std::string& selection) { - // TODO - return E_NONE; + return __proxy->call(METHOD_REMOVE, g_variant_new("(ss)", __uri.c_str(), selection.c_str())); } diff --git a/src/client/PlatformManagedStore.h b/src/client/PlatformManagedStore.h index fbc7bcd..c5d0786 100644 --- a/src/client/PlatformManagedStore.h +++ b/src/client/PlatformManagedStore.h @@ -18,6 +18,8 @@ #define __CONTEXT_STORE_PLATFORM_MANAGED_STORE_H__ #include +#include +#include #include #include @@ -30,15 +32,14 @@ namespace ctx { int init(const std::string& uri); - int insert(const std::vector& columns, Tuple* record); - int insert(const std::vector& columns, std::vector& records); + int insert(const std::string& columns, Tuple* record); + int insert(const std::string& columns, std::vector& records); int retrieve(const ContextStoreSearchQuery& query, std::vector& result); int remove(const std::string& selection); - static int destroy(const std::string& uri, ServiceProxy* proxy); - private: ServiceProxy* __proxy; + std::string __uri; }; } diff --git a/src/shared/ContextStoreTypesPrivate.h b/src/shared/ContextStoreTypesPrivate.h index 5322d5f..e482308 100644 --- a/src/shared/ContextStoreTypesPrivate.h +++ b/src/shared/ContextStoreTypesPrivate.h @@ -23,9 +23,33 @@ #define CTX_CONTEXT_STORE "ContextStore" #define CTX_CONTEXT_STORE_SPEC \ - "" \ - " " \ - " " \ + "" \ + " " \ + " " \ + "" \ + "" \ + " " \ + " " \ + " " \ + "" \ + "" \ + " " \ + " " \ + " " \ + " " \ + " " \ + " " \ + "" \ + "" \ + " " \ + " " \ "" +#define METHOD_GET_ACCESS "GetAccess" +#define METHOD_INSERT "Insert" +#define METHOD_RETRIEVE "Retrieve" +#define METHOD_REMOVE "Remove" + +#define IDX_RETRIEVE_RESULT 0 + #endif -- 2.7.4 From 7a2782856a772c192430d1f56c6f4f62d7839ece Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:38:58 +0900 Subject: [PATCH 05/16] Define shared constants Change-Id: I17cb3508e80e0699383add2a805923841eab620d Signed-off-by: Mu-Woong Lee --- src/shared/ContextStoreTypesPrivate.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/shared/ContextStoreTypesPrivate.h b/src/shared/ContextStoreTypesPrivate.h index e482308..786fec0 100644 --- a/src/shared/ContextStoreTypesPrivate.h +++ b/src/shared/ContextStoreTypesPrivate.h @@ -50,6 +50,16 @@ #define METHOD_RETRIEVE "Retrieve" #define METHOD_REMOVE "Remove" +#define IDX_URI 0 #define IDX_RETRIEVE_RESULT 0 +#define MAX_RETENTION 8760 // one year +#define MAX_LIMIT 100000 +#define PRIVILEGE_DELIM ";" + +#define BASE_PATH "context-store" + +#define COL_TIMESTAMP "__timestamp" +#define COL_OWNER_ID "__ownerId" + #endif -- 2.7.4 From e3d9d6af8b3122afab2dba7668f764afaffc9bc0 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:43:09 +0900 Subject: [PATCH 06/16] API: fix bugs of query construction & GVariant extraction Change-Id: Id47ea1dcddff38717f1aecc8acd8a2cb15bef7a7 Signed-off-by: Mu-Woong Lee --- src/client/ContextStoreSearchQuery.cpp | 5 ++++- src/client/PlatformManagedStore.cpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/client/ContextStoreSearchQuery.cpp b/src/client/ContextStoreSearchQuery.cpp index d401725..e1161b6 100644 --- a/src/client/ContextStoreSearchQuery.cpp +++ b/src/client/ContextStoreSearchQuery.cpp @@ -27,7 +27,10 @@ ContextStoreSearchQuery::ContextStoreSearchQuery() : ContextStoreSearchQuery::ContextStoreSearchQuery(const std::string& projection, const std::string& selection, const std::string& sortOrder, unsigned int limit) : - __limit(0) + __projection(projection), + __selection(selection), + __sortOrder(sortOrder), + __limit(limit) { } diff --git a/src/client/PlatformManagedStore.cpp b/src/client/PlatformManagedStore.cpp index 1c0c8f8..ed913af 100644 --- a/src/client/PlatformManagedStore.cpp +++ b/src/client/PlatformManagedStore.cpp @@ -71,7 +71,10 @@ int PlatformManagedStore::retrieve(const ContextStoreSearchQuery& query, std::ve int error = __proxy->call(METHOD_RETRIEVE, param, &outParam); IF_FAIL_RETURN_TAG(error == E_NONE, error, _E, "%s", CTX_ERROR_STR(error)); - result = Tuple::buildFrom(g_variant_get_child_value(outParam, IDX_RETRIEVE_RESULT)); + GVariant* vals = NULL; + g_variant_get(outParam, "(v)", &vals); + + result = Tuple::buildFrom(vals); return E_NONE; } -- 2.7.4 From 0a7cbcee315f5c24df8f4554352b4ff8d4ca098c Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:44:26 +0900 Subject: [PATCH 07/16] Add a schema sample xml Change-Id: Ie233bd104e6538d91857ba4468eaeff6c4e2c7ad Signed-off-by: Mu-Woong Lee --- schema/sample.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 schema/sample.xml diff --git a/schema/sample.xml b/schema/sample.xml new file mode 100644 index 0000000..6e67c38 --- /dev/null +++ b/schema/sample.xml @@ -0,0 +1,11 @@ + + + + http://tizen.org/privilege/location + http://tizen.org/privilege/internal/service + + + + + + -- 2.7.4 From a4402485c82fbe50366d4d096b2619b192a313d3 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:47:08 +0900 Subject: [PATCH 08/16] Implement DatabaseManager in the server Change-Id: Ib88f7fd66583ce4632d52a003b169c9479d664b4 Signed-off-by: Mu-Woong Lee --- src/server/DatabaseManager.cpp | 111 +++++++++++++++++++++++++++++++++++++++++ src/server/DatabaseManager.h | 48 ++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 src/server/DatabaseManager.cpp create mode 100644 src/server/DatabaseManager.h diff --git a/src/server/DatabaseManager.cpp b/src/server/DatabaseManager.cpp new file mode 100644 index 0000000..bb5a3bb --- /dev/null +++ b/src/server/DatabaseManager.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2017 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 +#include "DatabaseManager.h" + +using namespace ctx; + +Database* DatabaseManager::__systemDatabase = NULL; +Database* DatabaseManager::__userDatabase = NULL; + +DatabaseManager::DatabaseManager() +{ +} + +bool DatabaseManager::openSystem() +{ + IF_FAIL_RETURN(!isSystemReady(), true); + + __systemDatabase = new(std::nothrow) PlatformDatabase(BASE_PATH); + IF_FAIL_RETURN_TAG(__systemDatabase, false, _E, "%s", CTX_ERROR_STR(E_NO_MEM)); + + if (!__systemDatabase->open()) { + _E("DB open failed"); + delete __systemDatabase; + __systemDatabase = NULL; + return false; + } + + _I("System DB opened"); + return true; +} + +bool DatabaseManager::openUser(uid_t uid) +{ + if (isUserReady()) { + _W("Closing the current DB"); + closeUser(); + } + + __userDatabase = new(std::nothrow) PlatformDatabase(BASE_PATH, uid); + IF_FAIL_RETURN_TAG(__userDatabase, false, _E, "%s", CTX_ERROR_STR(E_NO_MEM)); + + if (!__userDatabase->open()) { + _E("DB open failed"); + delete __userDatabase; + __userDatabase = NULL; + return false; + } + + _I("User DB opened"); + return true; +} + +void DatabaseManager::closeSystem() +{ + if (!__systemDatabase) + return; + + __systemDatabase->close(); + _I("System DB closed"); + + delete __systemDatabase; + __systemDatabase = NULL; + +} + +void DatabaseManager::closeUser() +{ + if (!__userDatabase) + return; + + __userDatabase->close(); + _I("User DB closed"); + + delete __userDatabase; + __userDatabase = NULL; +} + +bool DatabaseManager::isSystemReady() +{ + return (__systemDatabase != NULL); +} + +bool DatabaseManager::isUserReady() +{ + return (__userDatabase != NULL); +} + +Database& DatabaseManager::getSystem() +{ + return *__systemDatabase; +} + +Database& DatabaseManager::getUser() +{ + return *__userDatabase; +} diff --git a/src/server/DatabaseManager.h b/src/server/DatabaseManager.h new file mode 100644 index 0000000..b274443 --- /dev/null +++ b/src/server/DatabaseManager.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017 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_STORE_DATABASE_MANAGER_H__ +#define __CONTEXT_STORE_DATABASE_MANAGER_H__ + +#include +#include + +namespace ctx { + + class DatabaseManager { + public: + static bool openSystem(); + static bool openUser(uid_t uid); + + static void closeSystem(); + static void closeUser(); + + static bool isSystemReady(); + static bool isUserReady(); + + static Database& getSystem(); + static Database& getUser(); + + private: + DatabaseManager(); + + static Database* __systemDatabase; + static Database* __userDatabase; + }; + +} + +#endif /* __CONTEXT_STORE_DATABASE_MANAGER_H__ */ -- 2.7.4 From 88dc1c784794d4854fae5a5b707beb15942e250d Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:52:19 +0900 Subject: [PATCH 09/16] Implement SchemaLoader in the server Change-Id: I86e52eace38cdd0710e198712df1aa894bf68dd4 Signed-off-by: Mu-Woong Lee --- packaging/context-store.spec | 4 +- script/init.sql | 7 + src/server/SchemaLoader.cpp | 375 +++++++++++++++++++++++++++++++++++++++++++ src/server/SchemaLoader.h | 91 +++++++++++ 4 files changed, 476 insertions(+), 1 deletion(-) create mode 100644 script/init.sql create mode 100644 src/server/SchemaLoader.cpp create mode 100644 src/server/SchemaLoader.h diff --git a/packaging/context-store.spec b/packaging/context-store.spec index 573a430..5c4a488 100644 --- a/packaging/context-store.spec +++ b/packaging/context-store.spec @@ -61,7 +61,8 @@ make %{?_smp_mflags} %install %make_install - +mkdir -p %{buildroot}%{TZ_SYS_DATA}/%{name} +install -m 0644 script/init.sql %{buildroot}%{TZ_SYS_DATA}/%{name}/ %post /sbin/ldconfig @@ -89,6 +90,7 @@ popd %manifest packaging/%{name}.manifest %{_libdir}/lib%{name}-client-genuine.so* %{_libdir}/lib%{name}-server-genuine.so* +%{TZ_SYS_DATA}/%{name}/*.sql %files devel diff --git a/script/init.sql b/script/init.sql new file mode 100644 index 0000000..d0ec0c9 --- /dev/null +++ b/script/init.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS ContextStoreSchema ( + "uri" TEXT PRIMARY KEY, + "retention" INTEGER NOT NULL DEFAULT 0, + "limit" INTEGER NOT NULL DEFAULT 0, + "readPrivileges" TEXT NOT NULL, + "writePrivileges" TEXT NOT NULL +); diff --git a/src/server/SchemaLoader.cpp b/src/server/SchemaLoader.cpp new file mode 100644 index 0000000..6185762 --- /dev/null +++ b/src/server/SchemaLoader.cpp @@ -0,0 +1,375 @@ +/* + * Copyright (c) 2017 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 +#include +#include +#include +#include +#include +#include "DatabaseManager.h" +#include "SchemaLoader.h" + +/* Schema XML example: + + + + + http://tizen.org/privilege/healthinfo + http://tizen.org/privilege/internal/service + + + + + + + */ + +#define URI_REGEX(CATEGORY) R"~(^http:\/\/[\w-]+(\.[\w-]+)*\/)~" CATEGORY R"~(\/[\w-]+(\.[\w-]+)*(\/[\w-]+(\.[\w-]+)*)*$)~" +#define COL_REGEX "^[A-Za-z]+\\w*$" + +#define CHK_NAME(NODE, NAME) (!xmlStrcmp(NODE->name, (const xmlChar*)(NAME))) + +using namespace ctx; + +static std::string __getXmlAttribute(xmlNode* node, const char* name) +{ + xmlChar* prop = xmlGetProp(node, reinterpret_cast(name)); + std::string out; + if (prop) { + out = reinterpret_cast(prop); + xmlFree(prop); + } + return out; +} + +static std::string __getXmlContent(xmlNode* node) +{ + xmlChar* content = xmlNodeGetContent(node); + _D("%s", content); + std::string out; + if (content) { + out = reinterpret_cast(content); + xmlFree(content); + } + return out; +} + +Schema::Schema() : + retention(0), + limit(0) +{ +} + +bool Schema::valid() +{ + if (retention > MAX_RETENTION || limit > MAX_LIMIT) + return false; + + if (retention == 0 && limit == 0) + return false; + + if (attributes.empty()) + return false; + + std::regex uriRegex(URI_REGEX("contextstore"), std::regex::optimize); + if (!std::regex_match(uri, uriRegex)) + return false; + + std::regex privilegeRegex(URI_REGEX("privilege"), std::regex::optimize); + for (auto& privil : readPrivileges) { + if (!std::regex_match(privil, privilegeRegex)) + return false; + } + + for (auto& privil : writePrivileges) { + if (!std::regex_match(privil, privilegeRegex)) + return false; + } + + std::regex columnNameRegex(COL_REGEX, std::regex::optimize); + for (auto& attr: attributes) { + if (!std::regex_match(attr.first, columnNameRegex)) + return false; + } + + return true; +} + +SchemaLoader::SchemaLoader() +{ +} + +SchemaLoader::~SchemaLoader() +{ +} + +bool SchemaLoader::load() +{ + bool result = true; + __getDatabase().beginTransaction(); + + if (!__createMetadataTables()) { + _E("Metadata table creation failed"); + result = false; + } + + try { + if (result && !__loadSchemaDir(__getSchemaDir())) { + _W("Schema loading failed"); + result = false; + } + } catch (std::regex_error& e) { + _E("%s", e.what()); + result = false; + } + + __getDatabase().endTransaction(); + return result; +} + +bool SchemaLoader::__createMetadataTables() +{ + std::string path = PathUtil::getSystemPath(TZ_SYS_DATA, BASE_PATH "/init.sql"); + IF_FAIL_RETURN(!path.empty(), false); + + std::ifstream inFile(path); + std::stringstream buffer; + buffer << inFile.rdbuf(); + + return __getDatabase().execute(buffer.str(), NULL); +} + +bool SchemaLoader::__loadSchemaDir(const std::string& dirPath) +{ + DIR* dir = NULL; + struct dirent entry; + struct dirent *result = NULL; + + dir = opendir(dirPath.c_str()); + IF_FAIL_RETURN_TAG(dir, false, _E, "Failed to open: %s", dirPath.c_str()); + + std::regex xmlFileRegex("^.*\\.(xml|XML)$", std::regex::optimize); + + while (true) { + if (readdir_r(dir, &entry, &result) != 0) + continue; + + if (result == NULL) + break; + + std::string filename = entry.d_name; + + if (!std::regex_match(filename, xmlFileRegex)) + continue; + + __parseSchemaFile(dirPath + "/" + filename); + } + + closedir(dir); + return true; +} + +void SchemaLoader::__parseSchemaFile(const std::string& filePath) +{ + _D("Parsing %s", filePath.c_str()); + + xmlDoc *doc = xmlReadFile(filePath.c_str(), NULL, 0); + IF_FAIL_VOID_TAG(doc, _W, "Could not parse %s", filePath.c_str()); + + xmlNode *node = xmlDocGetRootElement(doc); + + if (!node || !CHK_NAME(node, "ContextStore")) { + _W("Not a ContextStore schema XML"); + xmlFreeDoc(doc); + return; + } + + for (node = node->children; node != NULL; node = node->next) { + if (node->type != XML_ELEMENT_NODE) + continue; + + if (!CHK_NAME(node, "Schema")) { + _D("Skip '%s'", node->name); + continue; + } + + Schema schema; + if (__parseSchema(node, schema)) { + __insertMetadata(schema); + __createStoreTable(schema); + } + } + + xmlFreeDoc(doc); + return; +} + +bool SchemaLoader::__parseSchema(xmlNode* node, Schema& schema) +{ + std::string retention = __getXmlAttribute(node, "retention"); + std::string limit = __getXmlAttribute(node, "limit"); + schema.uri = __getXmlAttribute(node, "uri"); + + for (node = node->children; node != NULL; node = node->next) { + if (node->type != XML_ELEMENT_NODE) + continue; + + if (CHK_NAME(node, "Privilege")) + __parsePrivilege(node, schema); + else if (CHK_NAME(node, "Attribute")) + __parseAttribute(node, schema); + } + + if (!retention.empty()) + schema.retention = std::atoi(retention.c_str()); + + if (!limit.empty()) + schema.limit = std::atoi(limit.c_str()); + + return schema.valid(); +} + +void SchemaLoader::__parsePrivilege(xmlNode* node, Schema& schema) +{ + std::string access = __getXmlAttribute(node, "access"); + std::string privil = __getXmlContent(node); + + if (access == "Read") + schema.readPrivileges.push_back(privil); + else if (access == "Write") + schema.writePrivileges.push_back(privil); +} + +void SchemaLoader::__parseAttribute(xmlNode* node, Schema& schema) +{ + std::string name = __getXmlAttribute(node, "name"); + std::string type = __getXmlAttribute(node, "type"); + + IF_FAIL_VOID(!name.empty()); + + if (type == "Integer") + schema.attributes.push_back(std::make_pair(name, Schema::AttributeType::INTEGER)); + else if (type == "Real") + schema.attributes.push_back(std::make_pair(name, Schema::AttributeType::REAL)); + else if (type == "Text") + schema.attributes.push_back(std::make_pair(name, Schema::AttributeType::TEXT)); +} + +void SchemaLoader::__insertMetadata(Schema& schema) +{ + char* updateSql = NULL; + char* insertSql = NULL; + int length = 0; + + std::string readPrivil; + std::string writePrivil; + + for (auto& item : schema.readPrivileges) { + readPrivil.append(item); + readPrivil.append(PRIVILEGE_DELIM); + } + + for (auto& item : schema.writePrivileges) { + writePrivil.append(item); + writePrivil.append(PRIVILEGE_DELIM); + } + + length = asprintf(&updateSql, + "UPDATE ContextStoreSchema" + " SET uri='%s', retention=%u, \"limit\"=%u, readPrivileges='%s', writePrivileges='%s'" + " WHERE uri='%s'", + schema.uri.c_str(), schema.retention, schema.limit, readPrivil.c_str(), writePrivil.c_str(), + schema.uri.c_str()); + if (length <= 0) { + _E("Memory allocation failed"); + return; + } + + length = asprintf(&insertSql, + "INSERT OR IGNORE INTO ContextStoreSchema" + " (uri, retention, \"limit\", readPrivileges, writePrivileges)" + " VALUES ('%s', %u, %u, '%s', '%s')", + schema.uri.c_str(), schema.retention, schema.limit, readPrivil.c_str(), writePrivil.c_str()); + if (length <= 0) { + g_free(updateSql); + _E("Memory allocation failed"); + return; + } + + __getDatabase().execute(updateSql, NULL); + __getDatabase().execute(insertSql, NULL); + + g_free(updateSql); + g_free(insertSql); +} + +void SchemaLoader::__createStoreTable(Schema& schema) +{ + std::string attr = + COL_TIMESTAMP " DATETIME DEFAULT CURRENT_TIMESTAMP," + COL_OWNER_ID " TEXT NOT NULL DEFAULT ''"; + + for (auto& item : schema.attributes) { + attr = attr + "," + item.first; + switch (item.second) { + case Schema::AttributeType::INTEGER: + attr = attr + " INTEGER NOT NULL DEFAULT 0"; + break; + case Schema::AttributeType::REAL: + attr = attr + " REAL NOT NULL DEFAULT 0"; + break; + default: + attr = attr + " TEXT NOT NULL DEFAULT ''"; + break; + } + } + + std::string query = + "CREATE TABLE IF NOT EXISTS [" + schema.uri + "] (" + attr + ")"; + + __getDatabase().execute(query.c_str(), NULL); +} + + +SystemSchemaLoader::SystemSchemaLoader() +{ +} + +Database& SystemSchemaLoader::__getDatabase() +{ + return DatabaseManager::getSystem(); +} + +std::string SystemSchemaLoader::__getSchemaDir() +{ + return PathUtil::getSystemPath(TZ_SYS_DATA, BASE_PATH "/system"); +} + + +UserSchemaLoader::UserSchemaLoader() +{ +} + +Database& UserSchemaLoader::__getDatabase() +{ + return DatabaseManager::getUser(); +} + +std::string UserSchemaLoader::__getSchemaDir() +{ + return PathUtil::getSystemPath(TZ_SYS_DATA, BASE_PATH "/user"); +} diff --git a/src/server/SchemaLoader.h b/src/server/SchemaLoader.h new file mode 100644 index 0000000..4b08934 --- /dev/null +++ b/src/server/SchemaLoader.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017 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_STORE_SCHEMA_LOADER_H__ +#define __CONTEXT_STORE_SCHEMA_LOADER_H__ + +#include +#include +#include +#include +#include +#include + +namespace ctx { + + class Schema { + public: + enum class AttributeType { + UNDEFINED = 0, + INTEGER = 1, + REAL = 2, + TEXT = 3 + }; + + Schema(); + bool valid(); + + std::string uri; + unsigned int retention; + unsigned int limit; + std::vector readPrivileges; + std::vector writePrivileges; + std::vector> attributes; + }; + + class SchemaLoader { + public: + virtual ~SchemaLoader(); + + bool load(); + + private: + bool __createMetadataTables(); + bool __loadSchemaDir(const std::string& dirPath); + void __parseSchemaFile(const std::string& filePath); + bool __parseSchema(xmlNode* node, Schema& schema); + void __parsePrivilege(xmlNode* node, Schema& schema); + void __parseAttribute(xmlNode* node, Schema& schema); + void __insertMetadata(Schema& schema); + void __createStoreTable(Schema& schema); + + virtual std::string __getSchemaDir() = 0; + virtual Database& __getDatabase() = 0; + + protected: + SchemaLoader(); + }; + + class SystemSchemaLoader : public SchemaLoader { + public: + SystemSchemaLoader(); + + private: + std::string __getSchemaDir(); + Database& __getDatabase(); + }; + + class UserSchemaLoader : public SchemaLoader { + public: + UserSchemaLoader(); + + private: + std::string __getSchemaDir(); + Database& __getDatabase(); + }; +} + +#endif /* __CONTEXT_STORE_SCHEMA_LOADER_H__ */ -- 2.7.4 From 48d1ecc062c7f6c413a0f2abbed0465f3e6d0cad Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:55:52 +0900 Subject: [PATCH 10/16] Implement StoreManager & store handle classes in the server Change-Id: Idcb901e3d7fa4b6bfdb753d428b7ad06c0668c9f Signed-off-by: Mu-Woong Lee --- src/server/Store.cpp | 97 ++++++++++++++++++++++++++++ src/server/Store.h | 74 +++++++++++++++++++++ src/server/StoreManager.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++ src/server/StoreManager.h | 51 +++++++++++++++ src/server/SystemStore.cpp | 53 +++++++++++++++ src/server/SystemStore.h | 40 ++++++++++++ src/server/UserStore.cpp | 56 ++++++++++++++++ src/server/UserStore.h | 40 ++++++++++++ 8 files changed, 563 insertions(+) create mode 100644 src/server/Store.cpp create mode 100644 src/server/Store.h create mode 100644 src/server/StoreManager.cpp create mode 100644 src/server/StoreManager.h create mode 100644 src/server/SystemStore.cpp create mode 100644 src/server/SystemStore.h create mode 100644 src/server/UserStore.cpp create mode 100644 src/server/UserStore.h diff --git a/src/server/Store.cpp b/src/server/Store.cpp new file mode 100644 index 0000000..1bd089b --- /dev/null +++ b/src/server/Store.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2017 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 "Store.h" + +using namespace ctx; + +Store::Metadata::Metadata() : + retention(0), + limit(0) +{ +} + +Store::Store() +{ +} + +Store::~Store() +{ +} + +bool Store::permitted(ContextStoreClient& client) +{ + return __readable(client) || __writable(client); +} + +const std::string& Store::getPath() +{ + return metadata.path; +} + +int Store::insert(ContextStoreClient& client, const std::string& columns, std::vector& tuples) +{ + //TODO: handling owner's ID + //TODO: delete expired data + if (!__writable(client)) + return E_ACCESS; + + _D("Inserting %u tuples of (%s) to %s", tuples.size(), columns.c_str(), metadata.uri.c_str()); + + int error = __getDatabase().insert(metadata.uri, columns, tuples) ? E_NONE : E_PARAM; + return error; +} + +int Store::retrieve(ContextStoreClient& client, + const std::string& projection, const std::string& selection, + const std::string& sortOrder, unsigned int limit, std::vector* tuples) +{ + if (!__readable(client)) + return E_ACCESS; + + _D("Retrieving (%s) from %s", projection.c_str(), metadata.uri.c_str()); + + std::string query = "SELECT " + projection + " FROM [" + metadata.uri + "]"; + if (!selection.empty()) + query = query + " WHERE " + selection; + if (!sortOrder.empty()) + query = query + " ORDER BY " + sortOrder; + if (limit > 0) + query = query + " LIMIT " + std::to_string(limit); + + if (!__getDatabase().execute(query, tuples)) + return E_PARAM; + + return E_NONE; +} + +int Store::remove(ContextStoreClient& client, const std::string selection) +{ + //TODO: handling owner's ID + if (!__writable(client)) + return E_ACCESS; + + _D("Removing from %s", metadata.uri.c_str()); + + std::string query = "DELETE FROM [" + metadata.uri + "]"; + if (!selection.empty()) + query = query + " WHERE " + selection; + + if (!__getDatabase().execute(query, NULL)) + return E_PARAM; + + return E_NONE; +} diff --git a/src/server/Store.h b/src/server/Store.h new file mode 100644 index 0000000..c51778a --- /dev/null +++ b/src/server/Store.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2017 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_STORE_STORE_H__ +#define __CONTEXT_STORE_STORE_H__ + +#include +#include +#include +#include +#include + +namespace ctx { + + class ContextStoreClient; + + class Store { + public: + class Metadata { + public: + Metadata(); + + std::string path; + std::string uri; + unsigned int retention; + unsigned int limit; + std::vector readPrivileges; + std::vector writePrivileges; + }; + + virtual ~Store(); + + bool permitted(ContextStoreClient& client); + + const std::string& getPath(); + + int insert(ContextStoreClient& client, const std::string& columns, std::vector& tuples); + + int retrieve(ContextStoreClient& client, + const std::string& projection, const std::string& selection, + const std::string& sortOrder, unsigned int limit, + std::vector* tuples); + + int remove(ContextStoreClient& client, const std::string selection); + + protected: + Store(); + + Metadata metadata; + + private: + virtual bool __readable(ContextStoreClient& client) = 0; + virtual bool __writable(ContextStoreClient& client) = 0; + virtual Database& __getDatabase() = 0; + + friend class StoreManager; + }; + +} + +#endif /* __CONTEXT_STORE_STORE_H__ */ diff --git a/src/server/StoreManager.cpp b/src/server/StoreManager.cpp new file mode 100644 index 0000000..9c36f95 --- /dev/null +++ b/src/server/StoreManager.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2017 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 "DatabaseManager.h" +#include "UserStore.h" +#include "SystemStore.h" +#include "StoreManager.h" + +#define CACHE_SIZE 3 + +using namespace ctx; + +StoreManager::StoreManager() +{ +} + +StoreManager::~StoreManager() +{ +} + +Store* StoreManager::__findStore(std::list& stores, const std::string& uri) +{ + for (auto& store : stores) { + if (store->metadata.uri != uri) + continue; + + _D("Cache hit!"); + return store; + } + + return NULL; +} + +std::vector __tokenize(std::string& in, const char* delim) +{ + std::vector tokens; + std::size_t begin = 0; + std::size_t end = in.find(delim, 0); + + while (end != std::string::npos) { + tokens.push_back(in.substr(begin, end - begin)); + begin = end + 1; + end = in.find(delim, begin); + } + + return tokens; +} + +template +Store* StoreManager::__createStore(Database& database, std::list& stores, const std::string& uri) +{ + std::string query = + "SELECT retention, \"limit\", readPrivileges, writePrivileges" + " FROM ContextStoreSchema" + " WHERE uri='" + uri + "'"; + + std::vector tuples; + if (!database.execute(query, COL_INT64 COL_INT64 COL_STRING COL_STRING, NULL, &tuples)) { + _E("DB search failed"); + return NULL; + } + if (tuples.empty()) { + _W("Not found"); + return NULL; + } + + int64_t retention = 0; + int64_t limit = 0; + std::string readPrivil; + std::string writePrivil; + + tuples[0]->getAt(0, &retention); + tuples[0]->getAt(1, &limit); + tuples[0]->getAt(2, &readPrivil); + tuples[0]->getAt(3, &writePrivil); + + delete tuples[0]; + + _D("URI: %s, Retention: %u, Limit: %u", uri.c_str(), static_cast(retention), static_cast(limit)); + _D("Read: %s", readPrivil.c_str()); + _D("Write: %s", writePrivil.c_str()); + + StoreType* store = new(std::nothrow) StoreType(); + IF_FAIL_RETURN_TAG(store, NULL, _E, "Memory allocation failed"); + + store->metadata.uri = uri; + store->metadata.retention= static_cast(retention); + store->metadata.limit = static_cast(limit); + store->metadata.readPrivileges = __tokenize(readPrivil, PRIVILEGE_DELIM); + store->metadata.writePrivileges = __tokenize(writePrivil, PRIVILEGE_DELIM); + + stores.push_front(store); + if (stores.size() > CACHE_SIZE) { + delete stores.back(); + stores.pop_back(); + } + + return store; +} + +void StoreManager::flushUserCache() +{ + for (auto& store : __userStores) { + delete store; + } + __userStores.clear(); +} + +void StoreManager::flushSystemCache() +{ + for (auto& store : __systemStores) { + delete store; + } + __systemStores.clear(); +} + +Store* StoreManager::getUserStore(const std::string& uri) +{ + _D("Checking the user DB"); + Store* store = __findStore(__userStores, uri); + if (store) + return store; + + store = __createStore(DatabaseManager::getUser(), __userStores, uri); + if (store) + return store; + + return getSystemStore(uri); +} + +Store* StoreManager::getSystemStore(const std::string& uri) +{ + _D("Checking the system DB"); + Store* store = __findStore(__systemStores, uri); + if (store) + return store; + + return __createStore(DatabaseManager::getSystem(), __systemStores, uri); +} diff --git a/src/server/StoreManager.h b/src/server/StoreManager.h new file mode 100644 index 0000000..f8d8e63 --- /dev/null +++ b/src/server/StoreManager.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2017 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_STORE_STORE_MANAGER_H__ +#define __CONTEXT_STORE_STORE_MANAGER_H__ + +#include +#include + +namespace ctx { + + class Store; + class Database; + + class StoreManager { + public: + StoreManager(); + ~StoreManager(); + + Store* getUserStore(const std::string& uri); + Store* getSystemStore(const std::string& uri); + + void flushUserCache(); + void flushSystemCache(); + + private: + Store* __findStore(std::list& stores, const std::string& uri); + + template + Store* __createStore(Database& database, std::list& stores, const std::string& uri); + + std::list __userStores; + std::list __systemStores; + }; + +} + +#endif /* __CONTEXT_STORE_STORE_MANAGER_H__ */ diff --git a/src/server/SystemStore.cpp b/src/server/SystemStore.cpp new file mode 100644 index 0000000..9bf5fb7 --- /dev/null +++ b/src/server/SystemStore.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2017 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 "ContextStoreClient.h" +#include "DatabaseManager.h" +#include "SystemStore.h" + +using namespace ctx; + +SystemStore::SystemStore() +{ +} + +SystemStore::~SystemStore() +{ +} + +bool SystemStore::__readable(ContextStoreClient& client) +{ + if (!client.hasPrivileges(metadata.readPrivileges)) + return false; + + return true; +} + +bool SystemStore::__writable(ContextStoreClient& client) +{ + if (!client.isSystem()) + return false; + + if (!client.hasPrivileges(metadata.writePrivileges)) + return false; + + return true; +} + +Database& SystemStore::__getDatabase() +{ + return DatabaseManager::getSystem(); +} diff --git a/src/server/SystemStore.h b/src/server/SystemStore.h new file mode 100644 index 0000000..12b6865 --- /dev/null +++ b/src/server/SystemStore.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 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_STORE_SYSTEM_STORE_H__ +#define __CONTEXT_STORE_SYSTEM_STORE_H__ + +#include "Store.h" + +namespace ctx { + + class SystemStore : public Store { + public: + ~SystemStore(); + + private: + SystemStore(); + + bool __readable(ContextStoreClient& client); + bool __writable(ContextStoreClient& client); + Database& __getDatabase(); + + friend class StoreManager; + }; + +} + +#endif /* __CONTEXT_STORE_SYSTEM_STORE_H__ */ diff --git a/src/server/UserStore.cpp b/src/server/UserStore.cpp new file mode 100644 index 0000000..e636190 --- /dev/null +++ b/src/server/UserStore.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017 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 "ContextStoreClient.h" +#include "DatabaseManager.h" +#include "UserStore.h" + +using namespace ctx; + +UserStore::UserStore() +{ +} + +UserStore::~UserStore() +{ +} + +bool UserStore::__readable(ContextStoreClient& client) +{ + if (client.isSystem()) + return false; + + if (!client.hasPrivileges(metadata.readPrivileges)) + return false; + + return true; +} + +bool UserStore::__writable(ContextStoreClient& client) +{ + if (client.isSystem()) + return false; + + if (!client.hasPrivileges(metadata.writePrivileges)) + return false; + + return true; +} + +Database& UserStore::__getDatabase() +{ + return DatabaseManager::getUser(); +} diff --git a/src/server/UserStore.h b/src/server/UserStore.h new file mode 100644 index 0000000..0ff8896 --- /dev/null +++ b/src/server/UserStore.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 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_STORE_USER_STORE_H__ +#define __CONTEXT_STORE_USER_STORE_H__ + +#include "Store.h" + +namespace ctx { + + class UserStore : public Store { + public: + ~UserStore(); + + private: + UserStore(); + + bool __readable(ContextStoreClient& client); + bool __writable(ContextStoreClient& client); + Database& __getDatabase(); + + friend class StoreManager; + }; + +} + +#endif /* __CONTEXT_STORE_USER_STORE_H__ */ -- 2.7.4 From 0ba8ec22afcf314b87dcdc701ab37c147dfbed47 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:58:23 +0900 Subject: [PATCH 11/16] Implement ContextStoreService & ContextStoreClient Change-Id: I6cfe0a430aa6abbebf5ef86ee3b1198990a67f1f Signed-off-by: Mu-Woong Lee --- include/ContextStoreService.h | 7 +++ src/server/ContextStoreClient.cpp | 126 ++++++++++++++++++++++++++++++++++++- src/server/ContextStoreClient.h | 14 +++++ src/server/ContextStoreService.cpp | 38 ++++++++++- 4 files changed, 181 insertions(+), 4 deletions(-) diff --git a/include/ContextStoreService.h b/include/ContextStoreService.h index ca384dc..828424e 100644 --- a/include/ContextStoreService.h +++ b/include/ContextStoreService.h @@ -24,6 +24,8 @@ namespace ctx { + class StoreManager; + class EXPORT_API ContextStoreService : public ServiceBase { public: ContextStoreService(GDBusConnection* conn); @@ -31,6 +33,8 @@ namespace ctx { bool isUserService(); + StoreManager& getStoreManager(); + protected: bool prepare(); void cleanup(); @@ -39,6 +43,9 @@ namespace ctx { void onUserActivated(); void onUserDeactivated(); + + private: + StoreManager* __storeManager; }; } diff --git a/src/server/ContextStoreClient.cpp b/src/server/ContextStoreClient.cpp index 6f64c7f..3d72f19 100644 --- a/src/server/ContextStoreClient.cpp +++ b/src/server/ContextStoreClient.cpp @@ -14,8 +14,11 @@ * limitations under the License. */ -#include +#include #include +#include "ContextStoreService.h" +#include "Store.h" +#include "StoreManager.h" #include "ContextStoreClient.h" using namespace ctx; @@ -31,10 +34,129 @@ ContextStoreClient::~ContextStoreClient() 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(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(E_PARAM); + } + return uri; +} + +StoreManager& ContextStoreClient::__getStoreManager() +{ + return static_cast(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(E_PARAM); + } + + return store; +} + +void ContextStoreClient::__getAccess(Store& store, MethodCall& methodCall) +{ + if (!store.permitted(*this)) + throw static_cast(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(E_PARAM); + } + + std::vector tuples = Tuple::buildFrom(vals); + if (tuples.empty()) { + throw static_cast(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(E_PARAM); + + std::vector 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(E_PARAM); + + methodCall.reply(store.remove(*this, selection)); +} diff --git a/src/server/ContextStoreClient.h b/src/server/ContextStoreClient.h index 0f51a77..a106acd 100644 --- a/src/server/ContextStoreClient.h +++ b/src/server/ContextStoreClient.h @@ -21,6 +21,9 @@ namespace ctx { + class StoreManager; + class Store; + class ContextStoreClient : public ClientBase { public: ContextStoreClient(ServiceBase* hostService, const std::string& busName); @@ -28,6 +31,17 @@ namespace ctx { void onMethodCalled(MethodCall* methodCall); void onDisconnected(); + + private: + void __verifyUid(uid_t uid); + std::string __getStoreUri(GVariant* param); + StoreManager& __getStoreManager(); + Store* __getStore(const std::string& uri); + + void __getAccess(Store& store, MethodCall& methodCall); + void __insert(Store& store, MethodCall& methodCall); + void __retrieve(Store& store, MethodCall& methodCall); + void __remove(Store& store, MethodCall& methodCall); }; } diff --git a/src/server/ContextStoreService.cpp b/src/server/ContextStoreService.cpp index c8dce21..e4162e1 100644 --- a/src/server/ContextStoreService.cpp +++ b/src/server/ContextStoreService.cpp @@ -17,11 +17,15 @@ #include #include #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) { } @@ -36,12 +40,21 @@ bool ContextStoreService::isUserService() 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) @@ -51,8 +64,29 @@ 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; } -- 2.7.4 From 9e7ad557894537aca6097c1c9840608b39a59187 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 21:46:16 +0900 Subject: [PATCH 12/16] Rename a pamareter to be consistant through the APIs Change-Id: I5338edc46a0f2bbde50badb1cb2e1ab51a6a6f80 Signed-off-by: Mu-Woong Lee --- include/ContextStore.h | 2 +- src/client-dummy/ContextStore.cpp | 2 +- src/client/ContextStore.cpp | 2 +- src/client/PlatformManagedStore.cpp | 4 ++-- src/client/PlatformManagedStore.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/ContextStore.h b/include/ContextStore.h index 214c988..cf6c098 100644 --- a/include/ContextStore.h +++ b/include/ContextStore.h @@ -30,7 +30,7 @@ namespace ctx { virtual int insert(const std::string& columns, Tuple* record); virtual int insert(const std::string& columns, std::vector& records); - virtual int retrieve(const ContextStoreSearchQuery& query, std::vector& result); + virtual int retrieve(const ContextStoreSearchQuery& query, std::vector& records); virtual int remove(const std::string& selection); protected: diff --git a/src/client-dummy/ContextStore.cpp b/src/client-dummy/ContextStore.cpp index 3ac2af0..3f14d37 100644 --- a/src/client-dummy/ContextStore.cpp +++ b/src/client-dummy/ContextStore.cpp @@ -37,7 +37,7 @@ int ContextStore::insert(const std::string& columns, std::vector& record return E_SUPPORT; } -int ContextStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +int ContextStore::retrieve(const ContextStoreSearchQuery& query, std::vector& records) { return E_SUPPORT; } diff --git a/src/client/ContextStore.cpp b/src/client/ContextStore.cpp index 3ac2af0..3f14d37 100644 --- a/src/client/ContextStore.cpp +++ b/src/client/ContextStore.cpp @@ -37,7 +37,7 @@ int ContextStore::insert(const std::string& columns, std::vector& record return E_SUPPORT; } -int ContextStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +int ContextStore::retrieve(const ContextStoreSearchQuery& query, std::vector& records) { return E_SUPPORT; } diff --git a/src/client/PlatformManagedStore.cpp b/src/client/PlatformManagedStore.cpp index ed913af..57f41c4 100644 --- a/src/client/PlatformManagedStore.cpp +++ b/src/client/PlatformManagedStore.cpp @@ -59,7 +59,7 @@ int PlatformManagedStore::insert(const std::string& columns, std::vector return __proxy->call(METHOD_INSERT, param); } -int PlatformManagedStore::retrieve(const ContextStoreSearchQuery& query, std::vector& result) +int PlatformManagedStore::retrieve(const ContextStoreSearchQuery& query, std::vector& records) { IF_FAIL_RETURN(query.valid(), E_PARAM); @@ -74,7 +74,7 @@ int PlatformManagedStore::retrieve(const ContextStoreSearchQuery& query, std::ve GVariant* vals = NULL; g_variant_get(outParam, "(v)", &vals); - result = Tuple::buildFrom(vals); + records = Tuple::buildFrom(vals); return E_NONE; } diff --git a/src/client/PlatformManagedStore.h b/src/client/PlatformManagedStore.h index c5d0786..77c3d38 100644 --- a/src/client/PlatformManagedStore.h +++ b/src/client/PlatformManagedStore.h @@ -34,7 +34,7 @@ namespace ctx { int insert(const std::string& columns, Tuple* record); int insert(const std::string& columns, std::vector& records); - int retrieve(const ContextStoreSearchQuery& query, std::vector& result); + int retrieve(const ContextStoreSearchQuery& query, std::vector& records); int remove(const std::string& selection); private: -- 2.7.4 From c2cc395595a65655a09c5206b0823ac0c296cd36 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Thu, 16 Mar 2017 15:26:52 +0900 Subject: [PATCH 13/16] Rename the pkg config template file Change-Id: I91a9526adeb9baacfe9d5b1dfca89faca87cd59c Signed-off-by: Mu-Woong Lee --- context-store.pc.in => pkg-config.pc.in | 0 src/client-dummy/CMakeLists.txt | 2 +- src/server-dummy/CMakeLists.txt | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename context-store.pc.in => pkg-config.pc.in (100%) diff --git a/context-store.pc.in b/pkg-config.pc.in similarity index 100% rename from context-store.pc.in rename to pkg-config.pc.in diff --git a/src/client-dummy/CMakeLists.txt b/src/client-dummy/CMakeLists.txt index f247700..1e5e9f4 100644 --- a/src/client-dummy/CMakeLists.txt +++ b/src/client-dummy/CMakeLists.txt @@ -24,7 +24,7 @@ SET(PC_REQUIRED ${DEPS}) SET(PC_LDFLAGS -l${target}) CONFIGURE_FILE( - ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc.in + ${CMAKE_SOURCE_DIR}/pkg-config.pc.in ${CMAKE_SOURCE_DIR}/${target}.pc @ONLY ) diff --git a/src/server-dummy/CMakeLists.txt b/src/server-dummy/CMakeLists.txt index d5a3a4d..1f0fdc0 100644 --- a/src/server-dummy/CMakeLists.txt +++ b/src/server-dummy/CMakeLists.txt @@ -24,7 +24,7 @@ SET(PC_REQUIRED ${DEPS}) SET(PC_LDFLAGS -l${target}) CONFIGURE_FILE( - ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc.in + ${CMAKE_SOURCE_DIR}/pkg-config.pc.in ${CMAKE_SOURCE_DIR}/${target}.pc @ONLY ) -- 2.7.4 From dd771220622302d6c0e6d11387698b10b5c7203e Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Thu, 16 Mar 2017 17:46:06 +0900 Subject: [PATCH 14/16] Disable redundant virtual packages Change-Id: Id77460862b678d34797c0f6a4be54eecb6499dc4 Signed-off-by: Mu-Woong Lee --- packaging/context-store.spec | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packaging/context-store.spec b/packaging/context-store.spec index 5c4a488..7b27165 100644 --- a/packaging/context-store.spec +++ b/packaging/context-store.spec @@ -14,9 +14,6 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(context-common-server) BuildRequires: pkgconfig(context-common-client) -Provides: %{name}-profile_tv = %{version}-%{release} -Provides: %{name}-profile_ivi = %{version}-%{release} -Provides: %{name}-profile_common = %{version}-%{release} %description Tizen Context Store Service @@ -26,8 +23,11 @@ Tizen Context Store Service Summary: Genuine Context Store service server and client libraries Requires: %{name} = %{version}-%{release} Requires: context-service -Provides: %{name}-profile_mobile = %{version}-%{release} -Provides: %{name}-profile_wearable = %{version}-%{release} +#Provides: %{name}-profile_common = %{version}-%{release} +#Provides: %{name}-profile_mobile = %{version}-%{release} +#Provides: %{name}-profile_wearable = %{version}-%{release} +#Provides: %{name}-profile_tv = %{version}-%{release} +#Provides: %{name}-profile_ivi = %{version}-%{release} %global __provides_exclude ^.*-genuine\\.so.*$ %description genuine -- 2.7.4 From 8b46e8a9e34b243267c84750a30a19e184da1c7a Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Thu, 16 Mar 2017 20:04:32 +0900 Subject: [PATCH 15/16] Set the default limit parameter of selection queries Change-Id: I1965cb8970d1c12417cbf479b3e055656a794776 Signed-off-by: Mu-Woong Lee --- src/client/ContextStoreSearchQuery.cpp | 6 ++++-- src/shared/ContextStoreTypesPrivate.h | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/client/ContextStoreSearchQuery.cpp b/src/client/ContextStoreSearchQuery.cpp index e1161b6..cc606c8 100644 --- a/src/client/ContextStoreSearchQuery.cpp +++ b/src/client/ContextStoreSearchQuery.cpp @@ -20,7 +20,7 @@ using namespace ctx; ContextStoreSearchQuery::ContextStoreSearchQuery() : - __limit(0) + __limit(DEFAULT_QUERY_LIMIT) { } @@ -84,6 +84,8 @@ unsigned int ContextStoreSearchQuery::getLimit() const bool ContextStoreSearchQuery::valid() const { - // TODO + if (__projection.empty()) + return false; + return true; } diff --git a/src/shared/ContextStoreTypesPrivate.h b/src/shared/ContextStoreTypesPrivate.h index 786fec0..1d3a5b8 100644 --- a/src/shared/ContextStoreTypesPrivate.h +++ b/src/shared/ContextStoreTypesPrivate.h @@ -62,4 +62,6 @@ #define COL_TIMESTAMP "__timestamp" #define COL_OWNER_ID "__ownerId" +#define DEFAULT_QUERY_LIMIT 10 + #endif -- 2.7.4 From 73ab667ec0b2409421fe73f706b4b487acaa500d Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Thu, 16 Mar 2017 21:02:55 +0900 Subject: [PATCH 16/16] Code skeleton of the query validity checker Change-Id: I90c2607563dd19b5b2555abacbbe5f58b5a995fe Signed-off-by: Mu-Woong Lee --- src/server/ContextStoreClient.cpp | 30 ++++++++++++++++-- src/server/QueryChecker.cpp | 57 +++++++++++++++++++++++++++++++++++ src/server/QueryChecker.h | 40 ++++++++++++++++++++++++ src/server/SchemaLoader.cpp | 1 - src/shared/ContextStoreTypesPrivate.h | 2 ++ 5 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 src/server/QueryChecker.cpp create mode 100644 src/server/QueryChecker.h diff --git a/src/server/ContextStoreClient.cpp b/src/server/ContextStoreClient.cpp index 3d72f19..096f35e 100644 --- a/src/server/ContextStoreClient.cpp +++ b/src/server/ContextStoreClient.cpp @@ -19,10 +19,13 @@ #include "ContextStoreService.h" #include "Store.h" #include "StoreManager.h" +#include "QueryChecker.h" #include "ContextStoreClient.h" using namespace ctx; +static QueryChecker __queryChecker; + ContextStoreClient::ContextStoreClient(ServiceBase* hostService, const std::string& busName) : ClientBase(hostService, busName) { @@ -50,6 +53,9 @@ void ContextStoreClient::onMethodCalled(MethodCall* methodCall) } } catch (int error) { methodCall->reply(error); + } catch (std::exception& e) { + _E("Exception: %s", e.what()); + methodCall->reply(E_FAILED); } delete methodCall; @@ -71,10 +77,10 @@ std::string ContextStoreClient::__getStoreUri(GVariant* param) { const char* uri = NULL; g_variant_get_child(param, IDX_URI, "&s", &uri); - if (!uri) { - _E("Invalid URI"); + + if (!uri) throw static_cast(E_PARAM); - } + return uri; } @@ -87,6 +93,9 @@ Store* ContextStoreClient::__getStore(const std::string& uri) { Store* store = NULL; + if (!__queryChecker.validateUri(uri)) + throw static_cast(E_PARAM); + if (isSystem()) { store = __getStoreManager().getSystemStore(uri); } else { @@ -121,6 +130,9 @@ void ContextStoreClient::__insert(Store& store, MethodCall& methodCall) throw static_cast(E_PARAM); } + if (!__queryChecker.validateProjection(cols)) + throw static_cast(E_PARAM); + std::vector tuples = Tuple::buildFrom(vals); if (tuples.empty()) { throw static_cast(E_PARAM); @@ -141,6 +153,15 @@ void ContextStoreClient::__retrieve(Store& store, MethodCall& methodCall) if (!projection || !selection || !sortOrder) throw static_cast(E_PARAM); + if (!__queryChecker.validateProjection(projection)) + throw static_cast(E_PARAM); + + if (!__queryChecker.validateSelection(selection)) + throw static_cast(E_PARAM); + + if (!__queryChecker.validateSortOrder(sortOrder)) + throw static_cast(E_PARAM); + std::vector tuples; int error = store.retrieve(*this, projection, selection, sortOrder, limit, &tuples); if (error != E_NONE) @@ -158,5 +179,8 @@ void ContextStoreClient::__remove(Store& store, MethodCall& methodCall) if (!selection) throw static_cast(E_PARAM); + if (!__queryChecker.validateSelection(selection)) + throw static_cast(E_PARAM); + methodCall.reply(store.remove(*this, selection)); } diff --git a/src/server/QueryChecker.cpp b/src/server/QueryChecker.cpp new file mode 100644 index 0000000..20a8bed --- /dev/null +++ b/src/server/QueryChecker.cpp @@ -0,0 +1,57 @@ + +/* + * Copyright (c) 2017 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 +#include "QueryChecker.h" + +using namespace ctx; + +QueryChecker::QueryChecker() +{ +} + +QueryChecker::~QueryChecker() +{ +} + +bool QueryChecker::validateUri(const std::string& uri) +{ + static std::regex uriRegex(URI_REGEX("contextstore"), std::regex::optimize); + if (!std::regex_match(uri, uriRegex)) { + _E("Invalid parameter"); + return false; + } + return true; +} + +bool QueryChecker::validateProjection(const std::string& projection) +{ + // TODO + return true; +} + +bool QueryChecker::validateSelection(const std::string& selection) +{ + // TODO + return true; +} + +bool QueryChecker::validateSortOrder(const std::string& sortOrder) +{ + // TODO + return true; +} diff --git a/src/server/QueryChecker.h b/src/server/QueryChecker.h new file mode 100644 index 0000000..eaaf932 --- /dev/null +++ b/src/server/QueryChecker.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 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_STORE_QUERY_CHECKER_H__ +#define __CONTEXT_STORE_QUERY_CHECKER_H__ + +#include + +namespace ctx { + + class QueryChecker { + public: + QueryChecker(); + ~QueryChecker(); + + bool validateUri(const std::string& uri); + + bool validateProjection(const std::string& projection); + + bool validateSelection(const std::string& selection); + + bool validateSortOrder(const std::string& sortOrder); + }; + +} + +#endif /* __CONTEXT_STORE_QUERY_CHECKER_H__ */ diff --git a/src/server/SchemaLoader.cpp b/src/server/SchemaLoader.cpp index 6185762..b1b01bd 100644 --- a/src/server/SchemaLoader.cpp +++ b/src/server/SchemaLoader.cpp @@ -38,7 +38,6 @@ */ -#define URI_REGEX(CATEGORY) R"~(^http:\/\/[\w-]+(\.[\w-]+)*\/)~" CATEGORY R"~(\/[\w-]+(\.[\w-]+)*(\/[\w-]+(\.[\w-]+)*)*$)~" #define COL_REGEX "^[A-Za-z]+\\w*$" #define CHK_NAME(NODE, NAME) (!xmlStrcmp(NODE->name, (const xmlChar*)(NAME))) diff --git a/src/shared/ContextStoreTypesPrivate.h b/src/shared/ContextStoreTypesPrivate.h index 1d3a5b8..7daa99f 100644 --- a/src/shared/ContextStoreTypesPrivate.h +++ b/src/shared/ContextStoreTypesPrivate.h @@ -64,4 +64,6 @@ #define DEFAULT_QUERY_LIMIT 10 +#define URI_REGEX(CATEGORY) R"~(^http:\/\/[\w-]+(\.[\w-]+)*\/)~" CATEGORY R"~(\/[\w-]+(\.[\w-]+)*(\/[\w-]+(\.[\w-]+)*)*$)~" + #endif -- 2.7.4