[Push] refactored module for using multiple managers
[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(): m_ignoreNotificationEvents(true) {
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_registerApplication",
44         std::bind(&PushInstance::registerApplication, this, _1, _2));
45     REGISTER_ASYNC("Push_unregisterApplication",
46         std::bind(&PushInstance::unregisterApplication, this, _1, _2));
47     REGISTER_SYNC("Push_connectService",
48         std::bind(&PushInstance::connectService, this, _1, _2));
49     REGISTER_SYNC("Push_disconnectService",
50         std::bind(&PushInstance::disconnectService, this, _1, _2));
51     REGISTER_SYNC("Push_getRegistrationId",
52         std::bind(&PushInstance::getRegistrationId, this, _1, _2));
53     REGISTER_SYNC("Push_getUnreadNotifications",
54         std::bind(&PushInstance::getUnreadNotifications, this, _1, _2));
55     REGISTER_SYNC("Push_getPushMessage",
56         std::bind(&PushInstance::getPushMessage, this, _1, _2));
57     impl = new PushManager();
58     impl->setListener(this);
59
60     #undef REGISTER_ASYNC
61     #undef REGISTER_SYNC
62 }
63
64 void PushInstance::registerApplication(const picojson::value& args,
65         picojson::object& out) {
66     LoggerD("Enter");
67
68     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
69     common::PlatformResult result = impl->registerApplication(
70             args.get("callbackId").get<double>());
71     if (result.IsError()) {
72         LogAndReportError(result, &out, ("Error occured"));
73     } else {
74         picojson::value result;
75         ReportSuccess(result, out);
76     }
77 }
78
79 void PushInstance::unregisterApplication(const picojson::value& args,
80         picojson::object& out) {
81     LoggerD("Enter");
82
83     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
84
85     common::PlatformResult result = impl->unregisterApplication(
86         args.get("callbackId").get<double>());
87     if (result.IsError()) {
88         LogAndReportError(result, &out, ("Error occured"));
89     } else {
90         picojson::value res;
91         ReportSuccess(res, out);
92     }
93 }
94
95 void PushInstance::connectService(const picojson::value& args,
96         picojson::object& out) {
97     LoggerD("Enter");
98
99     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
100
101     m_ignoreNotificationEvents = false;
102     picojson::value result;
103     ReportSuccess(result, out);
104 }
105
106 void PushInstance::disconnectService(const picojson::value& args,
107         picojson::object& out) {
108     LoggerD("Enter");
109
110     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
111
112     m_ignoreNotificationEvents = true;
113     picojson::value result;
114     ReportSuccess(result, out);
115 }
116
117 void PushInstance::getRegistrationId(const picojson::value& args,
118         picojson::object& out) {
119     LoggerD("Enter");
120
121     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
122
123     std::string id;
124     common::PlatformResult result = impl->getRegistrationId(id);
125     if (result.IsError()) {
126         // this method should fail silently and return null
127         picojson::value res = picojson::value();
128         ReportSuccess(res, out);
129     } else {
130         picojson::value res(id);
131         ReportSuccess(res, out);
132     }
133 }
134
135 void PushInstance::getUnreadNotifications(const picojson::value& args,
136         picojson::object& out) {
137     LoggerD("Enter");
138
139     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
140
141     common::PlatformResult result = impl->getUnreadNotifications();
142     if (result.IsError()) {
143         LogAndReportError(result, &out, ("Error occured"));
144     } else {
145         picojson::value res;
146         ReportSuccess(res, out);
147     }
148 }
149
150 void PushInstance::getPushMessage(const picojson::value& args,
151                                   picojson::object& out) {
152   LoggerD("Enter");
153
154   CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
155
156   picojson::value msg;
157   common::PlatformResult result = impl->getPushMessage(&msg);
158
159   if (result.IsError()) {
160     LoggerE("Error occurred");
161     ReportError(result, &out);
162   } else {
163     ReportSuccess(msg, out);
164   }
165 }
166
167 void PushInstance::onPushRegister(double callbackId,
168         common::PlatformResult result, const std::string& id) {
169     LoggerD("Enter");
170     picojson::value::object dict;
171     dict["callbackId"] = picojson::value(callbackId);
172     if (result.IsError()) {
173         dict["error"] = result.ToJSON();
174     } else {
175         dict["registrationId"] = picojson::value(id);
176     }
177     picojson::value res(dict);
178     Instance::PostMessage(this, res.serialize().c_str());
179 }
180
181 void PushInstance::onPushNotify(push_service_notification_h noti) {
182     LoggerD("Enter");
183     if (m_ignoreNotificationEvents) {
184         LoggerD("Listener not set, ignoring event");
185     }
186     picojson::value::object dict;
187     picojson::value::object pushMessage;
188     impl->notificationToJson(noti, &pushMessage);
189
190     dict["listenerId"] = picojson::value("Push_Notification_Listener");
191     dict["pushMessage"] = picojson::value(pushMessage);
192     picojson::value resultListener(dict);
193     Instance::PostMessage(this, resultListener.serialize().c_str());
194 }
195
196 void PushInstance::onDeregister(double callbackId,
197         common::PlatformResult result) {
198     LoggerD("Enter");
199     picojson::value::object dict;
200     dict["callbackId"] = picojson::value(callbackId);
201     if (result.IsError()) {
202         dict["error"] = result.ToJSON();
203     }
204     picojson::value res(dict);
205     Instance::PostMessage(this, res.serialize().c_str());
206 }
207
208 PushInstance::~PushInstance() {
209     LoggerD("Enter");
210     impl->setListener(nullptr);
211 }
212
213 }  // namespace push
214 }  // namespace extension