[Push] Added new functionalites, code uses push-service.h
authorTomasz Marciniak <t.marciniak@samsung.com>
Tue, 9 Feb 2016 14:27:02 +0000 (15:27 +0100)
committerTomasz Marciniak <t.marciniak@samsung.com>
Fri, 29 Apr 2016 12:41:08 +0000 (14:41 +0200)
[Verification] Code compiles, TCT pass rate: 100% (41/41/0/0/0)

Change-Id: Ia5bfdd92c450877aa83fa9e306bf8bbf1733c4c7
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
src/push/push_api.js
src/push/push_instance.cc
src/push/push_instance.h
src/push/push_manager.cc
src/push/push_manager.h

index 93fc048..c103654 100644 (file)
@@ -48,13 +48,20 @@ function PushManager() {
   }
 }
 
-PushManager.prototype.registerService = function(appControl, successCallback, errorCallback) {
-  var data = validator.validateArgs(arguments, [
+PushManager.prototype.registerService = function() {
+  validator.validateArgs(arguments, [
     {
       name: 'appControl',
       type: validator.Types.PLATFORM_OBJECT,
       values: tizen.ApplicationControl
-    },
+    }
+  ]);
+  console.warn('Method registerService() is deprecated, use register() instead.');
+  this.register.apply(this, Array.prototype.slice.call(arguments, 1));
+};
+
+PushManager.prototype.register = function() {
+  var data = validator.validateArgs(arguments, [
     {
       name: 'successCallback',
       type: validator.Types.FUNCTION
@@ -66,13 +73,8 @@ PushManager.prototype.registerService = function(appControl, successCallback, er
       nullable: true
     }
   ]);
-  var ret = native.call('Push_registerService', {
-    operation: data.appControl.operation,
-    uri: data.appControl.uri,
-    mime: data.appControl.mime,
-    category: data.appControl.category,
-    data: data.appControl.data
-  }, function(msg) {
+
+  var ret = native.call('Push_register', {}, function(msg) {
     if (msg.error) {
       if (validatorType.isFunction(data.errorCallback)) {
         data.errorCallback(native.getErrorObject(msg));
@@ -86,7 +88,12 @@ PushManager.prototype.registerService = function(appControl, successCallback, er
   }
 };
 
-PushManager.prototype.unregisterService = function(successCallback, errorCallback) {
+PushManager.prototype.unregisterService = function() {
+  console.warn('Method unregisterService() is deprecated, use unregister() instead.');
+  this.unregister.apply(this, arguments);
+};
+
+PushManager.prototype.unregister = function() {
   var data = validator.validateArgs(arguments, [
     {
       name: 'successCallback',
@@ -101,7 +108,7 @@ PushManager.prototype.unregisterService = function(successCallback, errorCallbac
       nullable: true
     }
   ]);
-  var result = native.call('Push_unregisterService', {}, function(msg) {
+  var result = native.call('Push_unregister', {}, function(msg) {
     if (msg.error) {
       if (validatorType.isFunction(data.errorCallback)) {
         data.errorCallback(native.getErrorObject(msg));
@@ -155,4 +162,14 @@ PushManager.prototype.getUnreadNotifications = function() {
   }
 };
 
+PushManager.prototype.getPushMessage = function() {
+
+  var ret = native.callSync('Push_getPushMessage', {});
+  if (native.isFailure(ret)) {
+    throw native.getErrorObject(ret);
+  } else {
+    return new PushMessage(native.getResultObject(ret));
+  }
+};
+
 exports = new PushManager();
index 2e8dc75..c588132 100644 (file)
@@ -40,10 +40,10 @@ PushInstance::PushInstance(): m_ignoreNotificationEvents(true) {
     #define REGISTER_SYNC(c, func) \
         RegisterSyncHandler(c, func);
 
-    REGISTER_ASYNC("Push_registerService",
-        std::bind(&PushInstance::registerService, this, _1, _2));
-    REGISTER_ASYNC("Push_unregisterService",
-        std::bind(&PushInstance::unregisterService, this, _1, _2));
+    REGISTER_ASYNC("Push_registerApplication",
+        std::bind(&PushInstance::registerApplication, this, _1, _2));
+    REGISTER_ASYNC("Push_unregisterApplication",
+        std::bind(&PushInstance::unregisterApplication, this, _1, _2));
     REGISTER_SYNC("Push_connectService",
         std::bind(&PushInstance::connectService, this, _1, _2));
     REGISTER_SYNC("Push_disconnectService",
@@ -52,43 +52,20 @@ PushInstance::PushInstance(): m_ignoreNotificationEvents(true) {
         std::bind(&PushInstance::getRegistrationId, this, _1, _2));
     REGISTER_SYNC("Push_getUnreadNotifications",
         std::bind(&PushInstance::getUnreadNotifications, this, _1, _2));
+    REGISTER_SYNC("Push_getPushMessage",
+        std::bind(&PushInstance::getPushMessage, this, _1, _2));
     PushManager::getInstance().setListener(this);
 
     #undef REGISTER_ASYNC
     #undef REGISTER_SYNC
 }
 
-void PushInstance::registerService(const picojson::value& args,
+void PushInstance::registerApplication(const picojson::value& args,
         picojson::object& out) {
     LoggerD("Enter");
 
     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
-
-    PushManager::ApplicationControl appControl;
-    appControl.operation = args.get("operation").get<std::string>();
-    if (args.get("uri").is<std::string>()) {
-        appControl.uri = args.get("uri").get<std::string>();
-    }
-    if (args.get("mime").is<std::string>()) {
-        appControl.mime = args.get("mime").get<std::string>();
-    }
-    if (args.get("category").is<std::string>()) {
-        appControl.category = args.get("category").get<std::string>();
-    }
-    if (args.get("data").is<picojson::null>() == false) {
-        std::vector<picojson::value> dataArray =
-                args.get("data").get<picojson::array>();
-        for (auto &item : dataArray) {
-            std::string key = item.get("key").get<std::string>();
-            std::vector<picojson::value> values =
-                    item.get("value").get<picojson::array>();
-            for (auto &value : values) {
-                appControl.data[key].push_back(value.to_str());
-            }
-        }
-    }
-    common::PlatformResult result = PushManager::getInstance().registerService(
-            appControl,
+    common::PlatformResult result = PushManager::getInstance().registerApplication(
             args.get("callbackId").get<double>());
     if (result.IsError()) {
         LogAndReportError(result, &out, ("Error occured"));
@@ -98,14 +75,14 @@ void PushInstance::registerService(const picojson::value& args,
     }
 }
 
-void PushInstance::unregisterService(const picojson::value& args,
+void PushInstance::unregisterApplication(const picojson::value& args,
         picojson::object& out) {
     LoggerD("Enter");
 
     CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
 
     common::PlatformResult result = PushManager::getInstance()
-            .unregisterService(args.get("callbackId").get<double>());
+            .unregisterApplication(args.get("callbackId").get<double>());
     if (result.IsError()) {
         LogAndReportError(result, &out, ("Error occured"));
     } else {
@@ -171,6 +148,23 @@ void PushInstance::getUnreadNotifications(const picojson::value& args,
     }
 }
 
+void PushInstance::getPushMessage(const picojson::value& args,
+                                  picojson::object& out) {
+  LoggerD("Enter");
+
+  CHECK_PRIVILEGE_ACCESS(kPrivilegePush, &out);
+
+  picojson::value msg;
+  common::PlatformResult result = PushManager::getInstance().getPushMessage(&msg);
+
+  if (result.IsError()) {
+    LoggerE("Error occurred");
+    ReportError(result, &out);
+  } else {
+    ReportSuccess(msg, out);
+  }
+}
+
 void PushInstance::onPushRegister(double callbackId,
         common::PlatformResult result, const std::string& id) {
     LoggerD("Enter");
@@ -185,18 +179,16 @@ void PushInstance::onPushRegister(double callbackId,
     Instance::PostMessage(this, res.serialize().c_str());
 }
 
-void PushInstance::onPushNotify(const std::string& appData,
-        const std::string& alertMessage, double date) {
+void PushInstance::onPushNotify(push_service_notification_h noti) {
     LoggerD("Enter");
     if (m_ignoreNotificationEvents) {
         LoggerD("Listener not set, ignoring event");
     }
     picojson::value::object dict;
-    dict["listenerId"] = picojson::value("Push_Notification_Listener");
     picojson::value::object pushMessage;
-    pushMessage["appData"] = picojson::value(appData);
-    pushMessage["alertMessage"] = picojson::value(alertMessage);
-    pushMessage["date"] = picojson::value(date);
+    PushManager::getInstance().notificationToJson(noti, &pushMessage);
+
+    dict["listenerId"] = picojson::value("Push_Notification_Listener");
     dict["pushMessage"] = picojson::value(pushMessage);
     picojson::value resultListener(dict);
     Instance::PostMessage(this, resultListener.serialize().c_str());
index 8800b59..90fb829 100644 (file)
@@ -30,18 +30,17 @@ class PushInstance: public common::ParsedInstance, public EventListener {
     virtual ~PushInstance();
     virtual void onPushRegister(double callbackId,
             common::PlatformResult result, const std::string& id);
-    virtual void onPushNotify(const std::string& appData,
-            const std::string& alertMessage, double date);
+    virtual void onPushNotify(push_service_notification_h noti);
     virtual void onDeregister(double callbackId, common::PlatformResult result);
 
  private:
-     void registerService(const picojson::value& args, picojson::object& out);
-     void unregisterService(const picojson::value& args, picojson::object& out);
+     void registerApplication(const picojson::value& args, picojson::object& out);
+     void unregisterApplication(const picojson::value& args, picojson::object& out);
      void connectService(const picojson::value& args, picojson::object& out);
      void disconnectService(const picojson::value& args, picojson::object& out);
      void getRegistrationId(const picojson::value& args, picojson::object& out);
-     void getUnreadNotifications(const picojson::value& args,
-            picojson::object& out);
+     void getUnreadNotifications(const picojson::value& args, picojson::object& out);
+     void getPushMessage(const picojson::value& args, picojson::object& out);
 
      bool m_ignoreNotificationEvents;
 };
index 02871ed..0e2e513 100644 (file)
 #include <unistd.h>
 #include <pcrecpp.h>
 #include <app_control.h>
+#include <app_control_internal.h>
 #include <app_manager.h>
+#include <bundle.h>
+
+#include "common/extension.h"
 #include "common/logger.h"
 
 namespace extension {
@@ -27,23 +31,65 @@ namespace push {
 using common::PlatformResult;
 using common::ErrorCode;
 
+namespace {
+
+ErrorCode ConvertPushError(int e) {
+  ErrorCode error;
+
+  switch(e) {
+    case PUSH_SERVICE_ERROR_NONE:
+      error = ErrorCode::NO_ERROR;
+      break;
+
+    case PUSH_SERVICE_ERROR_INVALID_PARAMETER:
+      error = ErrorCode::INVALID_VALUES_ERR;
+      break;
+
+    case PUSH_SERVICE_ERROR_OUT_OF_MEMORY:
+    case PUSH_SERVICE_ERROR_NOT_CONNECTED:
+    case PUSH_SERVICE_ERROR_OPERATION_FAILED:
+    default:
+      error = ErrorCode::ABORT_ERR;
+      break;
+  }
+
+  return error;
+}
+
+}  // namespace
+
 PushManager::PushManager() :
     m_handle(NULL),
     m_listener(NULL),
-    m_state(PUSH_STATE_UNREGISTERED) {
+    m_state(PUSH_SERVICE_STATE_UNREGISTERED),
+    app_control_(nullptr),
+    operation_(nullptr) {
     LoggerD("Enter");
+
     initAppId();
+    InitAppControl();
 
-    int ret = push_connect(m_pkgId.c_str(), onPushState, onPushNotify, NULL,
+    int ret = push_service_connect(m_pkgId.c_str(), onPushState, onPushNotify, NULL,
         &m_handle);
-    if (ret != PUSH_ERROR_NONE) {
+    if (ret != PUSH_SERVICE_ERROR_NONE) {
         LoggerE("Failed to connect to push (%d)", ret);
     }
 }
 
 PushManager::~PushManager() {
     LoggerD("Enter");
-    push_disconnect(m_handle);
+
+    if (m_handle) {
+      push_service_disconnect(m_handle);
+    }
+
+    if (app_control_ && (APP_CONTROL_ERROR_NONE != app_control_destroy(app_control_))) {
+      LoggerE("Failed to destroy app control");
+    }
+
+    if (operation_) {
+      free(operation_);
+    }
 }
 
 void PushManager::setListener(EventListener* listener) {
@@ -83,119 +129,57 @@ void PushManager::initAppId() {
     app_info_destroy(info);
 }
 
+void PushManager::InitAppControl() {
+  ScopeLogger();
+
+  const auto encoded_bundle = GetEncodedBundle();
+
+  auto bundle = bundle_decode((bundle_raw*) (encoded_bundle.c_str()),
+                              encoded_bundle.length());
+  if (nullptr == bundle) {
+    LoggerE("Failed to decode bundle");
+    return;
+  }
+
+  app_control_h app_control = nullptr;
+  int ret = app_control_create_event(bundle, &app_control);
+  bundle_free(bundle);
+
+  if (APP_CONTROL_ERROR_NONE != ret) {
+    LoggerE("app_control_create_event() failed: %d (%s)", ret, get_error_message(ret));
+  } else {
+    app_control_ = app_control;
+    ret = app_control_get_operation(app_control, &operation_);
+    if (APP_CONTROL_ERROR_NONE != ret) {
+      LoggerE("app_control_get_operation() failed: %d (%s)", ret, get_error_message(ret));
+    }
+  }
+}
+
 PushManager& PushManager::getInstance() {
   static PushManager instance;
   return instance;
 }
 
-PlatformResult PushManager::registerService(
-        const ApplicationControl &appControl, double callbackId) {
-    LoggerD("Enter");
-    app_control_h service;
-    int ret = app_control_create(&service);
-    if (ret != APP_CONTROL_ERROR_NONE) {
-        return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-            "Failed to create service",
-            ("Failed to create service: app_control_create failed(%d)", ret));
-    }
-
-    if (appControl.operation.empty()) {
-        app_control_destroy(service);
-        return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR,
-            "Operation is empty");
-    }
-    ret = app_control_set_operation(service, appControl.operation.c_str());
-    if (ret != APP_CONTROL_ERROR_NONE) {
-        app_control_destroy(service);
-        return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-            "Failed to set operation",
-            ("Failed to set operation: app_control_set_operation failed(%d)", ret));
-    }
+PlatformResult PushManager::registerApplication(double callbackId) {
+  LoggerD("Enter");
 
-    if (!appControl.uri.empty()) {
-        ret = app_control_set_uri(service, appControl.uri.c_str());
-        if (ret != APP_CONTROL_ERROR_NONE) {
-            app_control_destroy(service);
-            return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                "Failed to set uri",
-                ("Failed to set uri: app_control_set_uri failed(%d)", ret));
-        }
-    }
+  double* pcallback = new double(callbackId);
 
-    if (!appControl.mime.empty()) {
-        ret = app_control_set_mime(service, appControl.mime.c_str());
-        if (ret != APP_CONTROL_ERROR_NONE) {
-            app_control_destroy(service);
-            return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                "Failed to set mime",
-                ("Failed to set mime: app_control_set_mime failed(%d)", ret));
-        }
-    }
+  int ret = push_service_register(m_handle, onApplicationRegister, static_cast<void*>(pcallback));
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    delete pcallback;
 
-    if (!appControl.category.empty()) {
-        ret = app_control_set_category(service, appControl.category.c_str());
-        if (ret != APP_CONTROL_ERROR_NONE) {
-            app_control_destroy(service);
-            return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                "Failed to set category",
-                ("Failed to set category: app_control_set_category failed"));
-        }
-    }
-
-    ret = app_control_set_app_id(service, m_appId.c_str());
-    if (ret != APP_CONTROL_ERROR_NONE) {
-        app_control_destroy(service);
-        return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-            "Failed to set app id",
-            ("Failed to set app id: app_control_set_app_id failed(%d)", ret));
-    }
-
-    for (auto &item : appControl.data) {
-        if (item.second.size() == 1) {
-            ret = app_control_add_extra_data(service, item.first.c_str(),
-                item.second.front().c_str());
-        } else {
-            const char *values[item.second.size()];
-            for (size_t i = 0; i < item.second.size(); ++i) {
-                values[i] = item.second.at(i).c_str();
-            }
-            ret = app_control_add_extra_data_array(service,
-                item.first.c_str(), values, item.second.size());
-        }
-        if (ret != APP_CONTROL_ERROR_NONE) {
-            app_control_destroy(service);
-            return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                "Failed to set extra data",
-                ("Failed to set extra data: app_control_add_extra_data failed(%d)", ret));
-        }
-    }
-
-    double* pcallback = new double(callbackId);
-    ret = push_register(m_handle, service, onPushRegister, pcallback);
-    app_control_destroy(service);
-    if (ret != PUSH_ERROR_NONE) {
-        delete pcallback;
-        if (ret == PUSH_ERROR_INVALID_PARAMETER) {
-          LoggerE("[push_register] PUSH_ERROR_INVALID_PARAMETER");
-        } else if (ret == PUSH_ERROR_OUT_OF_MEMORY) {
-          LoggerE("[push_register] PUSH_ERROR_OUT_OF_MEMORY");
-        } else if (ret == PUSH_ERROR_NOT_CONNECTED) {
-          LoggerE("[push_register] PUSH_ERROR_NOT_CONNECTED");
-        } else if (ret == PUSH_ERROR_OPERATION_FAILED) {
-          LoggerE("[push_register] PUSH_ERROR_OPERATION_FAILED");
-        }
-        return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-            "Failed to register",
-            ("Failed to register push: push_register failed (%d)", ret));
-    }
-
-    return common::PlatformResult(ErrorCode::NO_ERROR);
+    return LogAndCreateResult(
+        ConvertPushError(ret), "Failed to register", ("push_service_register failed (%d)", ret));
+  }
+  return common::PlatformResult(ErrorCode::NO_ERROR);
 }
 
-common::PlatformResult PushManager::unregisterService(double callbackId) {
+common::PlatformResult PushManager::unregisterApplication(double callbackId) {
     LoggerD("Enter");
     double* pcallbackId = new double(callbackId);
-    if (m_state == PUSH_STATE_UNREGISTERED) {
+    if (m_state == PUSH_SERVICE_STATE_UNREGISTERED) {
         LoggerD("Already unregister, call unregister callback");
         if (!g_idle_add(onFakeDeregister, pcallbackId)) {
             delete pcallbackId;
@@ -203,21 +187,21 @@ common::PlatformResult PushManager::unregisterService(double callbackId) {
                 "Unknown error", ("g_idle_add failed"));
         }
     } else {
-        int ret = push_deregister(m_handle, onDeregister, pcallbackId);
-        if (ret != PUSH_ERROR_NONE) {
+        int ret = push_service_deregister(m_handle, onDeregister, pcallbackId);
+        if (ret != PUSH_SERVICE_ERROR_NONE) {
             delete pcallbackId;
-            if (ret == PUSH_ERROR_INVALID_PARAMETER) {
-              LoggerE("[push_deregister] PUSH_ERROR_INVALID_PARAMETER");
-            } else if (ret == PUSH_ERROR_OUT_OF_MEMORY) {
-              LoggerE("[push_deregister] PUSH_ERROR_OUT_OF_MEMORY");
-            } else if (ret == PUSH_ERROR_NOT_CONNECTED) {
-              LoggerE("[push_deregister] PUSH_ERROR_NOT_CONNECTED");
-            } else if (ret == PUSH_ERROR_OPERATION_FAILED) {
-              LoggerE("[push_deregister] PUSH_ERROR_OPERATION_FAILED");
+            if (ret == PUSH_SERVICE_ERROR_INVALID_PARAMETER) {
+              LoggerE("[push_service_deregister] PUSH_SERVICE_ERROR_INVALID_PARAMETER");
+            } else if (ret == PUSH_SERVICE_ERROR_OUT_OF_MEMORY) {
+              LoggerE("[push_service_deregister] PUSH_SERVICE_ERROR_OUT_OF_MEMORY");
+            } else if (ret == PUSH_SERVICE_ERROR_NOT_CONNECTED) {
+              LoggerE("[push_service_deregister] PUSH_SERVICE_ERROR_NOT_CONNECTED");
+            } else if (ret == PUSH_SERVICE_ERROR_OPERATION_FAILED) {
+              LoggerE("[push_service_deregister] PUSH_SERVICE_ERROR_OPERATION_FAILED");
             }
             return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
                 "Unknown error",
-                ("Failed to deregister: push_deregister failed (%d)", ret));
+                ("Failed to deregister: push_service_deregister failed (%d)", ret));
         }
     }
     return common::PlatformResult(ErrorCode::NO_ERROR);
@@ -226,18 +210,18 @@ common::PlatformResult PushManager::unregisterService(double callbackId) {
 common::PlatformResult PushManager::getRegistrationId(std::string& id) {
     LoggerD("Enter");
     char* temp = NULL;
-    int ret = push_get_registration_id(m_handle, &temp);
-    if (ret != PUSH_ERROR_NONE) {
-        if (ret == PUSH_ERROR_INVALID_PARAMETER) {
-          LoggerE("[push_get_registration_id]   PUSH_ERROR_INVALID_PARAMETER");
-        } else if (ret == PUSH_ERROR_OUT_OF_MEMORY) {
-          LoggerE("[push_get_registration_id]   PUSH_ERROR_OUT_OF_MEMORY");
-        } else if (ret == PUSH_ERROR_NO_DATA) {
-          LoggerE("[push_get_registration_id]   PUSH_ERROR_NO_DATA");
+    int ret = push_service_get_registration_id(m_handle, &temp);
+    if (ret != PUSH_SERVICE_ERROR_NONE) {
+        if (ret == PUSH_SERVICE_ERROR_INVALID_PARAMETER) {
+          LoggerE("[push_service_get_registration_id]   PUSH_SERVICE_ERROR_INVALID_PARAMETER");
+        } else if (ret == PUSH_SERVICE_ERROR_OUT_OF_MEMORY) {
+          LoggerE("[push_service_get_registration_id]   PUSH_SERVICE_ERROR_OUT_OF_MEMORY");
+        } else if (ret == PUSH_SERVICE_ERROR_NO_DATA) {
+          LoggerE("[push_service_get_registration_id]   PUSH_SERVICE_ERROR_NO_DATA");
         }
         return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
             "Unknown error",
-            ("Failed to get id: push_get_registration_id failed (%d)", ret));
+            ("Failed to get id: push_service_get_registration_id failed (%d)", ret));
     }
     id = temp;
     free(temp);
@@ -246,105 +230,173 @@ common::PlatformResult PushManager::getRegistrationId(std::string& id) {
 
 common::PlatformResult PushManager::getUnreadNotifications() {
     LoggerD("Enter");
-    int ret = push_request_unread_notification(m_handle);
-    if (ret != PUSH_ERROR_NONE) {
+    int ret = push_service_request_unread_notification(m_handle);
+    if (ret != PUSH_SERVICE_ERROR_NONE) {
         return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
             "Unknown error",
-            ("Failed to send request: push_request_unread_notification failed (%d)", ret));
+            ("Failed to send request: push_service_request_unread_notification failed (%d)", ret));
     }
     return common::PlatformResult(ErrorCode::NO_ERROR);
 }
 
-void PushManager::onPushState(push_state_e state, const char* err,
-        void* user_data) {
-    LoggerD("Enter %d, err: %s", state, err);
-    getInstance().m_state = state;
-}
+PlatformResult PushManager::getPushMessage(picojson::value* out) {
+  LoggerD("Enter");
 
-void PushManager::onPushNotify(push_notification_h noti, void* user_data) {
-    LoggerD("Enter");
-    if (!getInstance().m_listener) {
-        LoggerW("Listener not set, ignoring");
-        return;
-    }
+  push_service_notification_h handle = nullptr;
+  int ret = push_service_app_control_to_notification(app_control_, operation_,
+                                                     &handle);
 
-    char* temp = NULL;
-    int ret = push_get_notification_data(noti, &temp);
-    if (ret != PUSH_ERROR_NONE) {
-        LoggerE("Failed to get appData");
-        return;
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    if (PUSH_SERVICE_ERROR_NO_DATA == ret || PUSH_SERVICE_ERROR_NOT_SUPPORTED == ret) {
+      // application was not started by push service, return null
+      *out = picojson::value{};
+      return common::PlatformResult(ErrorCode::NO_ERROR);
+    } else {
+      return LogAndCreateResult(
+              ConvertPushError(ret), "Failed to get message",
+              ("push_service_app_control_to_notification failed: (%d)", ret));
     }
-    std::string appData = temp;
-    free(temp);
+  }
 
-    temp = NULL;
-    ret = push_get_notification_message(noti, &temp);
-    if (ret != PUSH_ERROR_NONE) {
-        LoggerE("Failed to get message");
-        return;
-    }
+  picojson::object notification;
+  notificationToJson(handle, &notification);
+  push_service_free_notification(handle);
 
-    // parse query string and find value for alertMessage
-    pcrecpp::StringPiece input(temp);
-    pcrecpp::RE re("([^=]+)=([^&]*)&?");
-    string key;
-    string value;
-    std::string alertMessage;
-    while (re.Consume(&input, &key, &value)) {
-        if (key == "alertMessage") {
-            alertMessage = value;
-            break;
-        }
-    }
-    free(temp);
+  *out = picojson::value{notification};
 
-    long long int date = -1;
-    ret = push_get_notification_time(noti, &date);
-    if (ret != PUSH_ERROR_NONE) {
-        LoggerE("Failed to get date");
-        return;
+  return common::PlatformResult(ErrorCode::NO_ERROR);
+}
+
+void PushManager::notificationToJson(push_service_notification_h noti, picojson::object* obj) {
+  LoggerD("Enter");
+
+  char* temp = nullptr;
+  int ret = push_service_get_notification_data(noti, &temp);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get appData");
+    return;
+  }
+  (*obj)["appData"] = picojson::value(temp);
+  free(temp);
+
+  temp = nullptr;
+  ret = push_service_get_notification_message(noti, &temp);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get message");
+    return;
+  }
+
+  // parse query string and find value for alertMessage
+  pcrecpp::StringPiece input(temp);
+  pcrecpp::RE re("([^=]+)=([^&]*)&?");
+  string key;
+  string value;
+  while (re.Consume(&input, &key, &value)) {
+    if (key == "alertMessage") {
+      (*obj)["alertMessage"] = picojson::value(key);
+      break;
     }
-    getInstance().m_listener->onPushNotify(appData, alertMessage, date);
+  }
+  free(temp);
+
+  long long int date = -1;
+  ret = push_service_get_notification_time(noti, &date);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get date");
+    return;
+  }
+  (*obj)["date"] = picojson::value(static_cast<double>(date));
+
+  ret = push_service_get_notification_sender(noti, &temp);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get sender");
+    return;
+  }
+  (*obj)["sender"] = picojson::value(temp);
+  free(temp);
+
+  ret = push_service_get_notification_session_info(noti, &temp);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get session info");
+    return;
+  }
+  std::string session_info = temp;
+  (*obj)["sesionInfo"] = picojson::value(temp);
+  free(temp);
+
+  ret = push_service_get_notification_request_id(noti, &temp);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get request id");
+    return;
+  }
+  (*obj)["requestId"] = picojson::value(temp);
+  free(temp);
+
+  int type;
+  ret = push_service_get_notification_type(noti, &type);
+  if (ret != PUSH_SERVICE_ERROR_NONE) {
+    LoggerE("Failed to get type");
+    return;
+  }
+  (*obj)["type"] = picojson::value(static_cast<double>(type));
 }
 
-void PushManager::onPushRegister(push_result_e result, const char* msg,
+void PushManager::onPushState(push_service_state_e state, const char* err,
         void* user_data) {
+    LoggerD("Enter %d, err: %s", state, err);
+    getInstance().m_state = state;
+}
+
+void PushManager::onPushNotify(push_service_notification_h noti, void* user_data) {
     LoggerD("Enter");
     if (!getInstance().m_listener) {
         LoggerW("Listener not set, ignoring");
         return;
     }
-    double* callbackId = static_cast<double*>(user_data);
-    std::string id;
-    PlatformResult res(ErrorCode::NO_ERROR);
-    if (result == PUSH_RESULT_SUCCESS) {
-        LoggerD("Success");
-        char *temp = NULL;
-        int ret = push_get_registration_id(getInstance().m_handle, &temp);
-        if (ret == PUSH_ERROR_NONE) {
-            LoggerD("Registration id retrieved");
-            id = temp;
-            free(temp);
-        } else {
-            res = LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                "Failed to retrieve registration id",
-                ("Failed to retrieve registration id: push_get_registration_id(%d)", ret));
-        }
+
+    getInstance().m_listener->onPushNotify(noti);
+}
+
+void PushManager::onApplicationRegister(push_service_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);
+  std::string id;
+  PlatformResult res(ErrorCode::NO_ERROR);
+
+  if (PUSH_SERVICE_RESULT_SUCCESS == result) {
+    LoggerD("Success");
+    char *temp = nullptr;
+    int ret = push_service_get_registration_id(getInstance().m_handle, &temp);
+    if (PUSH_SERVICE_ERROR_NONE == ret) {
+      LoggerD("Registration id retrieved");
+      id = temp;
+      free(temp);
     } else {
-        if (result == PUSH_RESULT_TIMEOUT) {
-            LoggerE("PUSH_RESULT_TIMEOUT");
-        } else if (result == PUSH_RESULT_SERVER_ERROR) {
-            LoggerE("PUSH_RESULT_SERVER_ERROR");
-        } else if (result == PUSH_RESULT_SYSTEM_ERROR) {
-            LoggerE("PUSH_RESULT_SYSTEM_ERROR");
-        }
-        res = LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
-                msg == NULL ? "Unknown error" : msg);
+      res = LogAndCreateResult(
+          ErrorCode::UNKNOWN_ERR, "Failed to retrieve registration id",
+          ("Failed to retrieve registration id: push_service_get_registration_id(%d)", ret));
     }
-    // onPushState is not always called when onPushRegister is successfull
-    getInstance().m_state = PUSH_STATE_REGISTERED;
-    getInstance().m_listener->onPushRegister(*callbackId, res, id);
-    delete callbackId;
+  } else {
+    if (PUSH_SERVICE_RESULT_TIMEOUT == result) {
+      LoggerE("PUSH_SERVICE_RESULT_TIMEOUT");
+    } else if (PUSH_SERVICE_RESULT_SERVER_ERROR == result) {
+      LoggerE("PUSH_SERVICE_RESULT_SERVER_ERROR");
+    } else if (PUSH_SERVICE_RESULT_SYSTEM_ERROR == result) {
+      LoggerE("PUSH_SERVICE_RESULT_SYSTEM_ERROR");
+    }
+    res = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, msg == nullptr ? "Unknown error" : msg);
+  }
+
+  // onPushState is not always called when onPushRegister is successful
+  getInstance().m_state = PUSH_SERVICE_STATE_REGISTERED;
+  getInstance().m_listener->onPushRegister(*callbackId, res, id);
+  delete callbackId;
 }
 
 gboolean PushManager::onFakeDeregister(gpointer user_data) {
@@ -360,7 +412,7 @@ gboolean PushManager::onFakeDeregister(gpointer user_data) {
     return G_SOURCE_REMOVE;
 }
 
-void PushManager::onDeregister(push_result_e result, const char* msg,
+void PushManager::onDeregister(push_service_result_e result, const char* msg,
         void* user_data) {
     LoggerD("Enter");
     if (!getInstance().m_listener) {
@@ -368,7 +420,7 @@ void PushManager::onDeregister(push_result_e result, const char* msg,
         return;
     }
     double* callbackId = static_cast<double*>(user_data);
-    if (result == PUSH_RESULT_SUCCESS) {
+    if (result == PUSH_SERVICE_RESULT_SUCCESS) {
         getInstance().m_listener->onDeregister(*callbackId,
             PlatformResult(ErrorCode::NO_ERROR));
     } else {
@@ -379,6 +431,23 @@ void PushManager::onDeregister(push_result_e result, const char* msg,
     delete callbackId;
 }
 
+
+std::string PushManager::GetEncodedBundle() {
+  LoggerD("Entered");
+
+  std::string result;
+  std::size_t size = 512;
+
+  // make sure we read the whole variable, if the length of read variable is equal
+  // to the size we were trying to obtain, the variable is likely to be longer
+  do {
+    size <<= 1;
+    result = common::GetCurrentExtension()->GetRuntimeVariable("encoded_bundle", size);
+  } while (strlen(result.c_str()) == size);
+
+  return result;
+}
+
 }  // namespace push
 }  // namespace extension
 
index a1a66e3..6f4906d 100644 (file)
@@ -17,7 +17,7 @@
 #ifndef SRC_PUSH_PUSH_MANAGER_H_
 #define SRC_PUSH_PUSH_MANAGER_H_
 
-#include <push.h>
+#include <push-service.h>
 #include <glib.h>
 #include <string>
 #include <vector>
@@ -31,8 +31,7 @@ class EventListener {
  public:
     virtual void onPushRegister(double callbackId,
             common::PlatformResult result, const std::string& id) = 0;
-    virtual void onPushNotify(const std::string& appData,
-            const std::string& alertMessage, double date) = 0;
+    virtual void onPushNotify(push_service_notification_h noti) = 0;
     virtual void onDeregister(double callbackId,
             common::PlatformResult result) = 0;
     virtual ~EventListener() {}
@@ -44,36 +43,38 @@ class PushManager {
     virtual ~PushManager();
 
     void setListener(EventListener* listener);
-    struct ApplicationControl {
-        std::string operation;
-        std::string uri;
-        std::string mime;
-        std::string category;
-        std::map<std::string, std::vector<std::string> > data;
-    };
-    common::PlatformResult registerService(const ApplicationControl &appControl,
-        double callbackId);
-    common::PlatformResult unregisterService(double callbackId);
+
+    common::PlatformResult registerApplication(double callbackId);
+    common::PlatformResult unregisterApplication(double callbackId);
     common::PlatformResult getRegistrationId(std::string &id);
     common::PlatformResult getUnreadNotifications();
+    common::PlatformResult getPushMessage(picojson::value* out);
+    void notificationToJson(push_service_notification_h noti, picojson::object* obj);
 
  private:
     PushManager();
     void initAppId();
-    static void onPushState(push_state_e state, const char *err,
+    void InitAppControl();
+
+    static void onPushState(push_service_state_e state, const char *err,
         void *user_data);
-    static void onPushNotify(push_notification_h noti, void *user_data);
-    static void onPushRegister(push_result_e result, const char *msg,
+    static void onPushNotify(push_service_notification_h noti, void *user_data);
+    static void onApplicationRegister(push_service_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,
+    static void onDeregister(push_service_result_e result, const char *msg,
         void *user_data);
 
-    push_connection_h m_handle;
+    static std::string GetEncodedBundle();
+
+    push_service_connection_h m_handle;
     EventListener* m_listener;
-    push_state_e m_state;
+    push_service_state_e m_state;
     std::string m_appId;
     std::string m_pkgId;
+
+    app_control_h app_control_;
+    char* operation_;
 };
 
 }  // namespace push