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