[Common] Ported CurrentApplication class from old webapi-plugins repository.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 22 Dec 2014 11:37:51 +0000 (12:37 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Mon, 29 Dec 2014 12:44:17 +0000 (13:44 +0100)
Change-Id: I6a1504610c8f96e2e5b6f8f79e812ae6f0d374cb
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
packaging/webapi-plugins.spec
src/common/common.gypi
src/common/current_application.cc [new file with mode: 0644]
src/common/current_application.h [new file with mode: 0644]

index 4ab10eb..2f9e907 100644 (file)
@@ -173,6 +173,8 @@ BuildRequires: pkgconfig(capi-network-wifi)
 BuildRequires: pkgconfig(tapi)
 BuildRequires: pkgconfig(libpcrecpp)
 BuildRequires: pkgconfig(capi-appfw-application)
+BuildRequires: pkgconfig(capi-appfw-app-manager)
+BuildRequires: pkgconfig(capi-appfw-package-manager)
 
 %if 0%{?tizen_feature_power_support}
 BuildRequires: pkgconfig(capi-system-power)
index 1c6a4fd..2f640c1 100644 (file)
@@ -25,6 +25,8 @@
             'dbus-1',
             'dlog',
             'glib-2.0',
+            'capi-appfw-app-manager',
+            'capi-appfw-package-manager'
           ]
         },
       }, {
@@ -71,6 +73,8 @@
     'sources': [
       'converter.cc',
       'converter.h',
+      'current_application.cc',
+      'current_application.h',
       'extension.cc',
       'extension.h',
       'picojson.h',
diff --git a/src/common/current_application.cc b/src/common/current_application.cc
new file mode 100644 (file)
index 0000000..70d63ba
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "common/current_application.h"
+
+#include <app_manager.h>
+#include <package_manager.h>
+
+#include "common/logger.h"
+
+namespace common {
+
+CurrentApplication& CurrentApplication::GetInstance() {
+  static CurrentApplication current_application;
+  return current_application;
+}
+
+pid_t CurrentApplication::GetProcessId() const {
+  return pid_;
+}
+
+std::string CurrentApplication::GetApplicationId() const {
+  return app_id_;
+}
+
+std::string CurrentApplication::GetPackageId() const {
+  return package_id_;
+}
+
+CurrentApplication::CurrentApplication() :
+    pid_(getpid()),
+    app_id_(FetchApplicationId()),
+    package_id_(FetchPackageId()) {
+}
+
+std::string CurrentApplication::FetchApplicationId() const {
+  std::string app_id;
+  char* tmp_str = nullptr;
+
+  const int ret = app_manager_get_app_id(pid_, &tmp_str);
+
+  if ((APP_MANAGER_ERROR_NONE == ret) && (nullptr != tmp_str)) {
+    app_id = tmp_str;
+  } else {
+    LoggerE("Failed to get application ID.");
+  }
+
+  free(tmp_str);
+
+  return app_id;
+}
+
+std::string CurrentApplication::FetchPackageId() const {
+  std::string package_id;
+  char* tmp_str = nullptr;
+
+  const int ret = package_manager_get_package_id_by_app_id(app_id_.c_str(),
+                                                           &tmp_str);
+
+  if ((PACKAGE_MANAGER_ERROR_NONE == ret) && (nullptr != tmp_str)) {
+    package_id = tmp_str;
+  } else {
+    LoggerE("Failed to get package ID.");
+  }
+
+  free(tmp_str);
+
+  return package_id;
+}
+
+}  // namespace common
diff --git a/src/common/current_application.h b/src/common/current_application.h
new file mode 100644 (file)
index 0000000..24562ee
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef COMMON_CURRENT_APPLICATION_H_
+#define COMMON_CURRENT_APPLICATION_H_
+
+#include <string>
+
+namespace common {
+
+class CurrentApplication {
+ public:
+  CurrentApplication(const CurrentApplication&) = delete;
+  CurrentApplication& operator=(const CurrentApplication&) = delete;
+
+  static CurrentApplication& GetInstance();
+  pid_t GetProcessId() const;
+  std::string GetApplicationId() const;
+  std::string GetPackageId() const;
+
+ private:
+  CurrentApplication();
+  std::string FetchApplicationId() const;
+  std::string FetchPackageId() const;
+
+ private:
+  pid_t pid_;
+  std::string app_id_;
+  std::string package_id_;
+};
+
+}  // namespace common
+
+#endif  // COMMON_CURRENT_APPLICATION_H_