同步代码到interface_sdk_c仓

Signed-off-by: huangdongting <huangdongting3@huawei.com>
This commit is contained in:
huangdongting
2023-08-03 20:31:01 +08:00
parent 8d73a1802a
commit 5733835c23
446 changed files with 146243 additions and 0 deletions
@@ -0,0 +1,71 @@
# Copyright (c) 2022 Huawei Device 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.
import("//build/ohos.gni")
import("../../../../appexecfwk.gni")
config("bundle_ndk_config") {
include_dirs = [ "include" ]
}
ohos_ndk_headers("bundle_header") {
dest_dir = "$ndk_headers_out_dir/bundle/"
sources = [ "./include/native_interface_bundle.h" ]
}
ohos_ndk_library("libbundle_ndk") {
ndk_description_file = "./libbundle.ndk.json"
min_compact_version = "9"
output_name = "bundle_ndk"
}
ohos_shared_library("bundle_ndk") {
public_configs = [ ":bundle_ndk_config" ]
include_dirs = [
"${common_path}/log/include",
"${inner_api_path}/appexecfwk_base/include",
"${inner_api_path}/appexecfwk_base/include/quick_fix",
"${inner_api_path}/appexecfwk_base/include/overlay",
"//third_party/json/include",
]
defines = [
"APP_LOG_TAG = \"BundleMgrService\"",
"LOG_DOMAIN = 0xD001120",
]
sources = [
"${inner_api_path}/appexecfwk_base/src/ability_info.cpp",
"${inner_api_path}/appexecfwk_base/src/application_info.cpp",
"${inner_api_path}/appexecfwk_base/src/bundle_info.cpp",
"${inner_api_path}/appexecfwk_base/src/compatible_application_info.cpp",
"${inner_api_path}/appexecfwk_base/src/extension_ability_info.cpp",
"${inner_api_path}/appexecfwk_base/src/hap_module_info.cpp",
"${inner_api_path}/appexecfwk_base/src/module_info.cpp",
"${inner_api_path}/appexecfwk_base/src/overlay/overlay_bundle_info.cpp",
"${inner_api_path}/appexecfwk_base/src/overlay/overlay_module_info.cpp",
"${inner_api_path}/appexecfwk_base/src/quick_fix/app_quick_fix.cpp",
"${inner_api_path}/appexecfwk_base/src/quick_fix/appqf_info.cpp",
"${inner_api_path}/appexecfwk_base/src/quick_fix/hqf_info.cpp",
"src/bundle_mgr_proxy_native.cpp",
"src/native_interface_bundle.cpp",
]
deps = [ "${common_path}:libappexecfwk_common" ]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
subsystem_name = "bundlemanager"
part_name = "bundle_framework"
}
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022 Huawei Device 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 FOUNDATION_APPEXECFWK_KITS_APPKIT_NATIVE_BUNDLE_INCLUDE_BUNDLE_MGR_PROXY_NATIVE_H
#define FOUNDATION_APPEXECFWK_KITS_APPKIT_NATIVE_BUNDLE_INCLUDE_BUNDLE_MGR_PROXY_NATIVE_H
#include <string>
#include "application_info.h"
#include "bundle_info.h"
#include "iremote_broker.h"
#include "iremote_object.h"
namespace OHOS {
namespace AppExecFwk {
class BundleMgrProxyNative {
public:
BundleMgrProxyNative() = default;
virtual ~BundleMgrProxyNative() = default;
/**
* @brief Obtains the BundleInfo based on calling uid.
* @param bundleName Indicates the application bundle name to be queried.
* @param flags Indicates the information contained in the BundleInfo object to be returned.
* @param bundleInfo Indicates the obtained BundleInfo object.
* @param userId Indicates the user ID.
* @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
*/
bool GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo);
enum {
GET_BUNDLE_INFO_FOR_SELF_NATIVE = 98
};
private:
sptr<IRemoteObject> GetBmsProxy();
template <typename T>
bool GetParcelableInfo(uint32_t code, MessageParcel &data, T &parcelableInfo);
bool SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply);
};
} // namespace AppExecFwk
} // namespace OHOS
#endif // FOUNDATION_APPEXECFWK_KITS_APPKIT_NATIVE_BUNDLE_INCLUDE_BUNDLE_MGR_PROXY_NATIVE_H
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2022 Huawei Device 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.
*/
/**
* @addtogroup Native_Bundle
* @{
*
* @brief Describes the Native Bundle.
*
* @since 9
* @version 1.0
*/
/**
* @file native_interface_bundle.h
*
* @brief Declares the <b>Bundle</b>-specific function, including function for obtaining application info.
*
* @since 9
* @version 1.0
*/
#ifndef FOUNDATION_APPEXECFWK_STANDARD_KITS_APPKIT_NATIVE_BUNDLE_INCLUDE_NATIVE_INTERFACE_BUNDLE_H
#define FOUNDATION_APPEXECFWK_STANDARD_KITS_APPKIT_NATIVE_BUNDLE_INCLUDE_NATIVE_INTERFACE_BUNDLE_H
#ifdef __cplusplus
extern "C" {
#endif
struct OH_NativeBundle_ApplicationInfo {
char* bundleName;
char* fingerprint;
};
/**
* @brief Obtains the application info based on the The current bundle.
*
* @return Returns the application info.
* @since 9
* @version 1.0
*/
OH_NativeBundle_ApplicationInfo OH_NativeBundle_GetCurrentApplicationInfo();
#ifdef __cplusplus
};
#endif
#endif // FOUNDATION_APPEXECFWK_STANDARD_KITS_APPKIT_NATIVE_BUNDLE_INCLUDE_NATIVE_INTERFACE_BUNDLE_H
@@ -0,0 +1,6 @@
[
{
"first_introduced": "9",
"name": "OH_NativeBundle_GetCurrentApplicationInfo"
}
]
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2022 Huawei Device 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 "bundle_mgr_proxy_native.h"
#include "app_log_wrapper.h"
#include "if_system_ability_manager.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "string_ex.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace AppExecFwk {
namespace {
const std::u16string BMS_PROXY_INTERFACE_TOKEN = u"ohos.appexecfwk.BundleMgr";
}
sptr<IRemoteObject> BundleMgrProxyNative::GetBmsProxy()
{
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
APP_LOGE("fail to get samgr.");
return nullptr;
}
return samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
}
bool BundleMgrProxyNative::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
{
APP_LOGI("begin to get bundle info for self");
MessageParcel data;
if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
APP_LOGE("fail to GetBundleInfoForSelf due to write InterfaceToken fail");
return false;
}
if (!data.WriteInt32(flags)) {
APP_LOGE("fail to GetBundleInfoForSelf due to write flag fail");
return false;
}
if (!GetParcelableInfo<BundleInfo>(GET_BUNDLE_INFO_FOR_SELF_NATIVE, data, bundleInfo)) {
APP_LOGE("fail to GetBundleInfoForSelf from server");
return false;
}
return true;
}
bool BundleMgrProxyNative::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply)
{
MessageOption option(MessageOption::TF_SYNC);
sptr<IRemoteObject> remote = GetBmsProxy();
if (remote == nullptr) {
APP_LOGE("fail to send transact cmd %{public}d due to remote object", code);
return false;
}
int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
if (result != NO_ERROR) {
APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code);
return false;
}
return true;
}
template<typename T>
bool BundleMgrProxyNative::GetParcelableInfo(uint32_t code, MessageParcel &data, T &parcelableInfo)
{
MessageParcel reply;
if (!SendTransactCmd(code, data, reply)) {
return false;
}
int32_t res = reply.ReadInt32();
if (res != NO_ERROR) {
APP_LOGE("reply result failed");
return false;
}
std::unique_ptr<T> info(reply.ReadParcelable<T>());
if (info == nullptr) {
APP_LOGE("readParcelableInfo failed");
return false;
}
parcelableInfo = *info;
APP_LOGD("get parcelable info success");
return true;
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2022 Huawei Device 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 "native_interface_bundle.h"
#include <mutex>
#include <string>
#include "application_info.h"
#include "bundle_info.h"
#include "app_log_wrapper.h"
#include "bundle_mgr_proxy_native.h"
#include "ipc_skeleton.h"
#include "securec.h"
namespace {
const size_t CHAR_MAX_LENGTH = 10240;
}
OH_NativeBundle_ApplicationInfo OH_NativeBundle_GetCurrentApplicationInfo()
{
OH_NativeBundle_ApplicationInfo nativeApplicationInfo;
OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
OHOS::AppExecFwk::BundleInfo bundleInfo;
auto bundleInfoFlag = static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
APP_LOGE("can not get bundleInfo for self");
return nativeApplicationInfo;
};
OHOS::AppExecFwk::ApplicationInfo applicationInfo = bundleInfo.applicationInfo;
size_t bundleNameLen = applicationInfo.bundleName.size();
if ((bundleNameLen == 0) || (bundleNameLen + 1) > CHAR_MAX_LENGTH) {
APP_LOGE("failed due to the length of bundleName is 0 or to long");
return nativeApplicationInfo;
}
nativeApplicationInfo.bundleName = static_cast<char*>(malloc(bundleNameLen + 1));
if (nativeApplicationInfo.bundleName == nullptr) {
APP_LOGE("failed due to malloc error");
return nativeApplicationInfo;
}
if (strcpy_s(nativeApplicationInfo.bundleName, bundleNameLen + 1, applicationInfo.bundleName.c_str()) != EOK) {
APP_LOGE("failed due to strcpy_s error");
free(nativeApplicationInfo.bundleName);
nativeApplicationInfo.bundleName = nullptr;
return nativeApplicationInfo;
}
size_t fingerprintLen = applicationInfo.fingerprint.size();
if ((fingerprintLen == 0) || (fingerprintLen + 1) > CHAR_MAX_LENGTH) {
APP_LOGE("failed due to the length of fingerprint is 0 or to long");
free(nativeApplicationInfo.bundleName);
nativeApplicationInfo.bundleName = nullptr;
return nativeApplicationInfo;
}
nativeApplicationInfo.fingerprint = static_cast<char*>(malloc(fingerprintLen + 1));
if (nativeApplicationInfo.fingerprint == nullptr) {
APP_LOGE("failed due to malloc error");
free(nativeApplicationInfo.bundleName);
nativeApplicationInfo.bundleName = nullptr;
return nativeApplicationInfo;
}
if (strcpy_s(nativeApplicationInfo.fingerprint, fingerprintLen + 1, applicationInfo.fingerprint.c_str()) != EOK) {
APP_LOGE("failed due to strcpy_s error");
free(nativeApplicationInfo.bundleName);
nativeApplicationInfo.bundleName = nullptr;
free(nativeApplicationInfo.fingerprint);
nativeApplicationInfo.fingerprint = nullptr;
return nativeApplicationInfo;
}
APP_LOGI("OH_NativeBundle_GetCurrentApplicationInfo success");
return nativeApplicationInfo;
}