--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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__ */
}
}
- return NULL;
+ throw E_SUPPORT;
}
ContextPublisher::~ContextPublisher()
}
}
+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;
}
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));
#include <ServerUtil.h>
#include <Conf.h>
#include <JobAction.h>
+#include "ContextManager.h"
#include "SchedTimer.h"
#include "JobRunner.h"
#include "JobManager.h"
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;
}
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;
}
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());
}
}
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());
}
}
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);
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);
#include <JobSchedulerService.h>
#include "JobManager.h"
#include "MethodCallHandler.h"
+#include "ContextManager.h"
#include "SchedTimer.h"
#define ROOT_UID 0
bool JobSchedulerService::prepare()
{
+ ContextManager::init();
SchedTimer::init();
__jobManagers.emplace(ROOT_UID, new JobManager(ROOT_UID));
return true;
void JobSchedulerService::cleanup()
{
+ ContextManager::release();
SchedTimer::release();
for (auto& it : __jobManagers) {
delete it.second;
_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);
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