2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "push/push_instance.h"
20 #include "common/logger.h"
21 #include "push/push_manager.h"
26 PushInstance::PushInstance(): m_ignoreNotificationEvents(true) {
28 using std::placeholders::_1;
29 using std::placeholders::_2;
31 #define REGISTER_ASYNC(c, func) \
32 RegisterSyncHandler(c, func);
33 #define REGISTER_SYNC(c, func) \
34 RegisterSyncHandler(c, func);
36 REGISTER_ASYNC("Push_registerService",
37 std::bind(&PushInstance::registerService, this, _1, _2));
38 REGISTER_ASYNC("Push_unregisterService",
39 std::bind(&PushInstance::unregisterService, this, _1, _2));
40 REGISTER_SYNC("Push_connectService",
41 std::bind(&PushInstance::connectService, this, _1, _2));
42 REGISTER_SYNC("Push_disconnectService",
43 std::bind(&PushInstance::disconnectService, this, _1, _2));
44 REGISTER_SYNC("Push_getRegistrationId",
45 std::bind(&PushInstance::getRegistrationId, this, _1, _2));
46 REGISTER_SYNC("Push_getUnreadNotifications",
47 std::bind(&PushInstance::getUnreadNotifications, this, _1, _2));
48 PushManager::getInstance().setListener(this);
54 void PushInstance::registerService(const picojson::value& args,
55 picojson::object& out) {
58 PushManager::ApplicationControl appControl;
59 appControl.operation = args.get("operation").get<std::string>();
60 if (args.get("uri").is<std::string>()) {
61 appControl.uri = args.get("uri").get<std::string>();
63 if (args.get("mime").is<std::string>()) {
64 appControl.mime = args.get("mime").get<std::string>();
66 if (args.get("category").is<std::string>()) {
67 appControl.category = args.get("category").get<std::string>();
69 if (args.get("data").is<picojson::null>() == false) {
70 std::vector<picojson::value> dataArray =
71 args.get("data").get<picojson::array>();
72 for (auto &item : dataArray) {
73 std::string key = item.get("key").get<std::string>();
74 std::vector<picojson::value> values =
75 item.get("value").get<picojson::array>();
76 for (auto &value : values) {
77 appControl.data[key].push_back(value.to_str());
81 common::PlatformResult result = PushManager::getInstance().registerService(
83 args.get("callbackId").get<double>());
84 if (result.IsError()) {
85 LoggerE("Error occured");
86 ReportError(result, &out);
88 picojson::value result;
89 ReportSuccess(result, out);
93 void PushInstance::unregisterService(const picojson::value& args,
94 picojson::object& out) {
97 common::PlatformResult result = PushManager::getInstance()
98 .unregisterService(args.get("callbackId").get<double>());
99 if (result.IsError()) {
100 LoggerE("Error occured");
101 ReportError(result, &out);
104 ReportSuccess(res, out);
108 void PushInstance::connectService(const picojson::value& args,
109 picojson::object& out) {
112 m_ignoreNotificationEvents = false;
113 picojson::value result;
114 ReportSuccess(result, out);
117 void PushInstance::disconnectService(const picojson::value& args,
118 picojson::object& out) {
121 m_ignoreNotificationEvents = true;
122 picojson::value result;
123 ReportSuccess(result, out);
126 void PushInstance::getRegistrationId(const picojson::value& args,
127 picojson::object& out) {
131 common::PlatformResult result = PushManager::getInstance()
132 .getRegistrationId(id);
133 if (result.IsError()) {
134 // this method should fail silently and return null
135 picojson::value res = picojson::value();
136 ReportSuccess(res, out);
138 picojson::value res(id);
139 ReportSuccess(res, out);
143 void PushInstance::getUnreadNotifications(const picojson::value& args,
144 picojson::object& out) {
147 common::PlatformResult result = PushManager::getInstance()
148 .getUnreadNotifications();
149 if (result.IsError()) {
150 LoggerE("Error occured");
151 ReportError(result, &out);
154 ReportSuccess(res, out);
158 void PushInstance::onPushRegister(double callbackId,
159 common::PlatformResult result, const std::string& id) {
161 picojson::value::object dict;
162 dict["callbackId"] = picojson::value(callbackId);
163 if (result.IsError()) {
164 dict["error"] = result.ToJSON();
166 dict["registrationId"] = picojson::value(id);
168 picojson::value res(dict);
169 Instance::PostMessage(this, res.serialize().c_str());
172 void PushInstance::onPushNotify(const std::string& appData,
173 const std::string& alertMessage, double date) {
175 if (m_ignoreNotificationEvents) {
176 LoggerD("Listener not set, ignoring event");
178 picojson::value::object dict;
179 dict["listenerId"] = picojson::value("Push_Notification_Listener");
180 picojson::value::object pushMessage;
181 pushMessage["appData"] = picojson::value(appData);
182 pushMessage["alertMessage"] = picojson::value(alertMessage);
183 pushMessage["date"] = picojson::value(date);
184 dict["pushMessage"] = picojson::value(pushMessage);
185 picojson::value resultListener(dict);
186 Instance::PostMessage(this, resultListener.serialize().c_str());
189 void PushInstance::onDeregister(double callbackId,
190 common::PlatformResult result) {
192 picojson::value::object dict;
193 dict["callbackId"] = picojson::value(callbackId);
194 if (result.IsError()) {
195 dict["error"] = result.ToJSON();
197 picojson::value res(dict);
198 Instance::PostMessage(this, res.serialize().c_str());
201 PushInstance::~PushInstance() {
206 } // namespace extension