Modify sending AUL signal
[platform/core/appfw/app-installers.git] / src / common / pkgmgr_signal.cc
index 3a93205..1f5ba98 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <manifest_parser/utils/logging.h>
 
+#include <aul.h>
+#include <bundle_cpp.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <systemd/sd-login.h>
@@ -15,7 +17,7 @@
 #include <map>
 #include <vector>
 
-#include "common/pkgmgr_query.h"
+#include "common/utils/pkgmgr_query.h"
 
 namespace {
 
@@ -24,7 +26,8 @@ namespace ci = common_installer;
 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 const std::map<ci::RequestType, const char*> kEventStr = {
   {ci::RequestType::Install, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
-  {ci::RequestType::Recovery, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
+  {ci::RequestType::Recovery, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
+  {ci::RequestType::RecoveryUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
   {ci::RequestType::Reinstall, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
   {ci::RequestType::Uninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
   {ci::RequestType::PartialUninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
@@ -50,30 +53,34 @@ const std::map<ci::RequestType, const char*> kEventStr = {
 
 namespace common_installer {
 
-PkgmgrSignal::State PkgmgrSignal::state_ = PkgmgrSignal::State::NOT_SENT;
 
-PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi, RequestType req_type)
-    : pi_(pi), request_type_(req_type), error_message_sent_(false) {
+PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi)
+    : pi_(pi), state_(State::NOT_SENT), request_type_(RequestType::Unknown),
+      error_message_sent_(false), is_upgrade_(false) {
   uid_ = pkgmgr_installer_get_uid(pi_);
   request_mode_ = GetRequestMode(uid_);
 }
 
 bool PkgmgrSignal::SendStarted(
     const std::string& type, const std::string& pkgid) {
-  if (state_ != State::NOT_SENT) {
+  if (state_ != State::NOT_SENT)
     return false;
-  }
 
   if (!SetupUserList(pkgid))
     LOG(WARNING) << "Failed to setup user list";
 
   auto key = kEventStr.find(request_type_);
-  if (key == kEventStr.end()) {
+  if (key == kEventStr.end())
     return false;
-  }
-  if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, key->second, type, pkgid)) {
+
+  if (strcmp(key->second, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
+    is_upgrade_ = true;
+
+  pkgmgr_installer_set_is_upgrade(pi_, is_upgrade_ ? 1 : 0);
+
+  if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, key->second, type, pkgid))
     return false;
-  }
+
   for (auto l : user_list_) {
     key = kEventStr.find(l.second);
     if (key == kEventStr.end())
@@ -97,44 +104,48 @@ bool PkgmgrSignal::SendStarted(
 
 bool PkgmgrSignal::SendProgress(int progress,
     const std::string& type, const std::string& pkgid) {
-  if (state_ != State::STARTED) {
+  if (state_ != State::STARTED)
     return false;
-  }
 
   if (!SendSignal(PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
       std::to_string(progress).c_str(), type, pkgid))
     return false;
-  for (auto l : user_list_)
+  for (auto l : user_list_) {
     // ignore error case
     SendSignal(l.first, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
         std::to_string(progress).c_str(), type, pkgid);
+  }
 
   return true;
 }
 
 bool PkgmgrSignal::SendFinished(
     Step::Status result, const std::string& type, const std::string& pkgid) {
-  if (state_ != State::STARTED) {
+  if (state_ != State::STARTED)
     return false;
-  }
+
+  pkgmgr_installer_set_is_upgrade(pi_, is_upgrade_ ? 1 : 0);
+
   if (result != Step::Status::OK && !error_message_sent_) {
     if (!SendSignal(
         PKGMGR_INSTALLER_ERROR_KEY_STR,
-        std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
+        std::to_string(static_cast<int>(result)).c_str(), type, pkgid))
       return false;
-    }
+
     for (auto l : user_list_)
       SendSignal(l.first,
           PKGMGR_INSTALLER_ERROR_KEY_STR,
           std::to_string(static_cast<int>(result)).c_str(), type, pkgid);
   }
+
   if (!SendSignal(
-      PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
+      PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid))
     return false;
-  }
+
   for (auto l : user_list_)
     SendSignal(l.first,
         PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid);
+
   state_ = State::FINISHED;
   return true;
 }
@@ -144,13 +155,15 @@ bool PkgmgrSignal::SendError(
     const std::string& error_message,
     const std::string& type,
     const std::string& pkgid) {
-  if (state_ != State::STARTED) {
+  if (state_ != State::STARTED)
     return false;
-  }
+
+  pkgmgr_installer_set_is_upgrade(pi_, is_upgrade_ ? 1 : 0);
+
   std::string error_value = std::to_string(static_cast<int>(result));
-  if (!error_message.empty()) {
+  if (!error_message.empty())
     error_value = error_value + ":" + error_message;
-  }
+
   LOG(ERROR) << "PkgmgrSignal error_value: (" << error_value << ")";
   error_message_sent_ = true;
   if (!SendSignal(
@@ -168,11 +181,28 @@ bool PkgmgrSignal::SendError(
   return true;
 }
 
+void PkgmgrSignal::SetRequestType(RequestType req_type) {
+  request_type_ = req_type;
+}
+
 bool PkgmgrSignal::SendSignal(
     const char* key,
     const char* value,
     const std::string& type,
     const std::string& pkgid) const {
+  if (!strcmp(key, "start") || !strcmp(key, "error") || !strcmp(key, "end")) {
+    uid_t uid = pkgmgr_installer_get_uid(pi_);
+    tizen_base::Bundle b({
+        { AUL_K_PKGID, pkgid },
+        { AUL_K_PACKAGETYPE, type },
+        { AUL_K_PKG_EVENT_NAME, key },
+        { AUL_K_PKG_EVENT_RESULT, value }
+        });
+
+    if (aul_package_pre_event_send(uid, b.GetHandle()) != AUL_R_OK)
+      LOG(ERROR) << "aul_package_pre_event_send() is failed";
+  }
+
   // send pkgmgr signal
   if (pkgmgr_installer_send_signal(
         pi_,
@@ -229,7 +259,8 @@ const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
 bool PkgmgrSignal::SendAppids(const std::string& type,
                               const std::string& pkgid) const {
   std::vector<std::string> appids;
-  if (!QueryAppidsForPkgId(pkgid, &appids, pkgmgr_installer_get_uid(pi_)))
+  ci::PkgQueryInterface pkg_query(pkgid, pkgmgr_installer_get_uid(pi_));
+  if (!pkg_query.AppidsForPkgId(&appids))
     return true;
   for (auto& appid : appids) {
     if (pkgmgr_installer_send_app_uninstall_signal(pi_, type.c_str(),
@@ -244,7 +275,8 @@ bool PkgmgrSignal::SendAppids(uid_t uid,
                               const std::string& type,
                               const std::string& pkgid) const {
   std::vector<std::string> appids;
-  if (!QueryAppidsForPkgId(pkgid, &appids, pkgmgr_installer_get_uid(pi_)))
+  ci::PkgQueryInterface pkg_query(pkgid, pkgmgr_installer_get_uid(pi_));
+  if (!pkg_query.AppidsForPkgId(&appids))
     return true;
   for (auto& appid : appids) {
     if (pkgmgr_installer_send_app_uninstall_signal_for_uid(
@@ -255,18 +287,18 @@ bool PkgmgrSignal::SendAppids(uid_t uid,
 }
 
 bool PkgmgrSignal::SetupUserList(const std::string& pkgid) {
-  int i;
   uid_t* uids = nullptr;
   int n = sd_get_uids(&uids);
 
   if (n < 0)
     return false;
-
-  for (i = 0; i < n; i++) {
+  ci::PkgQueryInterface pkg_query_for_global(pkgid, kGlobalUserUid);
+  for (int i = 0; i < n; i++) {
+    ci::PkgQueryInterface pkg_query(pkgid, uids[i]);
     switch (request_mode_) {
       case RequestMode::GLOBAL:
         // if user pkg is installed, installer will not send signal to user.
-        if (QueryIsPackageInstalled(pkgid, RequestMode::USER, uids[i]))
+        if (pkg_query.IsPackageInstalled(ci::RequestMode::USER))
           continue;
         else
           user_list_.emplace_back(uids[i], request_type_);
@@ -274,12 +306,7 @@ bool PkgmgrSignal::SetupUserList(const std::string& pkgid) {
       case RequestMode::USER:
         if (uid_ != uids[i])
           continue;
-        // if global pkg is installed,
-        // user pkg operation will be handled as update.
-        if (QueryIsPackageInstalled(pkgid, kGlobalUserUid))
-          user_list_.emplace_back(uids[i], RequestType::Update);
-        else
-          user_list_.emplace_back(uids[i], request_type_);
+        user_list_.emplace_back(uids[i], request_type_);
         break;
       default:
         user_list_.emplace_back(uids[i], request_type_);