[Alarm][Notification] Refactored notification creation 32/146032/2
authorPiotr Kosko <p.kosko@samsung.com>
Thu, 24 Aug 2017 12:57:30 +0000 (14:57 +0200)
committerPiotr Kosko <p.kosko@samsung.com>
Thu, 31 Aug 2017 09:55:18 +0000 (11:55 +0200)
[Feature] Avoiding copying the notification json by passing
  picojson::value to getNotiHandleFromJson method.

[Verification] Code compiles without errors.
  TCT passrate of Alarm and Notification didn't change.

Change-Id: Ia996809944df74ab8c80d3bebe8f73ad7b060796
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/alarm/alarm_manager.cc
src/notification/common_notification.cc
src/notification/common_notification.h
src/notification/notification_manager.cc
src/notification/status_notification.cc
src/notification/status_notification.h
src/notification/user_notification.cc
src/notification/user_notification.h

index e4747cc9456aebcbcf19ca175d6aefe21799f9c9..6bb33e4f0f5f5067843ed95168b12990e16f74e8 100755 (executable)
@@ -270,9 +270,7 @@ void AlarmManager::AddAlarmNotification(const picojson::value& args, picojson::o
   };
 
   using namespace std::placeholders;
-  std::function <PlatformResult(const picojson::object& args,
-                                bool is_update,
-                                notification_h *noti_handle)> impl {};
+  GetHandleFromJsonFun impl {};
   if (args.contains("newImpl") && args.get("newImpl").is<bool>()
       && args.get("newImpl").get<bool>()) {
     LoggerD("New implementation");
@@ -282,8 +280,7 @@ void AlarmManager::AddAlarmNotification(const picojson::value& args, picojson::o
     impl = std::bind(&StatusNotification::GetNotiHandleFromJson, _1, _2, _3);
   }
 
-  PlatformResult platform_result = impl(args.get<picojson::object>(),
-                                        false, &notification_handle);
+  PlatformResult platform_result = impl(args.get("notification"), false, &notification_handle);
   if (!platform_result) {
     LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, platform_result.message().c_str()),
                       &out);
index ea7f87c30f8d4f28da0d6a03717c7287cccb36fe..a62df7a7b0ba8ea0de8553f17cf50485395137ba 100644 (file)
@@ -760,9 +760,7 @@ PlatformResult CommonNotification::UpdateNotificationAfterPost(
     notification_h noti_handle, int id, picojson::object* out_ptr) {
   time_t posted_time;
   PlatformResult status = GetPostedTime(noti_handle, &posted_time);
-  if (status.IsError()) {
-    return status;
-  }
+  CHECK_ERROR(status);
 
   picojson::object& out = *out_ptr;
   out["postedTime"] =
@@ -786,11 +784,12 @@ PlatformResult CommonNotification::PostNotification(const picojson::object& args
     notification_free(noti_handle);
   };
 
-  PlatformResult status = getHandle(args, is_update, &noti_handle);
-
-  if (status.IsError()) {
-    return status;
+  const auto& noti_val_it = args.find("notification");
+  if (args.end() == noti_val_it) {
+    return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot post notification");
   }
+  PlatformResult status = getHandle(noti_val_it->second, is_update, &noti_handle);
+  CHECK_ERROR(status);
 
   if (is_update) {
     ret = notification_update(noti_handle);
index ba23662b3cda1b0e803762193c0332b366ca5006..47c7079af0f4354734e214e30769fb0f423a4aef 100644 (file)
@@ -40,9 +40,10 @@ const std::string kProgressTypeByte = "BYTE";
 typedef std::map<int, notification_text_type_e> InformationEnumMap;
 typedef std::map<int, notification_image_type_e> ImageEnumMap;
 
-typedef std::function<common::PlatformResult(const picojson::object& args,
-                                             bool is_update,
-                                             notification_h *noti_handle)> GetHandleFromJsonFun;
+XW_EXPORT typedef std::function<
+    common::PlatformResult(const picojson::value& noti_val,
+                           bool is_update,
+                           notification_h *noti_handle)> GetHandleFromJsonFun;
 
 class CommonNotification {
  public:
index 2476484d7960339492a1eeae27ef5d2149d5a82d..d457437049736caa8b40637a4f48b70abbbb0153 100644 (file)
@@ -280,10 +280,12 @@ common::PlatformResult NotificationManager::SaveTemplate(const picojson::object&
 
   PlatformResult status = PlatformResult(ErrorCode::NO_ERROR);
   const auto& new_impl_it = args.find("newImpl");
-  if (args.end() != new_impl_it && new_impl_it->second.get<bool>()) {
-    status = UserNotification::GetNotiHandleFromJson(args, false, &noti_handle);
+  const auto& noti_val_it = args.find("notification");
+  if (args.end() != new_impl_it && args.end() != noti_val_it
+      && new_impl_it->second.get<bool>()) {
+    status = UserNotification::GetNotiHandleFromJson(noti_val_it->second, false, &noti_handle);
   } else {
-    status = StatusNotification::GetNotiHandleFromJson(args, false, &noti_handle);
+    status = StatusNotification::GetNotiHandleFromJson(noti_val_it->second, false, &noti_handle);
   }
 
   if (status.IsError()) {
index 40be3a0cab7aa42f95e677b53e5c102d5ba36adb..303c22524270569384aad66c011456f5217aa03a 100644 (file)
@@ -100,13 +100,11 @@ PlatformResult StatusNotification::ToJson(int id,
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-PlatformResult StatusNotification::GetNotiHandleFromJson(const picojson::object& args,
+PlatformResult StatusNotification::GetNotiHandleFromJson(const picojson::value& noti_val,
                                                          bool is_update,
                                                          notification_h *noti_handle) {
   LoggerD("Enter");
-  picojson::object noti_obj =
-      common::FromJson<picojson::object>(args, "notification");
-  picojson::value noti_val(noti_obj);
+  picojson::object noti_obj = noti_val.get<picojson::object>();
 
   notification_h tmp_noti = nullptr;
   // statusType, id
index 9c573e003bc60438d8757edb44383756220f8e9f..5e767e73bd1babaae2853fe5362c510182f8c160 100644 (file)
@@ -36,7 +36,7 @@ class StatusNotification : public CommonNotification {
                                                  notification_h noti_handle,
                                                  app_control_h app_handle,
                                                  picojson::object* out_ptr);
-  XW_EXPORT static common::PlatformResult GetNotiHandleFromJson(const picojson::object& args,
+  XW_EXPORT static common::PlatformResult GetNotiHandleFromJson(const picojson::value& noti_val,
                                                                 bool is_update,
                                                                 notification_h *noti_handle);
   static common::PlatformResult PostStatusNotification(const picojson::object& args,
index 7185d98cc53088f9d6af7b8119be174ee2565daf..1c498e152abfca228afb12587adbb987121df4a9 100644 (file)
@@ -64,15 +64,11 @@ PlatformResult UserNotification::ToJson(int id,
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-PlatformResult UserNotification::GetNotiHandleFromJson(const picojson::object& args,
+PlatformResult UserNotification::GetNotiHandleFromJson(const picojson::value& noti_val,
                                                        bool is_update,
                                                        notification_h *noti_handle) {
   LoggerD("Enter");
-  // TODO change this function to use const picojson::value& args
-  // instead of object (no copying would be needed)
-  picojson::object noti_obj =
-      common::FromJson<picojson::object>(args, "notification");
-  picojson::value noti_val(noti_obj);
+  picojson::object noti_obj = noti_val.get<picojson::object>();
 
   notification_h tmp_noti = nullptr;
   PlatformResult status = InitNotiFromJson(noti_obj, "userType", is_update, &tmp_noti);
index 47f5b7fe2c7f9490c975c7776d702903d114ff46..3ed1dfdcef018ce04456371bd6bf6e64ea51dc4f 100644 (file)
@@ -28,7 +28,7 @@ class UserNotification : public CommonNotification {
                                                  notification_h noti_handle,
                                                  app_control_h app_handle,
                                                  picojson::object* out_ptr);
-  XW_EXPORT static common::PlatformResult GetNotiHandleFromJson(const picojson::object& args,
+  XW_EXPORT static common::PlatformResult GetNotiHandleFromJson(const picojson::value& noti_val,
                                                                 bool is_update,
                                                                 notification_h *noti_handle);
   static common::PlatformResult PostUserNotification(const picojson::object& args,