[Push] unregister
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Thu, 12 Feb 2015 14:31:57 +0000 (15:31 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 20 Feb 2015 12:38:54 +0000 (21:38 +0900)
Change-Id: I4b221c615b0c7cb09c13ba82f73bb7fd1f6031c3
Signed-off-by: Przemyslaw Ciezkowski <p.ciezkowski@samsung.com>
src/push/push_instance.cc
src/push/push_instance.h
src/push/push_manager.cc
src/push/push_manager.h

index d6edec3..0ae23ce 100644 (file)
@@ -72,8 +72,15 @@ void PushInstance::registerService(const picojson::value& args,
 void PushInstance::unregisterService(const picojson::value& args,
         picojson::object& out) {
     LoggerD("Enter");
-    picojson::value result;
-    ReportSuccess(result, out);
+    common::PlatformResult result = PushManager::getInstance()
+            .unregisterService(args.get("callbackId").get<double>());
+    if (result.IsError()) {
+        LoggerE("Error occured");
+        ReportError(result, &out);
+    } else {
+        picojson::value res;
+        ReportSuccess(res, out);
+    }
 }
 
 void PushInstance::connectService(const picojson::value& args,
@@ -137,6 +144,18 @@ void PushInstance::onPushNotify(const std::string& appData,
     PostMessage(resultListener.serialize().c_str());
 }
 
+void PushInstance::onDeregister(double callbackId,
+        common::PlatformResult result) {
+    LoggerD("Enter");
+    picojson::value::object dict;
+    dict["callbackId"] = picojson::value(callbackId);
+    if (result.IsError()) {
+        dict["error"] = result.ToJSON();
+    }
+    picojson::value res(dict);
+    PostMessage(res.serialize().c_str());
+}
+
 PushInstance::~PushInstance() {
     LoggerD("Enter");
 }
index 70e2311..d2d25ef 100644 (file)
@@ -20,6 +20,7 @@ class PushInstance: public common::ParsedInstance, public EventListener {
             common::PlatformResult result, const std::string& id);
     virtual void onPushNotify(const std::string& appData,
             const std::string& alertMessage, double date);
+    virtual void onDeregister(double callbackId, common::PlatformResult result);
 
  private:
      void registerService(const picojson::value& args, picojson::object& out);
index 41d145d..8ac8ca6 100644 (file)
@@ -17,7 +17,8 @@ using common::ErrorCode;
 
 PushManager::PushManager() :
     m_handle(NULL),
-    m_listener(NULL) {
+    m_listener(NULL),
+    m_state(PUSH_STATE_UNREGISTERED) {
     LoggerD("Enter");
     initAppId();
 
@@ -169,9 +170,32 @@ PlatformResult PushManager::registerService(
     return common::PlatformResult(ErrorCode::NO_ERROR);
 }
 
+common::PlatformResult PushManager::unregisterService(double callbackId) {
+    double* pcallbackId = new double(callbackId);
+    if (m_state == PUSH_STATE_UNREGISTERED) {
+        LoggerD("Already unregister, call unregister callback");
+        if (!g_idle_add(onFakeDeregister, pcallbackId)) {
+            delete pcallbackId;
+            LoggerE("g_idle_add failed");
+            return common::PlatformResult(ErrorCode::UNKNOWN_ERR,
+                "Unknown error");
+        }
+    } else {
+        int ret = push_deregister(m_handle, onDeregister, pcallbackId);
+        if (ret != PUSH_ERROR_NONE) {
+            delete pcallbackId;
+            LoggerE("Failed to deregister: push_deregister failed");
+            return common::PlatformResult(ErrorCode::UNKNOWN_ERR,
+                "Unknown error");
+        }
+    }
+    return common::PlatformResult(ErrorCode::NO_ERROR);
+}
+
 void PushManager::onPushState(push_state_e state, const char* err,
         void* user_data) {
     LoggerD("Enter %d", state);
+    getInstance().m_state = state;
 }
 
 void PushManager::onPushNotify(push_notification_h noti, void* user_data) {
@@ -250,6 +274,38 @@ void PushManager::onPushRegister(push_result_e result, const char* msg,
     delete callbackId;
 }
 
+gboolean PushManager::onFakeDeregister(gpointer user_data) {
+    LoggerD("Enter");
+    if (!getInstance().m_listener) {
+        LoggerW("Listener not set, ignoring");
+        return G_SOURCE_REMOVE;
+    }
+    double* callbackId = static_cast<double*>(user_data);
+    getInstance().m_listener->onDeregister(*callbackId,
+        PlatformResult(ErrorCode::NO_ERROR));
+    delete callbackId;
+    return G_SOURCE_REMOVE;
+}
+
+void PushManager::onDeregister(push_result_e result, const char* msg,
+        void* user_data) {
+    LoggerD("Enter");
+    if (!getInstance().m_listener) {
+        LoggerW("Listener not set, ignoring");
+        return;
+    }
+    double* callbackId = static_cast<double*>(user_data);
+    if (result == PUSH_RESULT_SUCCESS) {
+        getInstance().m_listener->onDeregister(*callbackId,
+            PlatformResult(ErrorCode::NO_ERROR));
+    } else {
+        getInstance().m_listener->onDeregister(*callbackId,
+            PlatformResult(ErrorCode::UNKNOWN_ERR,
+                msg == NULL ? "Unknown error" : msg));
+    }
+    delete callbackId;
+}
+
 }  // namespace push
 }  // namespace extension
 
index f45876a..62f9c72 100644 (file)
@@ -6,6 +6,7 @@
 #define SRC_PUSH_PUSH_MANAGER_H_
 
 #include <push.h>
+#include <glib.h>
 #include <string>
 #include <vector>
 #include <map>
@@ -20,6 +21,8 @@ class EventListener {
             common::PlatformResult result, const std::string& id) = 0;
     virtual void onPushNotify(const std::string& appData,
             const std::string& alertMessage, double date) = 0;
+    virtual void onDeregister(double callbackId,
+            common::PlatformResult result) = 0;
     virtual ~EventListener() {}
 };
 
@@ -38,6 +41,7 @@ class PushManager {
     };
     common::PlatformResult registerService(const ApplicationControl &appControl,
         double callbackId);
+    common::PlatformResult unregisterService(double callbackId);
 
  private:
     PushManager();
@@ -47,9 +51,13 @@ class PushManager {
     static void onPushNotify(push_notification_h noti, void *user_data);
     static void onPushRegister(push_result_e result, const char *msg,
         void *user_data);
+    static gboolean onFakeDeregister(gpointer user_data);
+    static void onDeregister(push_result_e result, const char *msg,
+        void *user_data);
 
     push_connection_h m_handle;
     EventListener* m_listener;
+    push_state_e m_state;
     std::string m_appId;
     std::string m_pkgId;
 };