diff --git a/bundle.json b/bundle.json index 4ce709dccbc2532237f52877b0c57aee7121f3ad..b6ab63515c411bf715fe03f96cc3fbc9bff1194a 100644 --- a/bundle.json +++ b/bundle.json @@ -84,6 +84,15 @@ "header_base": "//base/security/access_token/interfaces/innerkits/privacy/include" } }, + { + "name": "//base/security/access_token/interfaces/innerkits/token_callback:libtoken_callback_sdk", + "header": { + "header_files": [ + "token_callback_stub.h" + ], + "header_base": "//base/security/access_token/interfaces/innerkits/token_callback/include" + } + }, { "name": "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", "header": { diff --git a/interfaces/innerkits/token_callback/BUILD.gn b/interfaces/innerkits/token_callback/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..84cea29b70fda6bcdba2113916a6d49da30ba075 --- /dev/null +++ b/interfaces/innerkits/token_callback/BUILD.gn @@ -0,0 +1,59 @@ +# 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") + +config("token_callback") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +if (is_standard_system) { + ohos_shared_library("libtoken_callback_sdk") { + subsystem_name = "security" + part_name = "access_token" + + output_name = "libtoken_callback_sdk" + + public_configs = [ ":token_callback" ] + + include_dirs = [ + "//utils/native/base/include", + "include", + "src", + "//base/security/access_token/frameworks/privacy/include", + "//base/security/access_token/frameworks/common/include", + ] + + sources = [ + "src/token_callback_proxy.cpp", + "src/token_callback_stub.cpp", + ] + + deps = [ + "//base/security/access_token/frameworks/common:accesstoken_common_cxx", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr_standard:samgr_proxy", + ] + + cflags_cc = [ + "-DHILOG_ENABLE", + "-DDEBUG_API_PERFORMANCE", + ] + } +} diff --git a/interfaces/innerkits/token_callback/include/i_token_callback.h b/interfaces/innerkits/token_callback/include/i_token_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..091577b5c6121d581ad91a672196553864059193 --- /dev/null +++ b/interfaces/innerkits/token_callback/include/i_token_callback.h @@ -0,0 +1,38 @@ +/* + * 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 I_TOKEN_CALLBACK_H +#define I_TOKEN_CALLBACK_H + +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +class ITokenCallback : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.accesstoken.tokencallback"); + + virtual void GrantResultsCallback( + const std::vector &permissions, const std::vector &grantResults) = 0; + + enum InterfaceCode { + GRANT_RESULT_CALLBACK = 0, + }; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // I_TOKEN_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/token_callback/include/token_callback_proxy.h b/interfaces/innerkits/token_callback/include/token_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..bba2db4e2779ed35e82050acc040f6b664e00f4e --- /dev/null +++ b/interfaces/innerkits/token_callback/include/token_callback_proxy.h @@ -0,0 +1,41 @@ +/* + * 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 PERMISSION_STATE_CHANGE_CALLBACK_PROXY_H +#define PERMISSION_STATE_CHANGE_CALLBACK_PROXY_H + +#include "i_token_callback.h" + +#include "iremote_proxy.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class TokenCallbackProxy : public IRemoteProxy { +public: + explicit TokenCallbackProxy(const sptr& impl); + ~TokenCallbackProxy() override; + + virtual void GrantResultsCallback( + const std::vector &permissions, const std::vector &grantResults) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERMISSION_STATE_CHANGE_CALLBACK_PROXY_H diff --git a/interfaces/innerkits/token_callback/include/token_callback_stub.h b/interfaces/innerkits/token_callback/include/token_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..541b01c193bcfb263da664950f6826b868bec2f2 --- /dev/null +++ b/interfaces/innerkits/token_callback/include/token_callback_stub.h @@ -0,0 +1,37 @@ +/* + * 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 I_TOKEN_CALLBACK_STUB_H +#define I_TOKEN_CALLBACK_STUB_H + +#include "i_token_callback.h" + +#include "iremote_stub.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class TokenCallbackStub : public IRemoteStub { +public: + TokenCallbackStub() = default; + virtual ~TokenCallbackStub() = default; + + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // I_TOKEN_CALLBACK_STUB_H \ No newline at end of file diff --git a/interfaces/innerkits/token_callback/src/token_callback_proxy.cpp b/interfaces/innerkits/token_callback/src/token_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..add445b8ac47b95ffc72b046ffe38d8910e58d0f --- /dev/null +++ b/interfaces/innerkits/token_callback/src/token_callback_proxy.cpp @@ -0,0 +1,96 @@ +/* + * 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 "token_callback_proxy.h" + +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenCallbackProxy" +}; +static const int32_t LIST_SIZE_MAX = 1024; +} + +TokenCallbackProxy::TokenCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) { +} + +TokenCallbackProxy::~TokenCallbackProxy() +{} + +void TokenCallbackProxy::GrantResultsCallback( + const std::vector &permissions, const std::vector &grantResults) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(ITokenCallback::GetDescriptor())) { + ACCESSTOKEN_LOG_ERROR(LABEL, "get unexpect descriptor"); + return; + } + + uint32_t listSize = permissions.size(); + if (listSize > LIST_SIZE_MAX) { + ACCESSTOKEN_LOG_ERROR(LABEL, "listSize is invalid"); + return; + } + if (!data.WriteUint32(listSize)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write listSize"); + return; + } + for (uint32_t i = 0; i < listSize; i++) { + if (!data.WriteString(permissions[i])) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permList[%{public}d], %{public}s", i, permissions[i].c_str()); + return; + } + } + + uint32_t resultSize = grantResults.size(); + if (resultSize != listSize) { + ACCESSTOKEN_LOG_ERROR(LABEL, "resultSize is invalid"); + return; + } + if (!data.WriteUint32(resultSize)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write resultSize"); + return; + } + for (uint32_t i = 0; i < resultSize; i++) { + if (!data.WriteInt32(grantResults[i])) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permList[%{public}d], %{public}d", i, grantResults[i]); + return; + } + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null."); + return; + } + int32_t requestResult = remote->SendRequest( + static_cast(ITokenCallback::GRANT_RESULT_CALLBACK), data, reply, option); + if (requestResult != NO_ERROR) { + ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult); + return; + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "SendRequest success"); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/token_callback/src/token_callback_stub.cpp b/interfaces/innerkits/token_callback/src/token_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d70b043ee9b388d3d2b8e80c7f6f9cf6ca7c1e8 --- /dev/null +++ b/interfaces/innerkits/token_callback/src/token_callback_stub.cpp @@ -0,0 +1,73 @@ +/* + * 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 "token_callback_stub.h" + +#include "accesstoken_log.h" +#include "string_ex.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "TokenCallbackStub" +}; +static const int32_t LIST_SIZE_MAX = 200; +static const int32_t FAILED = -1; +} + +int32_t TokenCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, code: 0x%{public}x", code); + std::u16string descriptor = data.ReadInterfaceToken(); + if (descriptor != ITokenCallback::GetDescriptor()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "get unexpect descriptor: %{public}s", Str16ToStr8(descriptor).c_str()); + return FAILED; + } + + int32_t msgCode = static_cast(code); + if (msgCode == ITokenCallback::GRANT_RESULT_CALLBACK) { + uint32_t permListSize = data.ReadUint32(); + if (permListSize > LIST_SIZE_MAX) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read permListSize fail"); + return FAILED; + } + std::vector permList; + for (uint32_t i = 0; i < permListSize; i++) { + std::string perm = data.ReadString(); + permList.emplace_back(perm); + } + + uint32_t statusListSize = data.ReadUint32(); + if (statusListSize != permListSize) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read permListSize fail"); + return FAILED; + } + std::vector grantResults; + for (uint32_t i = 0; i < permListSize; i++) { + int32_t res = data.ReadInt32(); + grantResults.emplace_back(res); + } + GrantResultsCallback(permList, grantResults); + } else { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return 0; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file