[Push] implementation of new manager version
[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 #include "push/push_manager_old.h"
24
25 namespace extension {
26 namespace push {
27
28 namespace {
29
30 const std::string kPrivilegePush = "http://tizen.org/privilege/push";
31
32 } // namespace
33
34 PushInstance::PushInstance() : impl(nullptr) {
35     LoggerD("Enter");
36     using std::placeholders::_1;
37     using std::placeholders::_2;
38
39     #define REGISTER_ASYNC(c, func) \
40         RegisterSyncHandler(c, func);
41     #define REGISTER_SYNC(c, func) \
42         RegisterSyncHandler(c, func);
43
44     REGISTER_ASYNC("Push_registerApplication",
45         std::bind(&PushInstance::registerApplication, this, _1, _2));
46     REGISTER_ASYNC("Push_unregisterApplication",
47         std::bind(&PushInstance::unregisterApplication, this, _1, _2));
48     REGISTER_SYNC("Push_connectService",
49         std::bind(&PushInstance::connectService, this, _1, _2));
50     REGISTER_SYNC("Push_disconnectService",
51         std::bind(&PushInstance::disconnectService, this, _1, _2));
52     REGISTER_SYNC("Push_getRegistrationId",
53         std::bind(&PushInstance::getRegistrationId, this, _1, _2));
54     REGISTER_SYNC("Push_getUnreadNotifications",
55         std::bind(&PushInstance::getUnreadNotifications, this, _1, _2));
56     REGISTER_SYNC("Push_getPushMessage",
57         std::bind(&PushInstance::getPushMessage, this, _1, _2));
58
59     #undef REGISTER_ASYNC
60     #undef REGISTER_SYNC
61 }
62
63 void PushInstance::initApi(ApiVersionType type) {
64   LoggerD("Enter");
65   if (!impl) {
66     if (PUSH_API_2_4 == type) {
67       LoggerD("No API already initialized - using Tizen 2.4 API");
68       impl = new PushManagerOld(this);
69     } else {
70       LoggerD("No API already initialized - using Tizen 3.0 API");
71       impl = new PushManager(this);
72     }
73   } else {
74     LoggerD("API already initialized - use currently initialized API");
75   }
76 }
77
78 void PushInstance::registerApplication(const picojson::value& args,
79         picojson::object& out) {
80     LoggerD("Enter");
81     initApi(PUSH_API_2_4);
82
83     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
84     common::PlatformResult result = impl->registerApplication(
85             args.get("callbackId").get<double>());
86     if (result.IsError()) {
87         LogAndReportError(result, &out, ("Error occured"));
88     } else {
89         ReportSuccess(out);
90     }
91 }
92
93 void PushInstance::unregisterApplication(const picojson::value& args,
94         picojson::object& out) {
95     LoggerD("Enter");
96     initApi(PUSH_API_2_4);
97
98     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
99
100     common::PlatformResult result = impl->unregisterApplication(
101         args.get("callbackId").get<double>());
102     if (result.IsError()) {
103         LogAndReportError(result, &out, ("Error occured"));
104     } else {
105         ReportSuccess(out);
106     }
107 }
108
109 void PushInstance::connectService(const picojson::value& args,
110         picojson::object& out) {
111     LoggerD("Enter");
112     initApi(PUSH_API_3_0);
113
114     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
115
116     common::PlatformResult result = impl->connectService();
117     if (result.IsError()) {
118       LogAndReportError(result, &out, ("Error while connect service"));
119     } else {
120       ReportSuccess(out);
121     }
122 }
123
124 void PushInstance::disconnectService(const picojson::value& args,
125         picojson::object& out) {
126     LoggerD("Enter");
127     initApi(PUSH_API_2_4);
128
129     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
130
131     common::PlatformResult result = impl->disconnectService();
132     if (result.IsError()) {
133       LogAndReportError(result, &out, ("Error while disconnect service"));
134     } else {
135       ReportSuccess(out);
136     }
137
138 }
139
140 void PushInstance::getRegistrationId(const picojson::value& args,
141         picojson::object& out) {
142     LoggerD("Enter");
143     initApi(PUSH_API_2_4);
144
145     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
146
147     std::string id;
148     common::PlatformResult result = impl->getRegistrationId(id);
149     if (result.IsError()) {
150         // this method should fail silently and return null
151         picojson::value res = picojson::value();
152         ReportSuccess(res, out);
153     } else {
154         picojson::value res(id);
155         ReportSuccess(res, out);
156     }
157 }
158
159 void PushInstance::getUnreadNotifications(const picojson::value& args,
160         picojson::object& out) {
161     LoggerD("Enter");
162     initApi(PUSH_API_2_4);
163
164     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
165
166     common::PlatformResult result = impl->getUnreadNotifications();
167     if (result.IsError()) {
168         LogAndReportError(result, &out, ("Error occured"));
169     } else {
170         ReportSuccess(out);
171     }
172 }
173
174 void PushInstance::getPushMessage(const picojson::value& args,
175                                   picojson::object& out) {
176   LoggerD("Enter");
177   initApi(PUSH_API_2_4);
178
179   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
180
181   picojson::value msg;
182   common::PlatformResult result = impl->getPushMessage(&msg);
183
184   if (result.IsError()) {
185     LoggerE("Error occurred");
186     ReportError(result, &out);
187   } else {
188     ReportSuccess(msg, out);
189   }
190 }
191
192 void PushInstance::onPushRegister(double callbackId,
193         common::PlatformResult result, const std::string& id) {
194     picojson::value::object dict;
195     dict["callbackId"] = picojson::value(callbackId);
196     if (result.IsError()) {
197         dict["error"] = result.ToJSON();
198     } else {
199         dict["registrationId"] = picojson::value(id);
200     }
201     picojson::value res(dict);
202     Instance::PostMessage(this, res.serialize().c_str());
203 }
204
205 void PushInstance::onPushNotify(push_service_notification_h noti) {
206     LoggerD("Enter");
207     picojson::value::object dict;
208     picojson::value::object pushMessage;
209     impl->notificationToJson(noti, &pushMessage);
210
211     dict["listenerId"] = picojson::value("Push_Notification_Listener");
212     dict["pushMessage"] = picojson::value(pushMessage);
213     picojson::value resultListener(dict);
214     Instance::PostMessage(this, resultListener.serialize().c_str());
215 }
216
217 void PushInstance::onDeregister(double callbackId,
218         common::PlatformResult result) {
219     LoggerD("Enter");
220     picojson::value::object dict;
221     dict["callbackId"] = picojson::value(callbackId);
222     if (result.IsError()) {
223         dict["error"] = result.ToJSON();
224     }
225     picojson::value res(dict);
226     Instance::PostMessage(this, res.serialize().c_str());
227 }
228
229 PushInstance::~PushInstance() {
230     LoggerD("Enter");
231     if (impl) {
232       delete impl;
233       impl = nullptr;
234     }
235 }
236
237 }  // namespace push
238 }  // namespace extension