Skip to content

Commit

Permalink
feat(packages): handle GetRuntimeVariable in runtime function
Browse files Browse the repository at this point in the history
Signed-off-by: Hosung Kim [email protected]
  • Loading branch information
hs0225 authored and daeyeon committed Apr 2, 2024
1 parent 1a1cdff commit d7f736e
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 14 deletions.
3 changes: 3 additions & 0 deletions deps/node/src/lwnode/aul-event-receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <unistd.h> // getpid
#include <uv.h>
#include <sstream>
#include "lwnode.h"
#include "trace.h"

#ifdef HOST_TIZEN
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions include/lwnode/lwnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>

namespace Escargot {
class ContextRef;
Expand Down Expand Up @@ -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<std::string> infos_;
private:
SystemInfo();
std::unordered_map<std::string, std::string> infos_;
};

} // namespace LWNode
4 changes: 2 additions & 2 deletions modules/packages/device-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=")

Expand All @@ -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})
11 changes: 11 additions & 0 deletions modules/packages/device-api/src/ExtensionAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "TizenDeviceAPIBase.h"
#include "TizenDeviceAPILoaderForEscargot.h"
#include "TizenRuntimeInfo.h"
#include "ExtensionAdapter.h"

namespace wrt {
Expand Down Expand Up @@ -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);
Expand Down
114 changes: 114 additions & 0 deletions modules/packages/device-api/src/TizenRuntimeInfo.cpp
Original file line number Diff line number Diff line change
@@ -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 <pkgmgr-info.h>
#include <tpk_manifest_handlers/privileges_handler.h>
#include <tpk_manifest_handlers/tpk_config_parser.h>
#include <memory>
#include <sstream>
#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<const tpk::parse::PrivilegesInfo*>(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
51 changes: 51 additions & 0 deletions modules/packages/device-api/src/TizenRuntimeInfo.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <map>

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
4 changes: 4 additions & 0 deletions modules/packages/packaging/lwnode-modules.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions src/lwnode/lwnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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

0 comments on commit d7f736e

Please sign in to comment.