Merge "[Convergence] - Functions return TizenResult and launchAppControl, start and...
[platform/core/api/webapi-plugins.git] / src / push / push_instance.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "push/push_instance.h"
18 #include <string>
19 #include <vector>
20 #include "common/logger.h"
21 #include "common/tools.h"
22 #include "push/push_manager.h"
23
24 namespace extension {
25 namespace push {
26
27 namespace {
28
29 const std::string kPrivilegePush = "http://tizen.org/privilege/push";
30
31 } // namespace
32
33 PushInstance::PushInstance() {
34     LoggerD("Enter");
35     using std::placeholders::_1;
36     using std::placeholders::_2;
37
38     #define REGISTER_ASYNC(c, func) \
39         RegisterSyncHandler(c, func);
40     #define REGISTER_SYNC(c, func) \
41         RegisterSyncHandler(c, func);
42
43     REGISTER_ASYNC("Push_registerService",
44             std::bind(&PushInstance::registerService, this, _1, _2));
45     REGISTER_ASYNC("Push_registerApplication",
46         std::bind(&PushInstance::registerApplication, this, _1, _2));
47     REGISTER_ASYNC("Push_unregisterService",
48             std::bind(&PushInstance::unregisterService, this, _1, _2));
49     REGISTER_ASYNC("Push_unregisterApplication",
50         std::bind(&PushInstance::unregisterApplication, this, _1, _2));
51     REGISTER_SYNC("Push_connectService",
52         std::bind(&PushInstance::connectService, this, _1, _2));
53     REGISTER_SYNC("Push_connect",
54             std::bind(&PushInstance::connect, this, _1, _2));
55     REGISTER_SYNC("Push_disconnectService",
56         std::bind(&PushInstance::disconnectService, this, _1, _2));
57     REGISTER_SYNC("Push_disconnect",
58             std::bind(&PushInstance::disconnect, this, _1, _2));
59     REGISTER_SYNC("Push_getRegistrationId",
60         std::bind(&PushInstance::getRegistrationId, this, _1, _2));
61     REGISTER_SYNC("Push_getUnreadNotifications",
62         std::bind(&PushInstance::getUnreadNotifications, this, _1, _2));
63     REGISTER_SYNC("Push_getPushMessage",
64         std::bind(&PushInstance::getPushMessage, this, _1, _2));
65
66     #undef REGISTER_ASYNC
67     #undef REGISTER_SYNC
68
69     impl = new PushManager(this);
70 }
71
72
73 void PushInstance::registerService(const picojson::value& args,
74         picojson::object& out) {
75     LoggerD("Enter");
76
77     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
78     common::PlatformResult result = impl->registerService(
79             args.get("callbackId").get<double>());
80     if (result.IsError()) {
81         LogAndReportError(result, &out, ("Error occured"));
82     } else {
83         ReportSuccess(out);
84     }
85 }
86
87 void PushInstance::registerApplication(const picojson::value& args,
88         picojson::object& out) {
89     LoggerD("Enter");
90
91     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
92     common::PlatformResult result = impl->registerApplication(
93             args.get("callbackId").get<double>());
94     if (result.IsError()) {
95         LogAndReportError(result, &out, ("Error occured"));
96     } else {
97         ReportSuccess(out);
98     }
99 }
100
101 void PushInstance::unregisterService(const picojson::value& args,
102         picojson::object& out) {
103     LoggerD("Enter");
104
105     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
106
107     common::PlatformResult result = impl->unregisterService(
108         args.get("callbackId").get<double>());
109     if (result.IsError()) {
110         LogAndReportError(result, &out, ("Error occured"));
111     } else {
112         ReportSuccess(out);
113     }
114 }
115
116 void PushInstance::unregisterApplication(const picojson::value& args,
117         picojson::object& out) {
118     LoggerD("Enter");
119
120     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
121
122     common::PlatformResult result = impl->unregisterApplication(
123         args.get("callbackId").get<double>());
124     if (result.IsError()) {
125         LogAndReportError(result, &out, ("Error occured"));
126     } else {
127         ReportSuccess(out);
128     }
129 }
130
131 void PushInstance::connectService(const picojson::value& args,
132         picojson::object& out) {
133     LoggerD("Enter");
134
135     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
136
137     common::PlatformResult result = impl->connectService();
138     if (result.IsError()) {
139       LogAndReportError(result, &out, ("Error while connect service"));
140     } else {
141       ReportSuccess(out);
142     }
143 }
144
145 void PushInstance::connect(const picojson::value& args,
146         picojson::object& out) {
147     LoggerD("Enter");
148
149     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
150
151     common::PlatformResult result = impl->connect();
152     if (result.IsError()) {
153       LogAndReportError(result, &out, ("Error while connect service"));
154     } else {
155       ReportSuccess(out);
156     }
157 }
158
159 void PushInstance::disconnectService(const picojson::value& args,
160         picojson::object& out) {
161     LoggerD("Enter");
162
163     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
164
165     common::PlatformResult result = impl->disconnectService();
166     if (result.IsError()) {
167       LogAndReportError(result, &out, ("Error while disconnect service"));
168     } else {
169       ReportSuccess(out);
170     }
171 }
172
173 void PushInstance::disconnect(const picojson::value& args,
174         picojson::object& out) {
175     LoggerD("Enter");
176
177     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
178
179     common::PlatformResult result = impl->disconnect();
180     if (result.IsError()) {
181       LogAndReportError(result, &out, ("Error while disconnect service"));
182     } else {
183       ReportSuccess(out);
184     }
185 }
186
187
188 void PushInstance::getRegistrationId(const picojson::value& args,
189         picojson::object& out) {
190     LoggerD("Enter");
191
192     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
193
194     std::string id;
195     common::PlatformResult result = impl->getRegistrationId(id);
196     if (result.IsError()) {
197         // this method should fail silently and return null
198         picojson::value res = picojson::value();
199         ReportSuccess(res, out);
200     } else {
201         picojson::value res(id);
202         ReportSuccess(res, out);
203     }
204 }
205
206 void PushInstance::getUnreadNotifications(const picojson::value& args,
207         picojson::object& out) {
208     LoggerD("Enter");
209
210     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
211
212     common::PlatformResult result = impl->getUnreadNotifications();
213     if (result.IsError()) {
214         LogAndReportError(result, &out, ("Error occured"));
215     } else {
216         ReportSuccess(out);
217     }
218 }
219
220 void PushInstance::getPushMessage(const picojson::value& args,
221                                   picojson::object& out) {
222   LoggerD("Enter");
223
224   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
225
226   picojson::value msg;
227   common::PlatformResult result = impl->getPushMessage(&msg);
228
229   if (result.IsError()) {
230     LoggerE("Error occurred");
231     ReportError(result, &out);
232   } else {
233     ReportSuccess(msg, out);
234   }
235 }
236
237 void PushInstance::onPushState(push_service_state_e state, common::PlatformResult result) {
238   LoggerD("Enter");
239   picojson::value res{picojson::object()};
240   picojson::object& dict = res.get<picojson::object>();
241
242   dict["listenerId"] = picojson::value("Push_State_Listener");
243   if (result.IsError()) {
244     dict["error"] = result.ToJSON();
245   } else {
246     dict["state"] = picojson::value(PushManagerCommon::StateToString(state));
247   }
248   Instance::PostMessage(this, res.serialize().c_str());
249 }
250
251 void PushInstance::onPushRegister(double callbackId,
252         common::PlatformResult result, const std::string& id) {
253     picojson::value res{picojson::object()};
254     picojson::object& dict = res.get<picojson::object>();
255
256     dict["callbackId"] = picojson::value(callbackId);
257     if (result.IsError()) {
258         dict["error"] = result.ToJSON();
259     } else {
260         dict["registrationId"] = picojson::value(id);
261     }
262     Instance::PostMessage(this, res.serialize().c_str());
263 }
264
265 void PushInstance::onPushNotify(push_service_notification_h noti) {
266     LoggerD("Enter");
267     picojson::value res{picojson::object()};
268     picojson::object& dict = res.get<picojson::object>();
269
270     picojson::value push_message{picojson::object()};
271     picojson::object& push_message_obj = push_message.get<picojson::object>();
272
273     PushManagerCommon::notificationToJson(noti, &push_message_obj);
274
275     dict["listenerId"] = picojson::value("Push_Notification_Listener");
276     dict["pushMessage"] = push_message;
277     Instance::PostMessage(this, res.serialize().c_str());
278 }
279
280 void PushInstance::onDeregister(double callbackId,
281         common::PlatformResult result) {
282     LoggerD("Enter");
283     picojson::value res{picojson::object()};
284     picojson::object& dict = res.get<picojson::object>();
285
286     dict["callbackId"] = picojson::value(callbackId);
287     if (result.IsError()) {
288         dict["error"] = result.ToJSON();
289     }
290     Instance::PostMessage(this, res.serialize().c_str());
291 }
292
293 PushInstance::~PushInstance() {
294     LoggerD("Enter");
295     if (impl) {
296       delete impl;
297       impl = nullptr;
298     }
299 }
300
301 }  // namespace push
302 }  // namespace extension