--- /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 <vector>
+#include <ContextTypes.h>
+#include <SharedUtil.h>
+#include "IContextObserver.h"
+#include "ContextPublisher.h"
+
+using namespace ctx;
+
+static std::vector<std::pair<const char*, ContextPublisher* (*)(uid_t)>> __creators;
+
+void ContextPublisher::registerPublisher(const char* uri, ContextPublisher* (*creator)(uid_t))
+{
+ __creators.emplace_back(uri, creator);
+}
+
+ContextPublisher* ContextPublisher::build(const char* uri, uid_t uid)
+{
+ for (auto& it : __creators) {
+ if (STR_EQ(it.first, uri)) {
+ return (it.second)(uid);
+ }
+ }
+
+ return NULL;
+}
+
+ContextPublisher::~ContextPublisher()
+{
+}
+
+void ContextPublisher::addObserver(IContextObserver* observer)
+{
+ if (__observers.empty()) {
+ read();
+ subscribe();
+ }
+
+ __observers.push_back(observer);
+}
+
+void ContextPublisher::removeObserver(IContextObserver* observer)
+{
+ __observers.remove(observer);
+
+ if (__observers.empty()) {
+ unsubscribe();
+ clearData();
+ }
+}
+
+bool ContextPublisher::isUserContext()
+{
+ return false;
+}
+
+const Json::Value& ContextPublisher::getData()
+{
+ return __data;
+}
+
+void ContextPublisher::notifyObservers()
+{
+ for (auto& observer : __observers) {
+ observer->ready();
+ }
+}
+
+bool ContextPublisher::isReady()
+{
+ return !__data.isNull();
+}
+
+void ContextPublisher::clearData()
+{
+ __data = Json::nullValue;
+}
--- /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_PUBLISHER_H__
+#define __CONTEXT_JOB_SCHEDULER_CONTEXT_PUBLISHER_H__
+
+#include <list>
+#include <json/json.h>
+
+#define REGISTER_PUBLISHER(URI, PUBS) \
+ static ctx::ContextPublisher* __create(uid_t uid) \
+ { \
+ return new PUBS(uid); \
+ } \
+ __attribute__((__constructor__)) static void __registerPublisher() \
+ { \
+ ctx::ContextPublisher::registerPublisher(URI, __create); \
+ }
+
+namespace ctx {
+
+ class IContextObserver;
+
+ class ContextPublisher {
+ public:
+ virtual ~ContextPublisher();
+
+ void addObserver(IContextObserver* observer);
+ void removeObserver(IContextObserver* observer);
+
+ virtual const char* getUri() = 0;
+ virtual bool isUserContext();
+ virtual const Json::Value& getData();
+
+ static void registerPublisher(const char* uri, ContextPublisher* (*creator)(uid_t));
+ static ContextPublisher* build(const char* uri, uid_t uid);
+
+ protected:
+ void notifyObservers();
+
+ virtual void read() = 0;
+ virtual void subscribe() = 0;
+ virtual void unsubscribe() = 0;
+
+ virtual bool isReady();
+ virtual void clearData();
+
+ Json::Value __data;
+
+ private:
+ std::list<IContextObserver*> __observers;
+ };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_CONTEXT_PUBLISHER_H__ */
--- /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_I_CONTEXT_OBSERVER_H__
+#define __CONTEXT_JOB_SCHEDULER_I_CONTEXT_OBSERVER_H__
+
+namespace ctx {
+
+ class IContextObserver {
+ public:
+ virtual ~IContextObserver() {}
+
+ virtual void ready() = 0;
+ };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_I_CONTEXT_OBSERVER_H__ */