From d7f736e493459dd7692d515fb6b7fa6a590b5a96 Mon Sep 17 00:00:00 2001 From: Hosung Kim Date: Tue, 2 Apr 2024 17:14:46 +0900 Subject: [PATCH] feat(packages): handle GetRuntimeVariable in runtime function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hosung Kim hs852.kim@samsung.com --- deps/node/src/lwnode/aul-event-receiver.cc | 3 + include/lwnode/lwnode.h | 15 +-- modules/packages/device-api/CMakeLists.txt | 4 +- .../device-api/src/ExtensionAdapter.cpp | 11 ++ .../device-api/src/TizenRuntimeInfo.cpp | 114 ++++++++++++++++++ .../device-api/src/TizenRuntimeInfo.h | 51 ++++++++ .../packages/packaging/lwnode-modules.spec | 4 + src/lwnode/lwnode.cc | 20 ++- 8 files changed, 208 insertions(+), 14 deletions(-) create mode 100644 modules/packages/device-api/src/TizenRuntimeInfo.cpp create mode 100644 modules/packages/device-api/src/TizenRuntimeInfo.h diff --git a/deps/node/src/lwnode/aul-event-receiver.cc b/deps/node/src/lwnode/aul-event-receiver.cc index 5a409a4afa..f359c890f9 100644 --- a/deps/node/src/lwnode/aul-event-receiver.cc +++ b/deps/node/src/lwnode/aul-event-receiver.cc @@ -19,6 +19,7 @@ #include // getpid #include #include +#include "lwnode.h" #include "trace.h" #ifdef HOST_TIZEN @@ -93,6 +94,8 @@ bool AULEventReceiver::start(int argc, char* argv[]) { appid_ = appid; + LWNode::SystemInfo::getInstance()->add("appid", appid); + initLoggerOutput(true, appid_); LWNODE_DEV_LOG(parsed_bundle); diff --git a/include/lwnode/lwnode.h b/include/lwnode/lwnode.h index bb32130943..e7cb50bf09 100644 --- a/include/lwnode/lwnode.h +++ b/include/lwnode/lwnode.h @@ -20,7 +20,7 @@ #include #include #include -#include +#include namespace Escargot { class ContextRef; @@ -75,16 +75,17 @@ class LWNODE_EXPORT Utils { static std::string trimLastNewLineIfNeeded(std::string&& str); }; -class SystemInfo { - public: +class LWNODE_EXPORT SystemInfo { + public: static SystemInfo* getInstance(); - void add(const char* info); + void add(const char* key, const char* value = ""); bool has(const std::string& info); + bool get(const char* info, std::string& value); - private: - SystemInfo(); - std::vector infos_; + private: + SystemInfo(); + std::unordered_map infos_; }; } // namespace LWNode diff --git a/modules/packages/device-api/CMakeLists.txt b/modules/packages/device-api/CMakeLists.txt index 1f2b1ac883..f4659b0868 100644 --- a/modules/packages/device-api/CMakeLists.txt +++ b/modules/packages/device-api/CMakeLists.txt @@ -3,7 +3,7 @@ set (CMAKE_CXX_STANDARD 11) project (device-api) find_package (PkgConfig REQUIRED) -pkg_check_modules(PACKAGES REQUIRED dlog glib-2.0) +pkg_check_modules(PACKAGES REQUIRED dlog glib-2.0 pkgmgr-info tpk-manifest-handlers) set(CMAKE_C_FLAGS "-std=gnu++14 -fPIE -Wno-invalid-offsetof -Wno-error=format=") @@ -22,4 +22,4 @@ include_directories( add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES}) set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") -target_link_libraries(${PROJECT_NAME} dlog glib-2.0) +target_link_libraries(${PROJECT_NAME} ${PACKAGES_LIBRARIES}) diff --git a/modules/packages/device-api/src/ExtensionAdapter.cpp b/modules/packages/device-api/src/ExtensionAdapter.cpp index b6b3d6268a..be028383ed 100644 --- a/modules/packages/device-api/src/ExtensionAdapter.cpp +++ b/modules/packages/device-api/src/ExtensionAdapter.cpp @@ -5,6 +5,7 @@ #include "TizenDeviceAPIBase.h" #include "TizenDeviceAPILoaderForEscargot.h" +#include "TizenRuntimeInfo.h" #include "ExtensionAdapter.h" namespace wrt { @@ -283,6 +284,16 @@ namespace xwalk { char* value, unsigned int value_len) { + // If the value is 'application_id', 'package_id', 'app_root' or + // 'privileges' the xw_extension is fixed to 0 in webapi. + if (xw_extension == 0) { + const std::string info = + DeviceAPI::TizenRuntimeInfo::getInstance()->getRuntimeVariable(key); + + strncpy(value, info.c_str(), value_len); + return; + } + Extension* extension = GetExtension(xw_extension); CHECK(extension, xw_extension); extension->GetRuntimeVariable(key, value, value_len); diff --git a/modules/packages/device-api/src/TizenRuntimeInfo.cpp b/modules/packages/device-api/src/TizenRuntimeInfo.cpp new file mode 100644 index 0000000000..11f6a8160a --- /dev/null +++ b/modules/packages/device-api/src/TizenRuntimeInfo.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2024-present Samsung Electronics Co., Ltd + * + * 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 "TizenRuntimeInfo.h" +#include +#include +#include +#include +#include +#include "TizenDeviceAPIBase.h" +#include "lwnode/lwnode.h" + +namespace DeviceAPI { + +TizenRuntimeInfo* TizenRuntimeInfo::getInstance() { + static TizenRuntimeInfo s_instance; + return &s_instance; +} + +TizenRuntimeInfo::TizenRuntimeInfo() { + initializeDefaultVariable(); +} + +void TizenRuntimeInfo::initializeDefaultVariable() { + std::string appid; + if (!LWNode::SystemInfo::getInstance()->get("appid", appid)) { + DEVICEAPI_LOG_ERROR("cannot get appid"); + return; + } else { + appid_ = appid; + DEVICEAPI_LOG_INFO("app id: %s", appid.c_str()); + } + + pkgmgrinfo_appinfo_h handle; + if (pkgmgrinfo_appinfo_get_appinfo(appid.c_str(), &handle) != PMINFO_R_OK) { + DEVICEAPI_LOG_ERROR("pkgmgrinfo_appinfo_get_appinfo failed"); + return; + } + + char* pkgid = nullptr; + if (pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid) != PMINFO_R_OK) { + DEVICEAPI_LOG_ERROR("pkgmgrinfo_appinfo_get_pkgid failed"); + } else { + pkgid_ = pkgid; + DEVICEAPI_LOG_INFO("package id: %s", pkgid); + } + + char* rootpath = nullptr; + if (pkgmgrinfo_appinfo_get_root_path(handle, &rootpath) != PMINFO_R_OK) { + DEVICEAPI_LOG_ERROR("pkgmgrinfo_appinfo_get_root_path failed"); + } else { + rootpath_ = rootpath; + DEVICEAPI_LOG_INFO("rootpath: %s", rootpath); + } + + pkgmgrinfo_appinfo_destroy_appinfo(handle); +} + +const std::string TizenRuntimeInfo::privileges() { + if (!privileges_.empty()) { + return privileges_; + } + + tpk::parse::TPKConfigParser parser; + boost::filesystem::path manifestPath(rootpath_ + "/tizen-manifest.xml"); + parser.ParseManifest(manifestPath); + if (!parser.ParseManifest(manifestPath)) { + DEVICEAPI_LOG_ERROR("cannot read manifest"); + return ""; + } + + auto manifestData = parser.GetManifestData(tpk::parse::PrivilegesInfo::key()); + auto privileges = + reinterpret_cast(manifestData.get()) + ->GetPrivileges(); + + std::stringstream stream; + for (auto& privilege : privileges) { + stream << privilege.first << ", "; + } + + privileges_ = stream.str(); + DEVICEAPI_LOG_INFO("privileges: %s", privileges_.c_str()); + + return privileges_; +} + +const std::string TizenRuntimeInfo::getRuntimeVariable(const std::string& key) { + if (key == "application_id") { + return appid_; + } else if (key == "package_id") { + return pkgid_; + } else if (key == "app_root") { + return rootpath_; + } else if (key == "privileges") { + return privileges(); + } + return ""; +} + +} // namespace DeviceAPI diff --git a/modules/packages/device-api/src/TizenRuntimeInfo.h b/modules/packages/device-api/src/TizenRuntimeInfo.h new file mode 100644 index 0000000000..794c59e4a4 --- /dev/null +++ b/modules/packages/device-api/src/TizenRuntimeInfo.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2024-present Samsung Electronics Co., Ltd + * + * 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 __TizenRuntimeInfo__ +#define __TizenRuntimeInfo__ + +#include +#include + +namespace DeviceAPI { + +class TizenRuntimeInfo { + public: + static TizenRuntimeInfo* getInstance(); + + const std::string getRuntimeVariable(const std::string& key); + + private: + TizenRuntimeInfo(); + + void initializeDefaultVariable(); + + static const int kMaxPackageNameSize{512}; + + std::string appid_; + std::string pkgid_; + std::string rootpath_; + std::string privileges_; + + const std::string appid(); + const std::string pkgid(); + const std::string rootpath(); + const std::string privileges(); +}; + +} // namespace DeviceAPI + +#endif diff --git a/modules/packages/packaging/lwnode-modules.spec b/modules/packages/packaging/lwnode-modules.spec index 965212c758..db2b320151 100644 --- a/modules/packages/packaging/lwnode-modules.spec +++ b/modules/packages/packaging/lwnode-modules.spec @@ -23,8 +23,12 @@ Source: %{name}-%{version}.tar.gz BuildRequires: cmake BuildRequires: make BuildRequires: ninja +BuildRequires: boost-devel BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(manifest-parser) +BuildRequires: pkgconfig(tpk-manifest-handlers) ############################################## # Packages for profiles diff --git a/src/lwnode/lwnode.cc b/src/lwnode/lwnode.cc index c37cca043c..a9e41c3b93 100644 --- a/src/lwnode/lwnode.cc +++ b/src/lwnode/lwnode.cc @@ -304,9 +304,9 @@ std::string Utils::trimLastNewLineIfNeeded(std::string&& str) { SystemInfo::SystemInfo() { #ifdef HOST_TIZEN - infos_.push_back("tizen"); + add("tizen"); #else - infos_.push_back("linux"); + add("linux"); #endif } @@ -315,12 +315,22 @@ SystemInfo* SystemInfo::getInstance() { return &s_instance; } -void SystemInfo::add(const char* info) { - infos_.push_back(info); +void SystemInfo::add(const char* info, const char* value) { + infos_.insert({info, value}); } bool SystemInfo::has(const std::string& info) { - return std::find(infos_.begin(), infos_.end(), info) != infos_.end(); + return infos_.find(info) != infos_.end(); +} + +bool SystemInfo::get(const char* info, std::string& value) { + auto iter = infos_.find(info); + if (iter == infos_.end()) { + return false; + } + + value = iter->second; + return true; } } // namespace LWNode