Add ContextManager and integrate it with JobManager 16/140216/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 07:25:25 +0000 (16:25 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 08:11:06 +0000 (17:11 +0900)
Change-Id: I0a6c929cc77abf93cc5320ec476b8ddcf9bd4e3f
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/ContextManager.cpp [new file with mode: 0644]
src/server/ContextManager.h [new file with mode: 0644]
src/server/ContextPublisher.cpp
src/server/ContextPublisher.h
src/server/JobManager.cpp
src/server/JobManager.h
src/server/JobSchedulerService.cpp
src/server/MethodCallHandler.cpp
src/server/publisher/GpsState.cpp

diff --git a/src/server/ContextManager.cpp b/src/server/ContextManager.cpp
new file mode 100644 (file)
index 0000000..7f6f27c
--- /dev/null
@@ -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.
+ */
+
+#include <SharedUtil.h>
+#include "ContextPublisher.h"
+#include "ContextManager.h"
+
+#define SYSTEM_UID     0
+
+using namespace ctx;
+
+ContextManager* ContextManager::__instance = NULL;
+
+ContextManager::ContextManager()
+{
+}
+
+ContextManager::~ContextManager()
+{
+       for (auto& pr : __publishers) {
+               delete pr.second;
+       }
+}
+
+void ContextManager::init()
+{
+       __instance = new ContextManager();
+}
+
+void ContextManager::release()
+{
+       delete __instance;
+       __instance = NULL;
+}
+
+ContextPublisher* ContextManager::getPublisher(const std::string& uri, uid_t uid)
+{
+       IF_FAIL_RETURN(__instance, NULL);
+       return __instance->__getPublisher(uri.c_str(), uid);
+}
+
+ContextPublisher* ContextManager::__getPublisher(const char* uri, uid_t uid)
+{
+       ContextPublisher* pubs = NULL;
+
+       if ((pubs = __find(uri, SYSTEM_UID)))
+               return pubs;
+
+       if ((pubs = __find(uri, uid)))
+               return pubs;
+
+       try {
+               pubs = ContextPublisher::build(uri, uid);
+       } catch (const int err) {
+               _I("'%s' is not supported", uri);
+               return NULL;
+       }
+
+       if (pubs->isUserContext())
+               __publishers.emplace(uid, pubs);
+       else
+               __publishers.emplace(SYSTEM_UID, pubs);
+
+       return pubs;
+}
+
+ContextPublisher* ContextManager::__find(const char* uri, uid_t uid)
+{
+       auto range = __publishers.equal_range(uid);
+
+       for (auto it = range.first; it != range.second; ++it) {
+               if (STR_EQ(it->second->getUri(), uri)) {
+                       return it->second;
+               }
+       }
+
+       return NULL;
+}
diff --git a/src/server/ContextManager.h b/src/server/ContextManager.h
new file mode 100644 (file)
index 0000000..25973b8
--- /dev/null
@@ -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_JOB_SCHEDULER_CONTEXT_MANAGER_H__
+#define __CONTEXT_JOB_SCHEDULER_CONTEXT_MANAGER_H__
+
+#include <map>
+#include <ContextTypes.h>
+#include "ContextPublisher.h"
+
+namespace ctx {
+
+       class ContextManager {
+       public:
+               ~ContextManager();
+
+               static void init();
+               static void release();
+               static ContextPublisher* getPublisher(const std::string& uri, uid_t uid);
+
+       private:
+               ContextManager();
+
+               ContextPublisher* __getPublisher(const char* uri, uid_t uid);
+               ContextPublisher* __find(const char* uri, uid_t uid);
+
+               std::multimap<uid_t, ContextPublisher*> __publishers;
+
+               static ContextManager* __instance;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_CONTEXT_MANAGER_H__ */
index ead189c63fba7b46cd813c6a6bfb896ccd2413b7..378f4d7f32b2974ba91bfc3931240cc56a6ec31c 100644 (file)
@@ -37,7 +37,7 @@ ContextPublisher* ContextPublisher::build(const char* uri, uid_t uid)
                }
        }
 
-       return NULL;
+       throw E_SUPPORT;
 }
 
 ContextPublisher::~ContextPublisher()
@@ -64,8 +64,21 @@ void ContextPublisher::removeObserver(IContextObserver* observer)
        }
 }
 
+const char* ContextPublisher::getPrivilege() const
+{
+       // Most context items are not privileged.
+       return NULL;
+}
+
+bool ContextPublisher::isReadable() const
+{
+       // Most context items are directly readable without subscribing them.
+       return true;
+}
+
 bool ContextPublisher::isUserContext() const
 {
+       // Most context items are system-level data.
        return false;
 }
 
index 78c85e2aeb74427ddde5315917680544613e27e9..aa4afcc8c47520f02201aa9fd559cf6a5b55c290 100644 (file)
@@ -42,7 +42,11 @@ namespace ctx {
                void removeObserver(IContextObserver* observer);
 
                virtual const char* getUri() const = 0;
+               virtual const char* getPrivilege() const;
+
+               virtual bool isReadable() const;
                virtual bool isUserContext() const;
+
                virtual const Json::Value& getData();
 
                static void registerPublisher(const char* uri, ContextPublisher* (*creator)(uid_t));
index be2e58537b1292652332b3df829bffe9c87644dd..1a96d9dfa5984d1983113c995ca5aec0b6a20b77 100644 (file)
@@ -18,6 +18,7 @@
 #include <ServerUtil.h>
 #include <Conf.h>
 #include <JobAction.h>
+#include "ContextManager.h"
 #include "SchedTimer.h"
 #include "JobRunner.h"
 #include "JobManager.h"
@@ -91,9 +92,14 @@ uid_t JobManager::getUid() const
        return __uid;
 }
 
-bool JobManager::isSupported(JobContext::Type type, const std::string& uri)
+bool JobManager::isSupported(JobContext::Type type, const std::string& uri, IClient* client)
 {
-       //TODO: availability check
+       ContextPublisher* pubs = ContextManager::getPublisher(uri, client->getUid());
+       IF_FAIL_RETURN(pubs, false);
+
+       if (type == JobContext::Type::REQUIREMENT && !pubs->isReadable())
+               return false;
+
        return true;
 }
 
@@ -284,9 +290,11 @@ JobInfo* JobManager::__getDuplicate(const std::string& ownerId, JobInfo* target)
        return NULL;
 }
 
-bool JobManager::__isPermitted(IClient* client, const std::string& uri)
+bool JobManager::__isPermitted(const std::string& uri, IClient* client)
 {
-       //TODO: permission check
+       ContextPublisher* pubs = ContextManager::getPublisher(uri, client->getUid());
+       IF_FAIL_RETURN(pubs, false);
+       IF_FAIL_RETURN(client->hasPrivilege(pubs->getPrivilege()), false);
        return true;
 }
 
@@ -326,10 +334,10 @@ void JobManager::__verifyTriggers(OnDemandJobInfo* jobInfo, IClient* owner)
                const std::string& uri = trig->getUri();
                _D("Check %s", uri.c_str());
 
-               IF_FAIL_THROW_TAG(isSupported(JobContext::Type::TRIGGER, uri), E_SUPPORT,
+               IF_FAIL_THROW_TAG(isSupported(JobContext::Type::TRIGGER, uri, owner), E_SUPPORT,
                                _W, "'%s' is not supported.", uri.c_str());
 
-               IF_FAIL_THROW_TAG(__isPermitted(owner, uri), E_ACCESS,
+               IF_FAIL_THROW_TAG(__isPermitted(uri, owner), E_ACCESS,
                                _W, "'%s' is not allowed to use '%s'.", owner->getName().c_str(), uri.c_str());
        }
 }
@@ -343,10 +351,10 @@ void JobManager::__verifyRequirements(JobInfo* jobInfo, IClient* owner)
                const std::string& uri = req->getUri();
                _D("Check %s", uri.c_str());
 
-               IF_FAIL_THROW_TAG(isSupported(JobContext::Type::REQUIREMENT, uri), E_SUPPORT,
+               IF_FAIL_THROW_TAG(isSupported(JobContext::Type::REQUIREMENT, uri, owner), E_SUPPORT,
                                _W, "'%s' is not supported.", uri.c_str());
 
-               IF_FAIL_THROW_TAG(__isPermitted(owner, uri), E_ACCESS,
+               IF_FAIL_THROW_TAG(__isPermitted(uri, owner), E_ACCESS,
                                _W, "'%s' is not allowed to use '%s'.", owner->getName().c_str(), uri.c_str());
        }
 }
index 0245bd275fdaee151b6a4283660bb931b3e762ef..4926773bdf8e2de58e9b844741631726fe3e3fd0 100644 (file)
@@ -34,7 +34,7 @@ namespace ctx {
 
                uid_t getUid() const;
 
-               bool isSupported(JobContext::Type type, const std::string& uri);
+               bool isSupported(JobContext::Type type, const std::string& uri, IClient* client);
 
                int addJob(JobInfo* jobInfo, IClient* owner);
                int startJob(int jobId, IClient* owner);
@@ -55,7 +55,7 @@ namespace ctx {
 
                JobInfo* __getDuplicate(const std::string& onwerId, JobInfo* target);
 
-               bool __isPermitted(IClient* client, const std::string& uri);
+               bool __isPermitted(const std::string& uri, IClient* client);
 
                void __verifyPeriodicJob(PeriodicJobInfo* jobInfo, IClient* owner);
                void __verifyOnDemandJob(OnDemandJobInfo* jobInfo, IClient* owner);
index e9ca58487e8c90091573945ad43ce069329bbd61..81ea9cfdd8e1dc952a21d0609a4c5d2bf331727a 100644 (file)
@@ -20,6 +20,7 @@
 #include <JobSchedulerService.h>
 #include "JobManager.h"
 #include "MethodCallHandler.h"
+#include "ContextManager.h"
 #include "SchedTimer.h"
 
 #define ROOT_UID       0
@@ -58,6 +59,7 @@ IMethodCallHandler* JobSchedulerService::createMethodCallHandler(IClient* client
 
 bool JobSchedulerService::prepare()
 {
+       ContextManager::init();
        SchedTimer::init();
        __jobManagers.emplace(ROOT_UID, new JobManager(ROOT_UID));
        return true;
@@ -65,6 +67,7 @@ bool JobSchedulerService::prepare()
 
 void JobSchedulerService::cleanup()
 {
+       ContextManager::release();
        SchedTimer::release();
        for (auto& it : __jobManagers) {
                delete it.second;
index 1edc8954e8d8d8ff9e457dc313d1e86c2338f997..4bdf7aacd9885f99d60981e693fe22ed29efb16d 100644 (file)
@@ -92,7 +92,7 @@ void MethodCallHandler::__isSupported(IMethodCall& methodCall)
 
        _D("Checking %s(%d)", uri, type);
 
-       bool supported = __getJobManager().isSupported(static_cast<JobContext::Type>(type), uri);
+       bool supported = __getJobManager().isSupported(static_cast<JobContext::Type>(type), uri, __caller);
        IF_FAIL_THROW(supported, static_cast<int>(E_SUPPORT));
 
        methodCall.reply(E_NONE);
index cf7370eb6b4a1e8f6a97da8b496a4b6f9b9bcc6d..49700a638b20a5454843ab113c464a1d915ec546 100644 (file)
@@ -65,11 +65,15 @@ GpsState::GpsState(uid_t uid)
        bool supported = false;
        system_info_get_platform_bool("tizen.org/feature/location.gps", &supported);
        IF_FAIL_THROW(supported, E_SUPPORT);
+
+       _D("Created");
 }
 
 GpsState::~GpsState()
 {
        unsubscribe();
+
+       _D("Destroyed");
 }
 
 const char* GpsState::getUri() const