[Code format] Fixed formating with auto-format tool
[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   ScopeLogger();
35   using std::placeholders::_1;
36   using std::placeholders::_2;
37
38 #define REGISTER_METHOD(M) RegisterSyncHandler(#M, std::bind(&PushInstance::M, this, _1, _2))
39   REGISTER_METHOD(PushRegisterService);
40   REGISTER_METHOD(PushRegisterApplication);
41   REGISTER_METHOD(PushUnregisterService);
42   REGISTER_METHOD(PushUnregisterApplication);
43   REGISTER_METHOD(PushConnectService);
44   REGISTER_METHOD(PushConnect);
45   REGISTER_METHOD(PushDisconnectService);
46   REGISTER_METHOD(PushDisconnect);
47   REGISTER_METHOD(PushGetRegistrationId);
48   REGISTER_METHOD(PushGetUnreadNotifications);
49   REGISTER_METHOD(PushGetPushMessage);
50 #undef REGISTER_METHOD
51
52   impl = new PushManager(this);
53 }
54
55 void PushInstance::PushRegisterService(const picojson::value& args, picojson::object& out) {
56   ScopeLogger();
57   LoggerW(
58       "DEPRECATION WARNING: registerService() is deprecated and will be removed from next release. "
59       "Use register() instead.");
60
61   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
62   common::PlatformResult result = impl->registerService(args.get("callbackId").get<double>());
63   if (result.IsError()) {
64     LogAndReportError(result, &out, ("Error occured"));
65   } else {
66     ReportSuccess(out);
67   }
68 }
69
70 void PushInstance::PushRegisterApplication(const picojson::value& args, picojson::object& out) {
71   ScopeLogger();
72
73   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
74   common::PlatformResult result = impl->registerApplication(args.get("callbackId").get<double>());
75   if (result.IsError()) {
76     LogAndReportError(result, &out, ("Error occured"));
77   } else {
78     ReportSuccess(out);
79   }
80 }
81
82 void PushInstance::PushUnregisterService(const picojson::value& args, picojson::object& out) {
83   ScopeLogger();
84   LoggerW(
85       "DEPRECATION WARNING: unregisterService() is deprecated and will be removed from next "
86       "release. Use unregister() instead.");
87
88   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
89
90   common::PlatformResult result = impl->unregisterService(args.get("callbackId").get<double>());
91   if (result.IsError()) {
92     LogAndReportError(result, &out, ("Error occured"));
93   } else {
94     ReportSuccess(out);
95   }
96 }
97
98 void PushInstance::PushUnregisterApplication(const picojson::value& args, picojson::object& out) {
99   ScopeLogger();
100
101   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
102
103   common::PlatformResult result = impl->unregisterApplication(args.get("callbackId").get<double>());
104   if (result.IsError()) {
105     LogAndReportError(result, &out, ("Error occured"));
106   } else {
107     ReportSuccess(out);
108   }
109 }
110
111 void PushInstance::PushConnectService(const picojson::value& args, picojson::object& out) {
112   ScopeLogger();
113   LoggerW(
114       "DEPRECATION WARNING: connectService() is deprecated and will be removed from next release. "
115       "Use connect() instead.");
116
117   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
118
119   common::PlatformResult result = impl->connectService();
120   if (result.IsError()) {
121     LogAndReportError(result, &out, ("Error while connect service"));
122   } else {
123     ReportSuccess(out);
124   }
125 }
126
127 void PushInstance::PushConnect(const picojson::value& args, picojson::object& out) {
128   ScopeLogger();
129
130   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
131
132   common::PlatformResult result = impl->connect();
133   if (result.IsError()) {
134     LogAndReportError(result, &out, ("Error while connect service"));
135   } else {
136     ReportSuccess(out);
137   }
138 }
139
140 void PushInstance::PushDisconnectService(const picojson::value& args, picojson::object& out) {
141   ScopeLogger();
142   LoggerW(
143       "DEPRECATION WARNING: disconnectService() is deprecated and will be removed from next "
144       "release. Use disconnect() instead.");
145
146   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
147
148   common::PlatformResult result = impl->disconnectService();
149   if (result.IsError()) {
150     LogAndReportError(result, &out, ("Error while disconnect service"));
151   } else {
152     ReportSuccess(out);
153   }
154 }
155
156 void PushInstance::PushDisconnect(const picojson::value& args, picojson::object& out) {
157   ScopeLogger();
158
159   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
160
161   common::PlatformResult result = impl->disconnect();
162   if (result.IsError()) {
163     LogAndReportError(result, &out, ("Error while disconnect service"));
164   } else {
165     ReportSuccess(out);
166   }
167 }
168
169 void PushInstance::PushGetRegistrationId(const picojson::value& args, picojson::object& out) {
170   ScopeLogger();
171
172   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
173
174   std::string id;
175   common::PlatformResult result = impl->getRegistrationId(id);
176   if (result.IsError()) {
177     // this method should fail silently and return null
178     picojson::value res = picojson::value();
179     ReportSuccess(res, out);
180   } else {
181     picojson::value res(id);
182     ReportSuccess(res, out);
183   }
184 }
185
186 void PushInstance::PushGetUnreadNotifications(const picojson::value& args, picojson::object& out) {
187   ScopeLogger();
188
189   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
190
191   common::PlatformResult result = impl->getUnreadNotifications();
192   if (result.IsError()) {
193     LogAndReportError(result, &out, ("Error occured"));
194   } else {
195     ReportSuccess(out);
196   }
197 }
198
199 void PushInstance::PushGetPushMessage(const picojson::value& args, picojson::object& out) {
200   ScopeLogger();
201
202   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
203
204   picojson::value msg;
205   common::PlatformResult result = impl->getPushMessage(&msg);
206
207   if (result.IsError()) {
208     LoggerE("Error occurred");
209     ReportError(result, &out);
210   } else {
211     ReportSuccess(msg, out);
212   }
213 }
214
215 void PushInstance::onPushState(push_service_state_e state, common::PlatformResult result) {
216   ScopeLogger();
217   picojson::value res{picojson::object()};
218   picojson::object& dict = res.get<picojson::object>();
219
220   dict["listenerId"] = picojson::value("Push_State_Listener");
221   if (result.IsError()) {
222     dict["error"] = result.ToJSON();
223   } else {
224     dict["state"] = picojson::value(PushManagerCommon::StateToString(state));
225   }
226   Instance::PostMessage(this, res.serialize().c_str());
227 }
228
229 void PushInstance::onPushRegister(double callbackId, common::PlatformResult result,
230                                   const std::string& id) {
231   ScopeLogger();
232   picojson::value res{picojson::object()};
233   picojson::object& dict = res.get<picojson::object>();
234
235   dict["callbackId"] = picojson::value(callbackId);
236   if (result.IsError()) {
237     dict["error"] = result.ToJSON();
238   } else {
239     dict["registrationId"] = picojson::value(id);
240   }
241   Instance::PostMessage(this, res.serialize().c_str());
242 }
243
244 void PushInstance::onPushNotify(push_service_notification_h noti) {
245   ScopeLogger();
246   picojson::value res{picojson::object()};
247   picojson::object& dict = res.get<picojson::object>();
248
249   picojson::value push_message{picojson::object()};
250   picojson::object& push_message_obj = push_message.get<picojson::object>();
251
252   PushManagerCommon::notificationToJson(noti, &push_message_obj);
253
254   dict["listenerId"] = picojson::value("Push_Notification_Listener");
255   dict["pushMessage"] = push_message;
256   Instance::PostMessage(this, res.serialize().c_str());
257 }
258
259 void PushInstance::onDeregister(double callbackId, common::PlatformResult result) {
260   ScopeLogger();
261   picojson::value res{picojson::object()};
262   picojson::object& dict = res.get<picojson::object>();
263
264   dict["callbackId"] = picojson::value(callbackId);
265   if (result.IsError()) {
266     dict["error"] = result.ToJSON();
267   }
268   Instance::PostMessage(this, res.serialize().c_str());
269 }
270
271 PushInstance::~PushInstance() {
272   ScopeLogger();
273   if (impl) {
274     delete impl;
275     impl = nullptr;
276   }
277 }
278
279 }  // namespace push
280 }  // namespace extension