Merge "[Push] Added stubs for second implementation for Tizen 3.0 apps" 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 #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(){
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     if (common::tools::IsAppVersionEarlierThan("3.0")) {
60       LoggerD("Application version is earlier than 3.0, use previous API");
61       impl = new PushManagerOld(this);
62     } else {
63       LoggerD("Application version is 3.0 or later, use new API");
64       impl = new PushManager(this);
65     }
66
67     #undef REGISTER_ASYNC
68     #undef REGISTER_SYNC
69 }
70
71 void PushInstance::registerApplication(const picojson::value& args,
72         picojson::object& out) {
73     LoggerD("Enter");
74
75     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
76     common::PlatformResult result = impl->registerApplication(
77             args.get("callbackId").get<double>());
78     if (result.IsError()) {
79         LogAndReportError(result, &out, ("Error occured"));
80     } else {
81         ReportSuccess(out);
82     }
83 }
84
85 void PushInstance::unregisterApplication(const picojson::value& args,
86         picojson::object& out) {
87     LoggerD("Enter");
88
89     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
90
91     common::PlatformResult result = impl->unregisterApplication(
92         args.get("callbackId").get<double>());
93     if (result.IsError()) {
94         LogAndReportError(result, &out, ("Error occured"));
95     } else {
96         ReportSuccess(out);
97     }
98 }
99
100 void PushInstance::connectService(const picojson::value& args,
101         picojson::object& out) {
102     LoggerD("Enter");
103
104     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
105
106     common::PlatformResult result = impl->connectService();
107     if (result.IsError()) {
108       LogAndReportError(result, &out, ("Error while connect service"));
109     } else {
110       ReportSuccess(out);
111     }
112 }
113
114 void PushInstance::disconnectService(const picojson::value& args,
115         picojson::object& out) {
116     LoggerD("Enter");
117
118     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
119
120     common::PlatformResult result = impl->connectService();
121     if (result.IsError()) {
122       LogAndReportError(result, &out, ("Error while disconnect service"));
123     } else {
124       ReportSuccess(out);
125     }
126
127 }
128
129 void PushInstance::getRegistrationId(const picojson::value& args,
130         picojson::object& out) {
131     LoggerD("Enter");
132
133     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
134
135     std::string id;
136     common::PlatformResult result = impl->getRegistrationId(id);
137     if (result.IsError()) {
138         // this method should fail silently and return null
139         picojson::value res = picojson::value();
140         ReportSuccess(res, out);
141     } else {
142         picojson::value res(id);
143         ReportSuccess(res, out);
144     }
145 }
146
147 void PushInstance::getUnreadNotifications(const picojson::value& args,
148         picojson::object& out) {
149     LoggerD("Enter");
150
151     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
152
153     common::PlatformResult result = impl->getUnreadNotifications();
154     if (result.IsError()) {
155         LogAndReportError(result, &out, ("Error occured"));
156     } else {
157         ReportSuccess(out);
158     }
159 }
160
161 void PushInstance::getPushMessage(const picojson::value& args,
162                                   picojson::object& out) {
163   LoggerD("Enter");
164
165   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
166
167   picojson::value msg;
168   common::PlatformResult result = impl->getPushMessage(&msg);
169
170   if (result.IsError()) {
171     LoggerE("Error occurred");
172     ReportError(result, &out);
173   } else {
174     ReportSuccess(msg, out);
175   }
176 }
177
178 void PushInstance::onPushRegister(double callbackId,
179         common::PlatformResult result, const std::string& id) {
180     LoggerD("Enter");
181     picojson::value::object dict;
182     dict["callbackId"] = picojson::value(callbackId);
183     if (result.IsError()) {
184         dict["error"] = result.ToJSON();
185     } else {
186         dict["registrationId"] = picojson::value(id);
187     }
188     picojson::value res(dict);
189     Instance::PostMessage(this, res.serialize().c_str());
190 }
191
192 void PushInstance::onPushNotify(push_service_notification_h noti) {
193     LoggerD("Enter");
194     picojson::value::object dict;
195     picojson::value::object pushMessage;
196     impl->notificationToJson(noti, &pushMessage);
197
198     dict["listenerId"] = picojson::value("Push_Notification_Listener");
199     dict["pushMessage"] = picojson::value(pushMessage);
200     picojson::value resultListener(dict);
201     Instance::PostMessage(this, resultListener.serialize().c_str());
202 }
203
204 void PushInstance::onDeregister(double callbackId,
205         common::PlatformResult result) {
206     LoggerD("Enter");
207     picojson::value::object dict;
208     dict["callbackId"] = picojson::value(callbackId);
209     if (result.IsError()) {
210         dict["error"] = result.ToJSON();
211     }
212     picojson::value res(dict);
213     Instance::PostMessage(this, res.serialize().c_str());
214 }
215
216 PushInstance::~PushInstance() {
217     LoggerD("Enter");
218     delete impl;
219 }
220
221 }  // namespace push
222 }  // namespace extension