diff --git a/frameworks/accesstoken/BUILD.gn b/frameworks/accesstoken/BUILD.gn index b05a2c71619d22d4fb37c568c55c2522ff4ac886..57c72eeec042f1815639f51ebaf70f335ecaf876 100644 --- a/frameworks/accesstoken/BUILD.gn +++ b/frameworks/accesstoken/BUILD.gn @@ -43,6 +43,8 @@ ohos_shared_library("accesstoken_communication_adapter_cxx") { "src/native_token_info_parcel.cpp", "src/permission_def_parcel.cpp", "src/permission_list_state_parcel.cpp", + "src/permission_state_change_info_parcel.cpp", + "src/permission_state_change_scope_parcel.cpp", "src/permission_state_full_parcel.cpp", ] diff --git a/frameworks/accesstoken/include/i_accesstoken_manager.h b/frameworks/accesstoken/include/i_accesstoken_manager.h index 46312f8d17a6daa96ffb84df853d9db8e323d494..466cafd370da437681a84f4dcb9cb037a3a28967 100644 --- a/frameworks/accesstoken/include/i_accesstoken_manager.h +++ b/frameworks/accesstoken/include/i_accesstoken_manager.h @@ -25,11 +25,13 @@ #include "hap_token_info_for_sync_parcel.h" #include "hap_token_info_parcel.h" #include "iremote_broker.h" +#include "i_permission_state_callback.h" #include "native_token_info_for_sync_parcel.h" #include "native_token_info_parcel.h" #include "permission_def_parcel.h" #include "permission_list_state_parcel.h" #include "permission_state_full_parcel.h" +#include "permission_state_change_scope_parcel.h" namespace OHOS { namespace Security { @@ -62,6 +64,9 @@ public: virtual int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfoParcel& hapTokenInfoRes) = 0; virtual int UpdateHapToken( AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParcel& policyParcel) = 0; + virtual int32_t RegisterPermStateChangeCallback( + const PermStateChangeScopeParcel& scope, const sptr& callback) = 0; + virtual int32_t UnRegisterPermStateChangeCallback(const sptr& callback) = 0; #ifdef TOKEN_SYNC_ENABLE virtual int GetHapTokenInfoFromRemote(AccessTokenID tokenID, @@ -108,6 +113,8 @@ public: DUMP_TOKENINFO = 0xff30, GET_PERMISSION_OPER_STATE = 0xff31, + REGISTER_PERM_STATE_CHANGE_CALLBACK = 0xff32, + UNREGISTER_PERM_STATE_CHANGE_CALLBACK = 0xff33, }; }; } // namespace AccessToken diff --git a/frameworks/accesstoken/include/i_permission_state_callback.h b/frameworks/accesstoken/include/i_permission_state_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..508f28c1e0835b77563c49633fcb4cd25590f13c --- /dev/null +++ b/frameworks/accesstoken/include/i_permission_state_callback.h @@ -0,0 +1,43 @@ +/* + * 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_PERMISSION_STATE_CALLBACK_H +#define I_PERMISSION_STATE_CALLBACK_H + +#include + +#include "access_token.h" +#include "errors.h" +#include "iremote_broker.h" +#include "permission_state_change_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class IPermissionStateCallback : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.accesstoken.IPermissionStateCallback"); + + virtual void PermStateChangeCallback(PermStateChangeInfo& result) = 0; + + enum { + PERMISSION_STATE_CHANGE = 0, + }; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // I_PERMISSION_STATE_CALLBACK_H diff --git a/frameworks/accesstoken/include/permission_state_change_info_parcel.h b/frameworks/accesstoken/include/permission_state_change_info_parcel.h new file mode 100644 index 0000000000000000000000000000000000000000..eb95593c328e9f76b399913d3b169ae559a18983 --- /dev/null +++ b/frameworks/accesstoken/include/permission_state_change_info_parcel.h @@ -0,0 +1,40 @@ +/* + * 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_INFO_PARCEL_H +#define PERMISSION_STATE_CHANGE_INFO_PARCEL_H + +#include "parcel.h" +#include "permission_state_change_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +struct PermissionStateChangeInfoParcel final : public Parcelable { + PermissionStateChangeInfoParcel() = default; + + ~PermissionStateChangeInfoParcel() override = default; + + bool Marshalling(Parcel& out) const override; + + static PermissionStateChangeInfoParcel* Unmarshalling(Parcel& in); + + PermStateChangeInfo changeInfo; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // PERMISSION_STATE_CHANGE_INFO_PARCEL_H diff --git a/frameworks/accesstoken/include/permission_state_change_scope_parcel.h b/frameworks/accesstoken/include/permission_state_change_scope_parcel.h new file mode 100644 index 0000000000000000000000000000000000000000..e1988a8f27bcc112fa80722991116954c28122eb --- /dev/null +++ b/frameworks/accesstoken/include/permission_state_change_scope_parcel.h @@ -0,0 +1,40 @@ +/* + * 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_SCOPE_PARCEL_H +#define PERMISSION_STATE_CHANGE_SCOPE_PARCEL_H + +#include "parcel.h" +#include "permission_state_change_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +struct PermStateChangeScopeParcel final : public Parcelable { + PermStateChangeScopeParcel() = default; + + ~PermStateChangeScopeParcel() override = default; + + bool Marshalling(Parcel& out) const override; + + static PermStateChangeScopeParcel* Unmarshalling(Parcel& in); + + PermStateChangeScope scope; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // PERMISSION_STATE_CHANGE_SCOPE_PARCEL_H diff --git a/frameworks/accesstoken/src/permission_state_change_info_parcel.cpp b/frameworks/accesstoken/src/permission_state_change_info_parcel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..173a2d25b8e3155d6dee4393818521c4d29e1f0e --- /dev/null +++ b/frameworks/accesstoken/src/permission_state_change_info_parcel.cpp @@ -0,0 +1,44 @@ +/* + * 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 "permission_state_change_info_parcel.h" +#include "parcel_utils.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +bool PermissionStateChangeInfoParcel::Marshalling(Parcel& out) const +{ + RETURN_IF_FALSE(out.WriteInt32(this->changeInfo.PermStateChangeType)); + RETURN_IF_FALSE(out.WriteUint32(this->changeInfo.tokenID)); + RETURN_IF_FALSE(out.WriteString(this->changeInfo.permissionName)); + return true; +} + +PermissionStateChangeInfoParcel* PermissionStateChangeInfoParcel::Unmarshalling(Parcel& in) +{ + auto* permissionStateParcel = new (std::nothrow) PermissionStateChangeInfoParcel(); + if (permissionStateParcel == nullptr) { + return nullptr; + } + RELEASE_IF_FALSE(in.ReadInt32(permissionStateParcel->changeInfo.PermStateChangeType), permissionStateParcel); + RELEASE_IF_FALSE(in.ReadUint32(permissionStateParcel->changeInfo.tokenID), permissionStateParcel); + RELEASE_IF_FALSE(in.ReadString(permissionStateParcel->changeInfo.permissionName), permissionStateParcel); + + return permissionStateParcel; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/frameworks/accesstoken/src/permission_state_change_scope_parcel.cpp b/frameworks/accesstoken/src/permission_state_change_scope_parcel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7305a31a39f70f2b615a1a723a7a9d1096f5558a --- /dev/null +++ b/frameworks/accesstoken/src/permission_state_change_scope_parcel.cpp @@ -0,0 +1,63 @@ +/* + * 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 "permission_state_change_scope_parcel.h" +#include "parcel_utils.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +bool PermStateChangeScopeParcel::Marshalling(Parcel& out) const +{ + RETURN_IF_FALSE(out.WriteUint32((this->scope.tokenIDs.size()))); + for (auto& tokenID : this->scope.tokenIDs) { + RETURN_IF_FALSE(out.WriteUint32(tokenID)); + } + + RETURN_IF_FALSE(out.WriteUint32((this->scope.permList.size()))); + for (const auto& permissionName : this->scope.permList) { + RETURN_IF_FALSE(out.WriteString(permissionName)); + } + return true; +} + +PermStateChangeScopeParcel* PermStateChangeScopeParcel::Unmarshalling(Parcel& in) +{ + auto* permStateChangeScopeParcel = new (std::nothrow) PermStateChangeScopeParcel(); + if (permStateChangeScopeParcel == nullptr) { + return nullptr; + } + uint32_t tokenIdListSize = 0; + RELEASE_IF_FALSE(in.ReadUint32(tokenIdListSize), permStateChangeScopeParcel); + RELEASE_IF_FALSE(tokenIdListSize <= TOKENIDS_LIST_SIZE_MAX, permStateChangeScopeParcel); + for (uint32_t i = 0; i < tokenIdListSize; i++) { + AccessTokenID tokenID; + RELEASE_IF_FALSE(in.ReadUint32(tokenID), permStateChangeScopeParcel); + permStateChangeScopeParcel->scope.tokenIDs.emplace_back(tokenID); + } + + uint32_t permListSize = 0; + RELEASE_IF_FALSE(in.ReadUint32(permListSize), permStateChangeScopeParcel); + RELEASE_IF_FALSE(permListSize <= PERMS_LIST_SIZE_MAX, permStateChangeScopeParcel); + for (uint32_t i = 0; i < permListSize; i++) { + std::string permName; + RELEASE_IF_FALSE(in.ReadString(permName), permStateChangeScopeParcel); + permStateChangeScopeParcel->scope.permList.emplace_back(permName); + } + return permStateChangeScopeParcel; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/frameworks/privacy/include/bundle_used_record_parcel.h b/frameworks/privacy/include/bundle_used_record_parcel.h index ce8e57ded993fb1143d029b79cc7a01acd576d40..d94e185e86e65f7a561fd076ea9ca403450d75ae 100644 --- a/frameworks/privacy/include/bundle_used_record_parcel.h +++ b/frameworks/privacy/include/bundle_used_record_parcel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef BUNDLE_USED_RECORD_PRACEL_H -#define BUNDLE_USED_RECORD_PRACEL_H +#ifndef BUNDLE_USED_RECORD_PARCEL_H +#define BUNDLE_USED_RECORD_PARCEL_H #include "parcel.h" #include "permission_used_result.h" @@ -37,4 +37,4 @@ struct BundleUsedRecordParcel final : public Parcelable { } // namespace Security } // namespace OHOS -#endif // BUNDLE_USED_RECORD_PRACEL_H +#endif // BUNDLE_USED_RECORD_PARCEL_H diff --git a/frameworks/privacy/include/permission_used_record_parcel.h b/frameworks/privacy/include/permission_used_record_parcel.h index 9282910c8e48dd9d8a8a29c5b2601c6e407d5bea..c2a24dcce5c50f2be0e00e5ccead0008ea070193 100644 --- a/frameworks/privacy/include/permission_used_record_parcel.h +++ b/frameworks/privacy/include/permission_used_record_parcel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef PERMISSION_USED_RECORD_PRACEL_H -#define PERMISSION_USED_RECORD_PRACEL_H +#ifndef PERMISSION_USED_RECORD_PARCEL_H +#define PERMISSION_USED_RECORD_PARCEL_H #include "parcel.h" #include "permission_used_result.h" @@ -37,4 +37,4 @@ struct PermissionUsedRecordParcel final : public Parcelable { } // namespace Security } // namespace OHOS -#endif // PERMISSION_USED_RECORD_PRACEL_H +#endif // PERMISSION_USED_RECORD_PARCEL_H diff --git a/frameworks/privacy/include/permission_used_request_parcel.h b/frameworks/privacy/include/permission_used_request_parcel.h index 1cf48993983c941ae24e88e1b964f3e11085fba0..e0146d5218af02289faf259cfe6414960ae48dce 100644 --- a/frameworks/privacy/include/permission_used_request_parcel.h +++ b/frameworks/privacy/include/permission_used_request_parcel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef PERMISSION_USED_REQUEST_PRACEL_H -#define PERMISSION_USED_REQUEST_PRACEL_H +#ifndef PERMISSION_USED_REQUEST_PARCEL_H +#define PERMISSION_USED_REQUEST_PARCEL_H #include "parcel.h" #include "permission_used_request.h" @@ -37,4 +37,4 @@ struct PermissionUsedRequestParcel final : public Parcelable { } // namespace Security } // namespace OHOS -#endif // PERMISSION_USED_REQUEST_PRACEL_H +#endif // PERMISSION_USED_REQUEST_PARCEL_H diff --git a/frameworks/privacy/include/permission_used_result_parcel.h b/frameworks/privacy/include/permission_used_result_parcel.h index 5a74e91a6e142f0719d646ff8635e12a53937911..40e3d01fd51187fcdfefa687315b886464fa00da 100644 --- a/frameworks/privacy/include/permission_used_result_parcel.h +++ b/frameworks/privacy/include/permission_used_result_parcel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef PERMISSION_USED_RESPONSE_PRACEL_H -#define PERMISSION_USED_RESPONSE_PRACEL_H +#ifndef PERMISSION_USED_RESPONSE_PARCEL_H +#define PERMISSION_USED_RESPONSE_PARCEL_H #include "parcel.h" #include "permission_used_result.h" @@ -37,4 +37,4 @@ struct PermissionUsedResultParcel final : public Parcelable { } // namespace Security } // namespace OHOS -#endif // PERMISSION_USED_RESPONSE_PRACEL_H +#endif // PERMISSION_USED_RESPONSE_PARCEL_H diff --git a/frameworks/privacy/include/used_record_detail_parcel.h b/frameworks/privacy/include/used_record_detail_parcel.h index 56410d586a55179500f12cc1db7ae16a189219bc..8922fc222149ca57e5b8115406fb00be3ab2f526 100644 --- a/frameworks/privacy/include/used_record_detail_parcel.h +++ b/frameworks/privacy/include/used_record_detail_parcel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef USED_RECORD_DETAIL_PRACEL_H -#define USED_RECORD_DETAIL_PRACEL_H +#ifndef USED_RECORD_DETAIL_PARCEL_H +#define USED_RECORD_DETAIL_PARCEL_H #include "parcel.h" #include "permission_used_result.h" @@ -37,4 +37,4 @@ struct UsedRecordDetailParcel final : public Parcelable { } // namespace Security } // namespace OHOS -#endif // USED_RECORD_DETAIL_PRACEL_H +#endif // USED_RECORD_DETAIL_PARCEL_H diff --git a/interfaces/innerkits/accesstoken/BUILD.gn b/interfaces/innerkits/accesstoken/BUILD.gn index 3a2c1fcd2b083aa136a05dff1896d395d921cd78..482985ca3b451492e44729a84afc809bd6f2b8a4 100644 --- a/interfaces/innerkits/accesstoken/BUILD.gn +++ b/interfaces/innerkits/accesstoken/BUILD.gn @@ -42,6 +42,9 @@ if (is_standard_system) { "src/accesstoken_kit.cpp", "src/accesstoken_manager_client.cpp", "src/accesstoken_manager_proxy.cpp", + "src/perm_state_change_callback_customize.cpp", + "src/permission_state_change_callback.cpp", + "src/permission_state_change_callback_stub.cpp", ] deps = [ diff --git a/interfaces/innerkits/accesstoken/include/accesstoken_death_recipient.h b/interfaces/innerkits/accesstoken/include/accesstoken_death_recipient.h index 974bdb50d70e8ef6298c3fca2f1f3aeba8db8bb6..6de93b0dfc9966a42662c5225f0f640a9a39dd42 100644 --- a/interfaces/innerkits/accesstoken/include/accesstoken_death_recipient.h +++ b/interfaces/innerkits/accesstoken/include/accesstoken_death_recipient.h @@ -26,7 +26,7 @@ class AccessTokenDeathRecipient : public IRemoteObject::DeathRecipient { public: AccessTokenDeathRecipient() {} virtual ~AccessTokenDeathRecipient() = default; - void OnRemoteDied(const wptr &object) override; + void OnRemoteDied(const wptr& object) override; }; } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/accesstoken/include/accesstoken_kit.h b/interfaces/innerkits/accesstoken/include/accesstoken_kit.h index 4ee095db065c8d1b77936ced8368ce784136838c..28fecfb4f77041eeb5eb7104fdf4ef3dff49e7f9 100644 --- a/interfaces/innerkits/accesstoken/include/accesstoken_kit.h +++ b/interfaces/innerkits/accesstoken/include/accesstoken_kit.h @@ -24,7 +24,9 @@ #include "native_token_info.h" #include "permission_def.h" #include "permission_list_state.h" +#include "permission_state_change_info.h" #include "permission_state_full.h" +#include "perm_state_change_callback_customize.h" namespace OHOS { namespace Security { @@ -56,6 +58,9 @@ public: static int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); static int ClearUserGrantedPermissionState(AccessTokenID tokenID); + static int32_t RegisterPermStateChangeCallback( + const std::shared_ptr& callback); + static int32_t UnRegisterPermStateChangeCallback(const std::shared_ptr& callback); #ifdef TOKEN_SYNC_ENABLE static int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync); diff --git a/interfaces/innerkits/accesstoken/include/perm_state_change_callback_customize.h b/interfaces/innerkits/accesstoken/include/perm_state_change_callback_customize.h new file mode 100644 index 0000000000000000000000000000000000000000..8ef8962c34948464d38af59f675cb589a583f20d --- /dev/null +++ b/interfaces/innerkits/accesstoken/include/perm_state_change_callback_customize.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 INTERFACES_INNER_KITS_PERM_STATE_CALLBACK_CUSTOMIZE_H +#define INTERFACES_INNER_KITS_PERM_STATE_CALLBACK_CUSTOMIZE_H + +#include "permission_state_change_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermStateChangeCallbackCustomize { +public: + PermStateChangeCallbackCustomize(); + explicit PermStateChangeCallbackCustomize(const PermStateChangeScope &scopeInfo); + virtual ~PermStateChangeCallbackCustomize(); + + virtual void PermStateChangeCallback(PermStateChangeInfo& result) = 0; + + void GetScope(PermStateChangeScope &scopeInfo) const; + +private: + PermStateChangeScope scopeInfo_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // INTERFACES_INNER_KITS_PERM_STATE_CALLBACK_CUSTOMIZE_H \ No newline at end of file diff --git a/interfaces/innerkits/accesstoken/include/permission_state_change_info.h b/interfaces/innerkits/accesstoken/include/permission_state_change_info.h new file mode 100644 index 0000000000000000000000000000000000000000..8ccf4df5e4d1cb186233a0a4007dbcecedda493f --- /dev/null +++ b/interfaces/innerkits/accesstoken/include/permission_state_change_info.h @@ -0,0 +1,44 @@ +/* + * 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 INTERFACES_INNER_KITS_PERMISSION_STATE_CHANGE_INFO_H +#define INTERFACES_INNER_KITS_PERMISSION_STATE_CHANGE_INFO_H + +#include +#include + +#include "access_token.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +#define TOKENIDS_LIST_SIZE_MAX 1024 +#define PERMS_LIST_SIZE_MAX 1024 + +struct PermStateChangeInfo { + int32_t PermStateChangeType; + AccessTokenID tokenID; + std::string permissionName; +}; + +struct PermStateChangeScope { + std::vector tokenIDs; + std::vector permList; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // INTERFACES_INNER_KITS_PERMISSION_STATE_CHANGE_INFO_H diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_death_recipient.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_death_recipient.cpp index d0608aaccf53944493865818696bc65e3c965eb3..095981870380b80fe7e1068bfc8903106b0ac157 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_death_recipient.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_death_recipient.cpp @@ -24,7 +24,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenDeathRecipient"}; } // namespace -void AccessTokenDeathRecipient::OnRemoteDied(const wptr &object) +void AccessTokenDeathRecipient::OnRemoteDied(const wptr& object) { ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); AccessTokenManagerClient::GetInstance().OnRemoteDiedHandle(); diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index 8168fcb952830d41a1e2f91fe18cfb2212dd1a24..d123d6632e64e7b627e1e1e8582974795055d531 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -22,6 +22,7 @@ #include "data_validator.h" #include "hap_token_info.h" #include "permission_def.h" +#include "perm_state_change_callback_customize.h" namespace OHOS { namespace Security { @@ -302,6 +303,20 @@ int AccessTokenKit::ClearUserGrantedPermissionState(AccessTokenID tokenID) return AccessTokenManagerClient::GetInstance().ClearUserGrantedPermissionState(tokenID); } +int32_t AccessTokenKit::RegisterPermStateChangeCallback( + const std::shared_ptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called"); + return AccessTokenManagerClient::GetInstance().RegisterPermStateChangeCallback(callback); +} + +int32_t AccessTokenKit::UnRegisterPermStateChangeCallback( + const std::shared_ptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called"); + return AccessTokenManagerClient::GetInstance().UnRegisterPermStateChangeCallback(callback); +} + #ifdef TOKEN_SYNC_ENABLE int AccessTokenKit::GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync) { diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp index 7f88044333e47875412c76c06fde0f333de31ab5..7b6c214e1d713ccc05a4d927bd53d7659d8075b1 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp @@ -22,6 +22,7 @@ #include "iservice_registry.h" #include "native_token_info_for_sync_parcel.h" #include "native_token_info.h" +#include "permission_state_change_callback.h" namespace OHOS { namespace Security { @@ -31,6 +32,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenManagerClient" }; } // namespace +static const uint32_t MAX_CALLBACK_MAP_SIZE = 200; AccessTokenManagerClient& AccessTokenManagerClient::GetInstance() { @@ -195,6 +197,84 @@ int AccessTokenManagerClient::ClearUserGrantedPermissionState(AccessTokenID toke return proxy->ClearUserGrantedPermissionState(tokenID); } +int32_t AccessTokenManagerClient::CreatePermStateChangeCallback( + const std::shared_ptr& customizedCb, sptr& callbackObject) +{ + std::lock_guard lock(callbackMutex_); + if (callbackMap_.size() == MAX_CALLBACK_MAP_SIZE) { + ACCESSTOKEN_LOG_ERROR(LABEL, "the maximum number of callback has been reached"); + return RET_FAILED; + } + + auto goalCallback = callbackMap_.find(customizedCb); + if (goalCallback != callbackMap_.end()) { + callbackObject = goalCallback->second->AsObject(); + ACCESSTOKEN_LOG_ERROR(LABEL, "already has the same callback"); + return RET_FAILED; + } else { + sptr callback = new (std::nothrow) PermissionStateChangeCallback(customizedCb); + if (!callback) { + ACCESSTOKEN_LOG_ERROR(LABEL, "memory allocation for callback failed!"); + return RET_FAILED; + } + callbackObject = callback->AsObject(); + callbackMap_[customizedCb] = callback; + } + return RET_SUCCESS; +} + +int32_t AccessTokenManagerClient::RegisterPermStateChangeCallback( + const std::shared_ptr& customizedCb) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called!"); + + if (customizedCb == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "customizedCb is nullptr"); + return RET_FAILED; + } + + sptr callbackObject = nullptr; + int32_t result = CreatePermStateChangeCallback(customizedCb, callbackObject); + if (result != RET_SUCCESS) { + return result; + } + auto proxy = GetProxy(); + if (proxy == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null"); + return RET_FAILED; + } + + PermStateChangeScopeParcel scopeParcel; + customizedCb->GetScope(scopeParcel.scope); + + return proxy->RegisterPermStateChangeCallback(scopeParcel, callbackObject); +} + +int32_t AccessTokenManagerClient::UnRegisterPermStateChangeCallback( + const std::shared_ptr& customizedCb) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s: called!", __func__); + + std::lock_guard lock(callbackMutex_); + auto goalCallback = callbackMap_.find(customizedCb); + if (goalCallback == callbackMap_.end()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "goalCallback already is not exist"); + return RET_FAILED; + } + + auto proxy = GetProxy(); + if (proxy == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null"); + return RET_FAILED; + } + + int32_t result = proxy->UnRegisterPermStateChangeCallback(goalCallback->second->AsObject()); + if (result == RET_SUCCESS) { + callbackMap_.erase(goalCallback); + } + return result; +} + AccessTokenIDEx AccessTokenManagerClient::AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy) { AccessTokenIDEx res = { 0 }; diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h index 82e867de4d1b231a9675520fc0cefbde06a5b805..f2ef276aef302b6666796c5cd93fd63c4bc49124 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h @@ -16,6 +16,7 @@ #ifndef ACCESSTOKEN_MANAGER_CLIENT_H #define ACCESSTOKEN_MANAGER_CLIENT_H +#include #include #include #include @@ -29,7 +30,9 @@ #include "native_token_info.h" #include "nocopyable.h" #include "permission_def.h" +#include "permission_state_change_callback.h" #include "permission_state_full.h" +#include "perm_state_change_callback_customize.h" namespace OHOS { namespace Security { @@ -60,6 +63,10 @@ public: int UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy); int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); + int32_t RegisterPermStateChangeCallback( + const std::shared_ptr& customizedCallback); + int32_t UnRegisterPermStateChangeCallback( + const std::shared_ptr& customizedCb); #ifdef TOKEN_SYNC_ENABLE int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync); @@ -77,6 +84,8 @@ public: private: AccessTokenManagerClient(); + int32_t CreatePermStateChangeCallback( + const std::shared_ptr& customizedCallback, sptr& callback); DISALLOW_COPY_AND_MOVE(AccessTokenManagerClient); std::mutex proxyMutex_; @@ -84,6 +93,8 @@ private: sptr serviceDeathObserver_ = nullptr; void InitProxy(); sptr GetProxy(); + std::mutex callbackMutex_; + std::map, sptr> callbackMap_; }; } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp index d8fd1e7e8ee0766eddeb392943ce47badb8e877e..0b5b01b17ce9d59028779c6ed7901ca490895d99 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp @@ -400,6 +400,80 @@ int AccessTokenManagerProxy::ClearUserGrantedPermissionState(AccessTokenID token return result; } +int32_t AccessTokenManagerProxy::RegisterPermStateChangeCallback( + const PermStateChangeScopeParcel& scope, const sptr& callback) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor())) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken."); + return RET_FAILED; + } + if (!data.WriteParcelable(&scope)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write PermStateChangeScopeParcel."); + return RET_FAILED; + } + if (!data.WriteRemoteObject(callback)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object."); + return RET_FAILED; + } + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null."); + return RET_FAILED; + } + int32_t requestResult = remote->SendRequest( + static_cast(IAccessTokenManager::InterfaceCode::REGISTER_PERM_STATE_CHANGE_CALLBACK), + data, reply, option); + if (requestResult != NO_ERROR) { + ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d.", requestResult); + return RET_FAILED; + } + + int32_t result; + if (!reply.ReadInt32(result)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 fail"); + return RET_FAILED; + } + return result; +} + +int32_t AccessTokenManagerProxy::UnRegisterPermStateChangeCallback(const sptr& callback) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor())) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken."); + return RET_FAILED; + } + if (!data.WriteRemoteObject(callback)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object."); + return RET_FAILED; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null."); + return RET_FAILED; + } + int32_t requestResult = remote->SendRequest( + static_cast(IAccessTokenManager::InterfaceCode::UNREGISTER_PERM_STATE_CHANGE_CALLBACK), + data, reply, option); + if (requestResult != NO_ERROR) { + ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d.", requestResult); + return RET_FAILED; + } + + int32_t result; + if (!reply.ReadInt32(result)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 fail"); + return RET_FAILED; + } + return result; +} + AccessTokenIDEx AccessTokenManagerProxy::AllocHapToken( const HapInfoParcel& hapInfo, const HapPolicyParcel& policyParcel) { diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h index 9bfbf432f5c2b9ab28e31ab01d65f4dbbae29a84..6177b1c6da6ecc6397b9902930b1a0797d505191 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h @@ -61,6 +61,9 @@ public: const HapPolicyParcel& policyPar) override; int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfoParcel& hapTokenInfoRes) override; int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfoParcel& nativeTokenInfoRes) override; + int32_t RegisterPermStateChangeCallback( + const PermStateChangeScopeParcel& scope, const sptr& callback) override; + int32_t UnRegisterPermStateChangeCallback(const sptr& callback) override; #ifdef TOKEN_SYNC_ENABLE int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSyncParcel& hapSyncParcel) override; diff --git a/interfaces/innerkits/accesstoken/src/perm_state_change_callback_customize.cpp b/interfaces/innerkits/accesstoken/src/perm_state_change_callback_customize.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0962ab6eab8eca2cdb0306a21a0d54bab6f493b --- /dev/null +++ b/interfaces/innerkits/accesstoken/src/perm_state_change_callback_customize.cpp @@ -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. + */ + +#include "perm_state_change_callback_customize.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +PermStateChangeCallbackCustomize::PermStateChangeCallbackCustomize() +{} + +PermStateChangeCallbackCustomize::PermStateChangeCallbackCustomize( + const PermStateChangeScope &scopeInfo) : scopeInfo_(scopeInfo) +{} + +PermStateChangeCallbackCustomize::~PermStateChangeCallbackCustomize() +{} + +void PermStateChangeCallbackCustomize::GetScope(PermStateChangeScope &scopeInfo) const +{ + scopeInfo = scopeInfo_; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/accesstoken/src/permission_state_change_callback.cpp b/interfaces/innerkits/accesstoken/src/permission_state_change_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e29a46ebebab97f19b77affacdc372a082be0b18 --- /dev/null +++ b/interfaces/innerkits/accesstoken/src/permission_state_change_callback.cpp @@ -0,0 +1,50 @@ +/* + * 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 "permission_state_change_callback.h" + +#include "access_token.h" +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermissionStateChangeCallback" +}; + +PermissionStateChangeCallback::PermissionStateChangeCallback( + const std::shared_ptr& customizedCallback) + : customizedCallback_(customizedCallback) +{} + +PermissionStateChangeCallback::~PermissionStateChangeCallback() +{} + +void PermissionStateChangeCallback::PermStateChangeCallback(PermStateChangeInfo& result) +{ + if (customizedCallback_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "customizedCallback_ is nullptr"); + return; + } + + customizedCallback_->PermStateChangeCallback(result); +} + +void PermissionStateChangeCallback::Stop() +{} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/accesstoken/src/permission_state_change_callback.h b/interfaces/innerkits/accesstoken/src/permission_state_change_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..8ee416fad2238760fcbdb0c9a890f49d38a8bc21 --- /dev/null +++ b/interfaces/innerkits/accesstoken/src/permission_state_change_callback.h @@ -0,0 +1,42 @@ +/* + * 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_H +#define PERMISSION_STATE_CHANGE_CALLBACK_H + + +#include "permission_state_change_callback_stub.h" +#include "perm_state_change_callback_customize.h" + + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermissionStateChangeCallback : public PermissionStateChangeCallbackStub { +public: + explicit PermissionStateChangeCallback(const std::shared_ptr& scope); + ~PermissionStateChangeCallback() override; + + void PermStateChangeCallback(PermStateChangeInfo& result) override; + + void Stop(); + +private: + std::shared_ptr customizedCallback_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERMISSION_STATE_CHANGE_CALLBACK_H diff --git a/interfaces/innerkits/accesstoken/src/permission_state_change_callback_stub.cpp b/interfaces/innerkits/accesstoken/src/permission_state_change_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7e1dfae32dfee20316a698d6611a0373d908e1d --- /dev/null +++ b/interfaces/innerkits/accesstoken/src/permission_state_change_callback_stub.cpp @@ -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. + */ + +#include "permission_state_change_callback_stub.h" + +#include "access_token.h" +#include "accesstoken_log.h" +#include "permission_state_change_info_parcel.h" +#include "string_ex.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermissionStateChangeCallbackStub" +}; +} + +int32_t PermissionStateChangeCallbackStub::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 != IPermissionStateCallback::GetDescriptor()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "get unexpect descriptor: %{public}s", Str16ToStr8(descriptor).c_str()); + return RET_FAILED; + } + + int32_t msgCode = static_cast(code); + if (msgCode == IPermissionStateCallback::PERMISSION_STATE_CHANGE) { + PermStateChangeInfo result; + sptr resultSptr = data.ReadParcelable(); + if (resultSptr == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail"); + return RET_FAILED; + } + + PermStateChangeCallback(resultSptr->changeInfo); + } else { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return RET_SUCCESS; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/accesstoken/src/permission_state_change_callback_stub.h b/interfaces/innerkits/accesstoken/src/permission_state_change_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..f29bca911113c41e19e937c986681ada5cf53e6b --- /dev/null +++ b/interfaces/innerkits/accesstoken/src/permission_state_change_callback_stub.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 PERMISSION_STATE_CHANGE_CALLBACK_STUB_H +#define PERMISSION_STATE_CHANGE_CALLBACK_STUB_H + + +#include "i_permission_state_callback.h" + +#include "iremote_stub.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermissionStateChangeCallbackStub : public IRemoteStub { +public: + PermissionStateChangeCallbackStub() = default; + virtual ~PermissionStateChangeCallbackStub() = default; + + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERMISSION_STATE_CHANGE_CALLBACK_STUB_H diff --git a/interfaces/innerkits/accesstoken/test/BUILD.gn b/interfaces/innerkits/accesstoken/test/BUILD.gn index 8120ebb50ec1fffe3ebb0a2515f00a2c719c9e28..b99e33ac6ed43e6f130511ff5e1f19745c7a5cb6 100644 --- a/interfaces/innerkits/accesstoken/test/BUILD.gn +++ b/interfaces/innerkits/accesstoken/test/BUILD.gn @@ -42,6 +42,7 @@ ohos_unittest("libaccesstoken_sdk_test") { external_deps = [ "dsoftbus:softbus_client", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", ] if (token_sync_enable == true) { cflags_cc += [ "-DTOKEN_SYNC_ENABLE" ] diff --git a/interfaces/innerkits/accesstoken/test/unittest/src/accesstoken_kit_test.cpp b/interfaces/innerkits/accesstoken/test/unittest/src/accesstoken_kit_test.cpp index d0373c59c2c87dcc7e87bcdc9e3de24a014be732..4cbfd648535e31dc0d3592b65b8ddb8b3b28952c 100644 --- a/interfaces/innerkits/accesstoken/test/unittest/src/accesstoken_kit_test.cpp +++ b/interfaces/innerkits/accesstoken/test/unittest/src/accesstoken_kit_test.cpp @@ -263,16 +263,6 @@ void AccessTokenKitTest::SetUp() AccessTokenKit::DeleteToken(tokenID); (void)remove("/data/token.json"); - NodeBasicInfo deviceInfo; - int32_t res = ::GetLocalNodeDeviceInfo(TEST_PKG_NAME.c_str(), &deviceInfo); - ASSERT_EQ(res, RET_SUCCESS); - char udid[128] = {0}; // 128 is udid length - ::GetNodeKeyInfo(TEST_PKG_NAME.c_str(), deviceInfo.networkId, - NodeDeviceInfoKey::NODE_KEY_UDID, (uint8_t *)udid, 128); // 128 is udid length - - udid_.append(udid); - networkId_.append(deviceInfo.networkId); - ACCESSTOKEN_LOG_INFO(LABEL, "SetUp ok."); } @@ -281,8 +271,6 @@ void AccessTokenKitTest::TearDown() AccessTokenID tokenID = GetAccessTokenID(TEST_USER_ID, TEST_BUNDLE_NAME, 0); AccessTokenKit::DeleteToken(tokenID); SetSelfTokenID(selfTokenId_); - udid_.clear(); - networkId_.clear(); } unsigned int AccessTokenKitTest::GetAccessTokenID(int userID, std::string bundleName, int instIndex) @@ -2411,1027 +2399,356 @@ HWTEST_F(AccessTokenKitTest, GetTokenTypeFlag003, TestSize.Level1) ASSERT_EQ(RET_SUCCESS, res); } -#ifdef TOKEN_SYNC_ENABLE -/** - * @tc.name: SetRemoteHapTokenInfo001 - * @tc.desc: set remote hap token info success - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo001 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = deviceID, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; +class CbCustomizeTest : public PermStateChangeCallbackCustomize { +public: + explicit CbCustomizeTest(const PermStateChangeScope &scopeInfo) + : PermStateChangeCallbackCustomize(scopeInfo) + { + GTEST_LOG_(INFO) << "CbCustomizeTest create"; + } - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); + ~CbCustomizeTest() + {} - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); + virtual void PermStateChangeCallback(PermStateChangeInfo& result) + { + ready_ = true; + GTEST_LOG_(INFO) << "CbCustomizeTest PermStateChangeCallback"; + GTEST_LOG_(INFO) << "tokenid" << result.tokenID; + GTEST_LOG_(INFO) << "permissionName" << result.permissionName; + } - // check local map token - HapTokenInfo resultInfo; - ret = AccessTokenKit::GetHapTokenInfo(mapID, resultInfo); - ASSERT_EQ(ret, RET_SUCCESS); - ASSERT_EQ(resultInfo.apl, remoteTokenInfo.baseInfo.apl); - ASSERT_EQ(resultInfo.ver, remoteTokenInfo.baseInfo.ver); - ASSERT_EQ(resultInfo.userID, remoteTokenInfo.baseInfo.userID); - ASSERT_EQ(resultInfo.bundleName, remoteTokenInfo.baseInfo.bundleName); - ASSERT_EQ(resultInfo.instIndex, remoteTokenInfo.baseInfo.instIndex); - ASSERT_EQ(resultInfo.appID, remoteTokenInfo.baseInfo.appID); - ASSERT_EQ(resultInfo.deviceID, remoteTokenInfo.baseInfo.deviceID); - ASSERT_NE(resultInfo.tokenID, remoteTokenInfo.baseInfo.tokenID); // tokenID already is map tokenID - ASSERT_EQ(resultInfo.tokenAttr, remoteTokenInfo.baseInfo.tokenAttr); - - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_GRANTED); - - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); -} + bool ready_; +}; /** - * @tc.name: SetRemoteHapTokenInfo002 - * @tc.desc: set remote hap token info, token info is wrong + * @tc.name: RegisterPermStateChangeCallback001 + * @tc.desc: RegisterPermStateChangeCallback permList * @tc.type: FUNC - * @tc.require:AR000GK6T5 + * @tc.require:AR000GK6TD */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo002, TestSize.Level1) +HWTEST_F(AccessTokenKitTest, RegisterPermStateChangeCallback001, TestSize.Level1) { - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo002 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo rightBaseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; + PermStateChangeScope scopeInfo; + scopeInfo.permList = {"ohos.permission.CAMERA"}; + scopeInfo.tokenIDs = {}; + auto callbackPtr = std::make_shared(scopeInfo); + callbackPtr->ready_ = false; - HapTokenInfo wrongBaseInfo = rightBaseInfo; - wrongBaseInfo.apl = (ATokenAplEnum)11; // wrong apl + int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr); - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_DENIED}, .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = wrongBaseInfo, - .permStateList = permStateList + .resDeviceID = {"local"} }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_NE(ret, RET_SUCCESS); - - std::string wrongStr(10241, 'x'); - - wrongBaseInfo = rightBaseInfo; - wrongBaseInfo.appID = wrongStr; // wrong appID - remoteTokenInfo.baseInfo = wrongBaseInfo; - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_NE(ret, RET_SUCCESS); - - wrongBaseInfo = rightBaseInfo; - wrongBaseInfo.bundleName = wrongStr; // wrong bundleName - remoteTokenInfo.baseInfo = wrongBaseInfo; - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_NE(ret, RET_SUCCESS); - - wrongBaseInfo = rightBaseInfo; - wrongBaseInfo.deviceID = wrongStr; // wrong deviceID - remoteTokenInfo.baseInfo = wrongBaseInfo; - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_NE(ret, RET_SUCCESS); - - wrongBaseInfo = rightBaseInfo; - wrongBaseInfo.tokenID = 0; // wrong tokenID - remoteTokenInfo.baseInfo = wrongBaseInfo; - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_NE(ret, RET_SUCCESS); -} - -/** - * @tc.name: SetRemoteHapTokenInfo003 - * @tc.desc: set remote hap token wrong permission grant - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo003, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo003 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { + static HapPolicyParams infoManagerTestPolicyPrams = { .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {11}, // wrong flags - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA} }; - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_DENIED); - - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); -} - -/** - * @tc.name: SetRemoteHapTokenInfo004 - * @tc.desc: update remote hap token when remote exist - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo004, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo004 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; + AccessTokenIDEx tokenIdEx = {0}; + AccessTokenID tokenID; + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, infoManagerTestPolicyPrams); - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_DENIED}, // first denied - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); + tokenID = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(0, tokenID); - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; + ATokenTypeEnum ret = AccessTokenKit::GetTokenTypeFlag(tokenID); + ASSERT_EQ(ret, TOKEN_HAP); - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_DENIED, res); - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_DENIED); + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_GRANTED, res); - remoteTokenInfo.permStateList[0].grantStatus[0] = PermissionState::PERMISSION_GRANTED; // second granted - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); + ASSERT_EQ(true, callbackPtr->ready_); - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_GRANTED); + callbackPtr->ready_ = false; - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); -} + res = AccessTokenKit::RevokePermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); -/** - * @tc.name: SetRemoteHapTokenInfo005 - * @tc.desc: add remote hap token, it can not grant by GrantPermission - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo005, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo005 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; + ASSERT_EQ(true, callbackPtr->ready_); - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_DENIED}, // first denied - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); + res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr); + ASSERT_EQ(RET_SUCCESS, res); - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; + callbackPtr->ready_ = false; - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); + ASSERT_EQ(false, callbackPtr->ready_); - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_DENIED); + callbackPtr->ready_ = false; - ret = AccessTokenKit::GrantPermission(mapID, "ohos.permission.test1", PermissionFlag::PERMISSION_SYSTEM_FIXED); - ASSERT_EQ(ret, RET_SUCCESS); + res = AccessTokenKit::RevokePermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_DENIED); + ASSERT_EQ(false, callbackPtr->ready_); - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); } /** - * @tc.name: SetRemoteHapTokenInfo006 - * @tc.desc: add remote hap token, it can not revoke by RevokePermission + * @tc.name: RegisterPermStateChangeCallback002 + * @tc.desc: RegisterPermStateChangeCallback permList * @tc.type: FUNC - * @tc.require:AR000GK6T5 + * @tc.require:AR000GK6TD */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo006, TestSize.Level1) +HWTEST_F(AccessTokenKitTest, RegisterPermStateChangeCallback002, TestSize.Level1) { - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo006 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, // first grant - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); + PermStateChangeScope scopeInfo; + scopeInfo.permList = {"ohos.permission.GET_BUNDLE_INFO"}; + scopeInfo.tokenIDs = {}; + auto callbackPtr = std::make_shared(scopeInfo); + callbackPtr->ready_ = false; - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_GRANTED); + int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr); - ret = AccessTokenKit::RevokePermission(mapID, "ohos.permission.test1", PermissionFlag::PERMISSION_SYSTEM_FIXED); - ASSERT_EQ(ret, RET_SUCCESS); - - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_GRANTED); - - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); -} - -/** - * @tc.name: SetRemoteHapTokenInfo007 - * @tc.desc: add remote hap token, it can not delete by DeleteToken - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo007, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo007 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_DENIED}, // first denied + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - - ret = AccessTokenKit::DeleteToken(mapID); - ASSERT_EQ(ret, RET_FAILED); - - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); -} - -/** - * @tc.name: SetRemoteHapTokenInfo008 - * @tc.desc: add remote hap token, it can not update by UpdateHapToken - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo008, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo008 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 + .resDeviceID = {"local"} }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}, - .grantStatus = {PermissionState::PERMISSION_DENIED}, // first denied + static PermissionStateFull infoManagerTestStateB = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .resDeviceID = {"local"} }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - - HapPolicyParams policy; - - ret = AccessTokenKit::UpdateHapToken(mapID, "updateFailed", policy); - ASSERT_EQ(ret, RET_FAILED); - - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); -} - -/** - * @tc.name: SetRemoteHapTokenInfo009 - * @tc.desc: add remote hap token, it can not clear by ClearUserGrantedPermissionState - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo009, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo009 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 + static HapPolicyParams infoManagerTestPolicyPrams = { + .apl = APL_SYSTEM_BASIC, + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA, infoManagerTestStateB} }; - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, infoManagerTestPolicyPrams); - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; - // Get local map token ID - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_GRANTED, res); - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_GRANTED); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); - ret = AccessTokenKit::ClearUserGrantedPermissionState(mapID); - ASSERT_EQ(ret, RET_SUCCESS); + ASSERT_EQ(false, callbackPtr->ready_); - ret = AccessTokenKit::VerifyAccessToken(mapID, "ohos.permission.test1"); - ASSERT_EQ(ret, PermissionState::PERMISSION_GRANTED); + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); + res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr); + ASSERT_EQ(RET_SUCCESS, res); } /** - * @tc.name: SetRemoteHapTokenInfo010 - * @tc.desc: tokenID is not hap token + * @tc.name: RegisterPermStateChangeCallback003 + * @tc.desc: RegisterPermStateChangeCallback permList * @tc.type: FUNC - * @tc.require:AR000GK6T5 + * @tc.require:AR000GK6TD */ -HWTEST_F(AccessTokenKitTest, SetRemoteHapTokenInfo010, TestSize.Level1) +HWTEST_F(AccessTokenKitTest, RegisterPermStateChangeCallback003, TestSize.Level1) { - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteHapTokenInfo009 start."); - std::string deviceID = udid_; - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x28100000, - .tokenAttr = 0 - }; + PermStateChangeScope scopeInfo; + scopeInfo.permList = {}; + scopeInfo.tokenIDs = {}; + auto callbackPtr = std::make_shared(scopeInfo); + callbackPtr->ready_ = false; - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); + int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr); - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_NE(ret, RET_SUCCESS); -} - -/** - * @tc.name: DeleteRemoteDeviceToken001 - * @tc.desc: delete exist device mapping tokenId - * @tc.type: FUNC - * @tc.require:AR000GK6TA - */ -HWTEST_F(AccessTokenKitTest, DeleteRemoteDeviceToken001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "DeleteRemoteDeviceTokens001 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = deviceID, - .tokenID = 0x20100000, - .tokenAttr = 0 + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO", + .grantFlags = {1}, + .grantStatus = {PERMISSION_DENIED}, + .isGeneral = true, + .resDeviceID = {"local"} }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, + static PermissionStateFull infoManagerTestStateB = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_DENIED}, .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .resDeviceID = {"local"} }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - - HapTokenInfo info; - ret = AccessTokenKit::GetHapTokenInfo(mapID, info); - ASSERT_EQ(ret, RET_SUCCESS); - - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_EQ(ret, RET_SUCCESS); - - ret = AccessTokenKit::GetHapTokenInfo(mapID, info); - ASSERT_NE(ret, RET_SUCCESS); -} - -/** - * @tc.name: DeleteRemoteDeviceToken002 - * @tc.desc: delete exist device mapping tokenId - * @tc.type: FUNC - * @tc.require:AR000GK6TA - */ -HWTEST_F(AccessTokenKitTest, DeleteRemoteDeviceToken002, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "DeleteRemoteDeviceTokens001 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = deviceID, - .tokenID = 0x20100000, - .tokenAttr = 0 + static HapPolicyParams infoManagerTestPolicyPrams = { + .apl = APL_SYSTEM_CORE, + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA, infoManagerTestStateB} }; - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList - }; + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, infoManagerTestPolicyPrams); - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); + callbackPtr->ready_ = false; + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_DENIED, res); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_GRANTED, res); + ASSERT_EQ(true, callbackPtr->ready_); + + callbackPtr->ready_ = false; + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.GET_BUNDLE_INFO"); + ASSERT_EQ(PERMISSION_DENIED, res); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.GET_BUNDLE_INFO", 2); + ASSERT_EQ(RET_SUCCESS, res); - HapTokenInfo info; - ret = AccessTokenKit::GetHapTokenInfo(mapID, info); - ASSERT_EQ(ret, RET_SUCCESS); + ASSERT_EQ(true, callbackPtr->ready_); - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0); - ASSERT_NE(ret, RET_SUCCESS); + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); - // deviceID is wrong - std::string wrongStr(10241, 'x'); - deviceID = wrongStr; - ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_NE(ret, RET_SUCCESS); + res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr); + ASSERT_EQ(RET_SUCCESS, res); } /** - * @tc.name: DeleteRemoteDeviceToken003 - * @tc.desc: delete exist device mapping tokenId + * @tc.name: RegisterPermStateChangeCallback004 + * @tc.desc: RegisterPermStateChangeCallback permList * @tc.type: FUNC - * @tc.require:AR000GK6TA + * @tc.require:AR000GK6TD */ -HWTEST_F(AccessTokenKitTest, DeleteRemoteDeviceToken003, TestSize.Level1) +HWTEST_F(AccessTokenKitTest, RegisterPermStateChangeCallback004, TestSize.Level1) { - ACCESSTOKEN_LOG_INFO(LABEL, "DeleteRemoteDeviceToken003 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); + PermStateChangeScope scopeInfo; + scopeInfo.permList = {"ohos.permission.GET_BUNDLE_INFO", "ohos.permission.CAMERA"}; + scopeInfo.tokenIDs = {555555}; // 555555为模拟的tokenid + auto callbackPtr = std::make_shared(scopeInfo); + callbackPtr->ready_ = false; - int ret = AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - ASSERT_NE(ret, RET_SUCCESS); -} + int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr); -/** - * @tc.name: DeleteRemoteDeviceTokens001 - * @tc.desc: delete all mapping tokens of exist device - * @tc.type: FUNC - * @tc.require:AR000GK6TA - */ -HWTEST_F(AccessTokenKitTest, DeleteRemoteDeviceTokens001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "DeleteRemoteDeviceTokens001 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100001); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, + .isGeneral = true, + .resDeviceID = {"local"} }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, + static PermissionStateFull infoManagerTestStateB = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .resDeviceID = {"local"} }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - HapTokenInfoForSync remoteTokenInfo1 = remoteTokenInfo; - remoteTokenInfo1.baseInfo.tokenID = 0x20100001; - remoteTokenInfo1.baseInfo.bundleName = "com.ohos.access_token1"; - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo1); - ASSERT_EQ(ret, RET_SUCCESS); - - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - AccessTokenID mapID1 = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100001); - ASSERT_NE(mapID1, 0); - - ret = AccessTokenKit::DeleteRemoteDeviceTokens(deviceID); - ASSERT_EQ(ret, RET_SUCCESS); - - HapTokenInfo info; - ret = AccessTokenKit::GetHapTokenInfo(mapID, info); - ASSERT_NE(ret, RET_SUCCESS); - ret = AccessTokenKit::GetHapTokenInfo(mapID1, info); - ASSERT_NE(ret, RET_SUCCESS); -} - -/** - * @tc.name: DeleteRemoteDeviceTokens002 - * @tc.desc: delete all mapping tokens of NOT exist device - * @tc.type: FUNC - * @tc.require:AR000GK6TA - */ -HWTEST_F(AccessTokenKitTest, DeleteRemoteDeviceTokens002, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "DeleteRemoteDeviceTokens002 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100001); - HapTokenInfo baseInfo = { + static HapPolicyParams infoManagerTestPolicyPrams = { .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA, infoManagerTestStateB} }; - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - HapTokenInfoForSync remoteTokenInfo1 = remoteTokenInfo; - remoteTokenInfo1.baseInfo.tokenID = 0x20100001; - remoteTokenInfo1.baseInfo.bundleName = "com.ohos.access_token1"; - ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo1); - ASSERT_EQ(ret, RET_SUCCESS); - - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - AccessTokenID mapID1 = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100001); - ASSERT_NE(mapID1, 0); - - ret = AccessTokenKit::DeleteRemoteDeviceTokens("1111111"); - ASSERT_NE(ret, RET_SUCCESS); - - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100001); -} - -/** - * @tc.name: GetHapTokenInfoFromRemote001 - * @tc.desc: get normal local tokenInfo - * @tc.type: FUNC - * @tc.require:AR000GK6TA - */ -HWTEST_F(AccessTokenKitTest, GetHapTokenInfoFromRemote001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "GetHapTokenInfoFromRemote001 start."); AccessTokenIDEx tokenIdEx = {0}; - tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams); - AccessTokenID localTokenID = tokenIdEx.tokenIdExStruct.tokenID; - - HapTokenInfoForSync infoSync; - int ret = AccessTokenKit::GetHapTokenInfoFromRemote(localTokenID, infoSync); - ASSERT_EQ(ret, RET_SUCCESS); - ASSERT_EQ(infoSync.baseInfo.apl, g_infoManagerTestPolicyPrams.apl); - ASSERT_EQ(infoSync.permStateList.size(), 2); - ASSERT_EQ(infoSync.permStateList[1].grantFlags.size(), 2); + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, infoManagerTestPolicyPrams); - ASSERT_EQ(infoSync.permStateList[0].permissionName, g_infoManagerTestPolicyPrams.permStateList[0].permissionName); - ASSERT_EQ(infoSync.permStateList[0].grantFlags[0], g_infoManagerTestPolicyPrams.permStateList[0].grantFlags[0]); - ASSERT_EQ(infoSync.permStateList[0].grantStatus[0], g_infoManagerTestPolicyPrams.permStateList[0].grantStatus[0]); - ASSERT_EQ(infoSync.permStateList[0].resDeviceID[0], g_infoManagerTestPolicyPrams.permStateList[0].resDeviceID[0]); - ASSERT_EQ(infoSync.permStateList[0].isGeneral, g_infoManagerTestPolicyPrams.permStateList[0].isGeneral); + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; - ASSERT_EQ(infoSync.permStateList[1].permissionName, g_infoManagerTestPolicyPrams.permStateList[1].permissionName); - ASSERT_EQ(infoSync.permStateList[1].grantFlags[0], g_infoManagerTestPolicyPrams.permStateList[1].grantFlags[0]); - ASSERT_EQ(infoSync.permStateList[1].grantStatus[0], g_infoManagerTestPolicyPrams.permStateList[1].grantStatus[0]); - ASSERT_EQ(infoSync.permStateList[1].resDeviceID[0], g_infoManagerTestPolicyPrams.permStateList[1].resDeviceID[0]); - ASSERT_EQ(infoSync.permStateList[1].isGeneral, g_infoManagerTestPolicyPrams.permStateList[1].isGeneral); + callbackPtr->ready_ = false; + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_GRANTED, res); + res = AccessTokenKit::RevokePermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_DENIED, res); + ASSERT_EQ(false, callbackPtr->ready_); + + callbackPtr->ready_ = false; + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.GET_BUNDLE_INFO"); + ASSERT_EQ(PERMISSION_GRANTED, res); + res = AccessTokenKit::RevokePermission(tokenID, "ohos.permission.GET_BUNDLE_INFO", 2); + ASSERT_EQ(RET_SUCCESS, res); - ASSERT_EQ(infoSync.permStateList[1].grantFlags[1], g_infoManagerTestPolicyPrams.permStateList[1].grantFlags[1]); - ASSERT_EQ(infoSync.permStateList[1].grantStatus[1], g_infoManagerTestPolicyPrams.permStateList[1].grantStatus[1]); - ASSERT_EQ(infoSync.permStateList[1].resDeviceID[1], g_infoManagerTestPolicyPrams.permStateList[1].resDeviceID[1]); + ASSERT_EQ(false, callbackPtr->ready_); - ASSERT_EQ(infoSync.baseInfo.bundleName, g_infoManagerTestInfoParms.bundleName); - ASSERT_EQ(infoSync.baseInfo.userID, g_infoManagerTestInfoParms.userID); - ASSERT_EQ(infoSync.baseInfo.instIndex, g_infoManagerTestInfoParms.instIndex); - ASSERT_EQ(infoSync.baseInfo.appID, g_infoManagerTestInfoParms.appIDDesc); - ASSERT_EQ(infoSync.baseInfo.ver, 1); - ASSERT_EQ(infoSync.baseInfo.tokenID, localTokenID); - ASSERT_EQ(infoSync.baseInfo.tokenAttr, 0); + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); - AccessTokenKit::DeleteToken(localTokenID); + res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr); + ASSERT_EQ(RET_SUCCESS, res); } /** - * @tc.name: GetHapTokenInfoFromRemote002 - * @tc.desc: get remote mapping tokenInfo + * @tc.name: RegisterPermStateChangeCallback005 + * @tc.desc: RegisterPermStateChangeCallback permList * @tc.type: FUNC - * @tc.require:AR000GK6TA + * @tc.require:AR000GK6TD */ -HWTEST_F(AccessTokenKitTest, GetHapTokenInfoFromRemote002, TestSize.Level1) +HWTEST_F(AccessTokenKitTest, RegisterPermStateChangeCallback005, TestSize.Level1) { - ACCESSTOKEN_LOG_INFO(LABEL, "GetHapTokenInfoFromRemote002 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { - .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_DENIED}, + .isGeneral = true, + .resDeviceID = {"local"} }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, + static PermissionStateFull infoManagerTestStateB = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .resDeviceID = {"local"} }; - - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); - - HapTokenInfoForSync infoSync; - ret = AccessTokenKit::GetHapTokenInfoFromRemote(mapID, infoSync); - ASSERT_NE(ret, RET_SUCCESS); - - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); -} - -/** - * @tc.name: GetHapTokenInfoFromRemote003 - * @tc.desc: get wrong tokenInfo - * @tc.type: FUNC - * @tc.require:AR000GK6TA - */ -HWTEST_F(AccessTokenKitTest, GetHapTokenInfoFromRemote003, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "GetHapTokenInfoFromRemote003 start."); - HapTokenInfoForSync infoSync; - int ret = AccessTokenKit::GetHapTokenInfoFromRemote(0, infoSync); - ASSERT_NE(ret, RET_SUCCESS); -} - -/** - * @tc.name: AllocLocalTokenID001 - * @tc.desc: get already mapping tokenInfo, makesure ipc right - * @tc.type: FUNC - * @tc.require:AR000GK6T5 - */ -HWTEST_F(AccessTokenKitTest, AllocLocalTokenID001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "AllocLocalTokenID001 start."); - std::string deviceID = udid_; - AccessTokenKit::DeleteRemoteToken(deviceID, 0x20100000); - HapTokenInfo baseInfo = { + static HapPolicyParams infoManagerTestPolicyPrams = { .apl = APL_NORMAL, - .ver = 1, - .userID = 1, - .bundleName = "com.ohos.access_token", - .instIndex = 1, - .appID = "testtesttesttest", - .deviceID = udid_, - .tokenID = 0x20100000, - .tokenAttr = 0 - }; - - PermissionStateFull infoManagerTestState = { - .grantFlags = {PermissionFlag::PERMISSION_USER_SET}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.test1", - .resDeviceID = {"local"}}; - std::vector permStateList; - permStateList.emplace_back(infoManagerTestState); - - HapTokenInfoForSync remoteTokenInfo = { - .baseInfo = baseInfo, - .permStateList = permStateList + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA, infoManagerTestStateB} }; - int ret = AccessTokenKit::SetRemoteHapTokenInfo(deviceID, remoteTokenInfo); - ASSERT_EQ(ret, RET_SUCCESS); - - AccessTokenID mapID = AccessTokenKit::AllocLocalTokenID(networkId_, 0x20100000); - ASSERT_NE(mapID, 0); -} - -/** - * @tc.name: GetAllNativeTokenInfo001 - * @tc.desc: get all native token with dcaps - * @tc.type: FUNC - * @tc.require:AR000GK6T6 - */ -HWTEST_F(AccessTokenKitTest, GetAllNativeTokenInfo001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "GetAllNativeTokenInfo001 start."); + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, infoManagerTestPolicyPrams); - std::vector nativeTokenInfosRes; - int ret = AccessTokenKit::GetAllNativeTokenInfo(nativeTokenInfosRes); - ASSERT_EQ(ret, RET_SUCCESS); -} + AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID; -/** - * @tc.name: SetRemoteNativeTokenInfo001 - * @tc.desc: set already mapping tokenInfo - * @tc.type: FUNC - * @tc.require:AR000GK6T6 - */ -HWTEST_F(AccessTokenKitTest, SetRemoteNativeTokenInfo001, TestSize.Level1) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "SetRemoteNativeTokenInfo001 start."); - std::string deviceID = udid_; + PermStateChangeScope scopeInfo; + scopeInfo.permList = {"ohos.permission.GET_BUNDLE_INFO", "ohos.permission.CAMERA"}; + scopeInfo.tokenIDs = {tokenID, 0}; + auto callbackPtr = std::make_shared(scopeInfo); + callbackPtr->ready_ = false; - NativeTokenInfoForSync native1 = { - .baseInfo.apl = APL_NORMAL, - .baseInfo.ver = 1, - .baseInfo.processName = "native_test1", - .baseInfo.dcap = {"SYSDCAP", "DMSDCAP"}, - .baseInfo.tokenID = 0x28000000, - .baseInfo.tokenAttr = 0, - .baseInfo.nativeAcls = {"ohos.permission.DISTRIBUTED_DATASYNC"}, - }; + int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr); - std::vector nativeTokenInfoList; - nativeTokenInfoList.emplace_back(native1); + callbackPtr->ready_ = false; + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(PERMISSION_DENIED, res); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.CAMERA", 2); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(true, callbackPtr->ready_); - int ret = AccessTokenKit::SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoList); - ASSERT_EQ(ret, RET_SUCCESS); + callbackPtr->ready_ = false; + res = AccessTokenKit::VerifyAccessToken(tokenID, "ohos.permission.GET_BUNDLE_INFO"); + ASSERT_EQ(PERMISSION_GRANTED, res); + res = AccessTokenKit::GrantPermission(tokenID, "ohos.permission.GET_BUNDLE_INFO", 2); + ASSERT_EQ(RET_SUCCESS, res); - AccessTokenID mapID = AccessTokenKit::GetRemoteNativeTokenID(deviceID, 0x28000000); - ASSERT_NE(mapID, 0); + ASSERT_EQ(false, callbackPtr->ready_); - NativeTokenInfo resultInfo; - ret = AccessTokenKit::GetNativeTokenInfo(mapID, resultInfo); - ASSERT_EQ(ret, RET_SUCCESS); + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); - ASSERT_EQ(resultInfo.apl, native1.baseInfo.apl); - ASSERT_EQ(resultInfo.ver, native1.baseInfo.ver); - ASSERT_EQ(resultInfo.processName, native1.baseInfo.processName); - ASSERT_EQ(resultInfo.dcap.size(), 2); - ASSERT_EQ(resultInfo.dcap[0], "SYSDCAP"); - ASSERT_EQ(resultInfo.dcap[1], "DMSDCAP"); - ASSERT_EQ(resultInfo.nativeAcls.size(), 1); - ASSERT_EQ(resultInfo.nativeAcls[0], "ohos.permission.DISTRIBUTED_DATASYNC"); - ASSERT_EQ(resultInfo.tokenID, mapID); - ASSERT_EQ(resultInfo.tokenAttr, native1.baseInfo.tokenAttr); -} -#endif + res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr); + ASSERT_EQ(RET_SUCCESS, res); +} diff --git a/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp b/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp index 9df60ca006ad17595eedd13ced7889a736f18bc5..7cbe9665d122dca0f10fc9fdc657111da89ffc72 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp +++ b/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp @@ -126,7 +126,7 @@ void TokenSyncManagerClient::LoadTokenSync() GetRemoteObject()->AddDeathRecipient(ptrTokenSyncDeathRecipient); } -void TokenSyncManagerClient::FinishStartSASuccess(const sptr &remoteObject) +void TokenSyncManagerClient::FinishStartSASuccess(const sptr& remoteObject) { ACCESSTOKEN_LOG_DEBUG(LABEL, "get tokensync sa success."); @@ -156,7 +156,7 @@ void TokenSyncManagerClient::FinishStartSAFailed() tokenSyncCon_.notify_one(); } -void TokenSyncManagerClient::SetRemoteObject(const sptr &remoteObject) +void TokenSyncManagerClient::SetRemoteObject(const sptr& remoteObject) { std::unique_lock lock(remoteMutex_); remoteObject_ = remoteObject; diff --git a/interfaces/innerkits/tokensync/src/token_sync_manager_client.h b/interfaces/innerkits/tokensync/src/token_sync_manager_client.h index 6736c8bf8e9cdeea5513e2192b4b383c1d2fa366..4a58c28f2cda8633605d8b3a4449f9a25f421d57 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_manager_client.h +++ b/interfaces/innerkits/tokensync/src/token_sync_manager_client.h @@ -40,9 +40,9 @@ public: int DeleteRemoteHapTokenInfo(AccessTokenID tokenID); int UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo); void LoadTokenSync(); - void FinishStartSASuccess(const sptr &remoteObject); + void FinishStartSASuccess(const sptr& remoteObject); void FinishStartSAFailed(); - void SetRemoteObject(const sptr &remoteObject); + void SetRemoteObject(const sptr& remoteObject); sptr GetRemoteObject(); void OnRemoteDiedHandle(); diff --git a/services/accesstokenmanager/BUILD.gn b/services/accesstokenmanager/BUILD.gn index f4bf54b94c6776aeaa82f141b914ef14db0a69a9..6638788b485d127e762100156bd69a9722da3bdf 100644 --- a/services/accesstokenmanager/BUILD.gn +++ b/services/accesstokenmanager/BUILD.gn @@ -27,6 +27,7 @@ if (is_standard_system) { part_name = "access_token" include_dirs = [ + "main/cpp/include/callback", "main/cpp/include/service", "main/cpp/include/token", "main/cpp/include/permission", @@ -45,6 +46,9 @@ if (is_standard_system) { ] sources = [ + "main/cpp/src/callback/callback_manager.cpp", + "main/cpp/src/callback/perm_state_callback_death_recipient.cpp", + "main/cpp/src/callback/permission_state_change_callback_proxy.cpp", "main/cpp/src/database/data_storage.cpp", "main/cpp/src/database/data_translator.cpp", "main/cpp/src/database/sqlite_storage.cpp", diff --git a/services/accesstokenmanager/main/cpp/include/callback/callback_manager.h b/services/accesstokenmanager/main/cpp/include/callback/callback_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..8ca6b3f5c070d3a8f5469af36d78befa06336fe3 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/include/callback/callback_manager.h @@ -0,0 +1,67 @@ +/* + * 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 ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H +#define ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H + +#include +#include + +#include "access_token.h" +#include "accesstoken_log.h" +#include "i_permission_state_callback.h" +#include "permission_state_change_info.h" +#include "permission_state_change_callback_proxy.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +enum PermStateChangeType { + REVOKED = 0, + GRANTED = 1, +}; +struct CallbackRecord { + CallbackRecord() : scopePtr_(nullptr), callbackObject_(nullptr) + {} + CallbackRecord(std::shared_ptr callbackScopePtr, sptr callback) + : scopePtr_(callbackScopePtr), callbackObject_(callback) + {} + + std::shared_ptr scopePtr_; + sptr callbackObject_; +}; + +class CallbackManager { +public: + virtual ~CallbackManager(); + CallbackManager(); + static CallbackManager& GetInstance(); + + int32_t AddCallback( + const std::shared_ptr& callbackScopePtr, const sptr& callback); + int32_t RemoveCallback(const sptr& callback); + bool CalledAccordingToTokenIdLlist(const std::vector& tokenIDList, AccessTokenID tokenID); + bool CalledAccordingToPermLlist(const std::vector& permList, const std::string& permName); + void ExcuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType); + +private: + std::mutex mutex_; + std::vector callbackInfoList_; + sptr callbackDeathRecipient_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H diff --git a/services/accesstokenmanager/main/cpp/include/callback/perm_state_callback_death_recipient.h b/services/accesstokenmanager/main/cpp/include/callback/perm_state_callback_death_recipient.h new file mode 100644 index 0000000000000000000000000000000000000000..4a4af90b7e3d76aab5f589d895e6020695d5bc11 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/include/callback/perm_state_callback_death_recipient.h @@ -0,0 +1,34 @@ +/* + * 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 PERM_STATE_CALLBACK_DEATH_RECIPIENT_H +#define PERM_STATE_CALLBACK_DEATH_RECIPIENT_H + +#include "iremote_object.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermStateCallbackDeathRecipient : public IRemoteObject::DeathRecipient { +public: + PermStateCallbackDeathRecipient() = default; + virtual ~PermStateCallbackDeathRecipient() = default; + + virtual void OnRemoteDied(const wptr& remote); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERM_STATE_CALLBACK_DEATH_RECIPIENT_H diff --git a/services/accesstokenmanager/main/cpp/include/callback/permission_state_change_callback_proxy.h b/services/accesstokenmanager/main/cpp/include/callback/permission_state_change_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..0625a24fd6f32716dc158e7da38f2cfa113afd69 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/include/callback/permission_state_change_callback_proxy.h @@ -0,0 +1,40 @@ +/* + * 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_permission_state_callback.h" + +#include "iremote_proxy.h" +#include "nocopyable.h" +#include "permission_state_change_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermissionStateChangeCallbackProxy : public IRemoteProxy { +public: + explicit PermissionStateChangeCallbackProxy(const sptr& impl); + ~PermissionStateChangeCallbackProxy() override; + virtual void PermStateChangeCallback(PermStateChangeInfo& result) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERMISSION_STATE_CHANGE_CALLBACK_PROXY_H diff --git a/services/accesstokenmanager/main/cpp/include/permission/permission_manager.h b/services/accesstokenmanager/main/cpp/include/permission/permission_manager.h index aa829f33340167f3ecbd8f5b5a470b1f3a19c7b2..db527aecb1d7a5364078b06622cfe501c53f3a64 100644 --- a/services/accesstokenmanager/main/cpp/include/permission/permission_manager.h +++ b/services/accesstokenmanager/main/cpp/include/permission/permission_manager.h @@ -21,8 +21,10 @@ #include "access_token.h" #include "hap_token_info_inner.h" +#include "i_permission_state_callback.h" #include "permission_def.h" #include "permission_list_state.h" +#include "permission_state_change_info.h" #include "permission_state_full.h" #include "rwlock.h" @@ -53,6 +55,10 @@ public: void ClearUserGrantedPermissionState(AccessTokenID tokenID); void GetSelfPermissionState( std::vector permsList, PermissionListState &permState); + int32_t AddPermStateChangeCallback( + const PermStateChangeScope& scope, const sptr& callback); + int32_t RemovePermStateChangeCallback(const sptr& callback); + private: PermissionManager(); void UpdateTokenPermissionState( diff --git a/services/accesstokenmanager/main/cpp/include/permission/permission_policy_set.h b/services/accesstokenmanager/main/cpp/include/permission/permission_policy_set.h index b90de396837f43de12e6a60daa826c5f45460914..d18e301a105a24344c64240ca37a8f74f9b809f3 100644 --- a/services/accesstokenmanager/main/cpp/include/permission/permission_policy_set.h +++ b/services/accesstokenmanager/main/cpp/include/permission/permission_policy_set.h @@ -45,7 +45,7 @@ public: void GetDefPermissions(std::vector& permList); void GetPermissionStateFulls(std::vector& permList); int QueryPermissionFlag(const std::string& permissionName); - void UpdatePermissionStatus(const std::string& permissionName, bool isGranted, uint32_t flag); + bool UpdatePermissionStatus(const std::string& permissionName, bool isGranted, uint32_t flag); void ToString(std::string& info); bool IsPermissionReqValid(int32_t tokenApl, const std::string& permissionName, const std::vector& nativeAcls); diff --git a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h index a6bb3c284efaab290193c6e2723788b67143fa6d..ebc754ad1142266180ed602e445f71b3551fccec 100644 --- a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h +++ b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h @@ -62,6 +62,9 @@ public: int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfoParcel& InfoParcel) override; int UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParcel& policyParcel) override; + int32_t RegisterPermStateChangeCallback( + const PermStateChangeScopeParcel& scope, const sptr& callback) override; + int32_t UnRegisterPermStateChangeCallback(const sptr& callback) override; #ifdef TOKEN_SYNC_ENABLE int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSyncParcel& hapSyncParcel) override; diff --git a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h index 6bd9a3ec2a6e8fbf9aa3026dff4b477cd5eadf33..cbc45c2beaf56f40810e05a8f8c800dcf94682ce 100644 --- a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h +++ b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h @@ -54,6 +54,8 @@ private: void GetHapTokenIDInner(MessageParcel& data, MessageParcel& reply); void CheckNativeDCapInner(MessageParcel& data, MessageParcel& reply); void GetTokenTypeInner(MessageParcel& data, MessageParcel& reply); + void RegisterPermStateChangeCallbackInner(MessageParcel& data, MessageParcel& reply); + void UnRegisterPermStateChangeCallbackInner(MessageParcel& data, MessageParcel& reply); #ifdef TOKEN_SYNC_ENABLE void GetHapTokenInfoFromRemoteInner(MessageParcel& data, MessageParcel& reply); diff --git a/services/accesstokenmanager/main/cpp/src/callback/callback_manager.cpp b/services/accesstokenmanager/main/cpp/src/callback/callback_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cef4eed56cd50946f8f29d4649f9177816a1fd1 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/src/callback/callback_manager.cpp @@ -0,0 +1,165 @@ +/* + * 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 "callback_manager.h" + +#include +#include +#include + +#include "access_token.h" +#include "perm_state_callback_death_recipient.h" + + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "CallbackManager"}; +const time_t MAX_TIMEOUT_SEC = 30; +} + +CallbackManager& CallbackManager::GetInstance() +{ + static CallbackManager instance; + return instance; +} + +CallbackManager::CallbackManager() : callbackDeathRecipient_(sptr( + new (std::nothrow) PermStateCallbackDeathRecipient())) +{ +} + +CallbackManager::~CallbackManager() +{ +} + +int32_t CallbackManager::AddCallback( + const std::shared_ptr& callbackScopePtr, const sptr& callback) +{ + if ((callbackScopePtr == nullptr) || (callback == nullptr)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "input is nullptr"); + return RET_FAILED; + } + + callback->AddDeathRecipient(callbackDeathRecipient_); + + CallbackRecord recordInstance; + recordInstance.callbackObject_ = callback; + recordInstance.scopePtr_ = callbackScopePtr; + + std::lock_guard lock(mutex_); + callbackInfoList_.emplace_back(recordInstance); + + ACCESSTOKEN_LOG_INFO(LABEL, "recordInstance is added"); + return RET_SUCCESS; +} + +int32_t CallbackManager::RemoveCallback(const sptr& callback) +{ + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback is nullptr"); + return RET_FAILED; + } + + std::lock_guard lock(mutex_); + + for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) { + if (callback == (*it).callbackObject_) { + ACCESSTOKEN_LOG_INFO(LABEL, "find callback"); + if (callbackDeathRecipient_ != nullptr) { + callback->RemoveDeathRecipient(callbackDeathRecipient_); + } + (*it).callbackObject_ = nullptr; + callbackInfoList_.erase(it); + break; + } + } + ACCESSTOKEN_LOG_INFO(LABEL, "callbackInfoList_ %{public}u", (uint32_t)callbackInfoList_.size()); + return RET_SUCCESS; +} + +bool CallbackManager::CalledAccordingToTokenIdLlist( + const std::vector& tokenIDList, AccessTokenID tokenID) +{ + if (tokenIDList.empty()) { + return true; + } + for (const auto& id : tokenIDList) { + if (id == tokenID) { + return true; + } + } + return false; +} + +bool CallbackManager::CalledAccordingToPermLlist(const std::vector& permList, const std::string& permName) +{ + if (permList.empty()) { + return true; + } + for (const auto& perm : permList) { + if (perm == permName) { + return true; + } + } + return false; +} + +void CallbackManager::ExcuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "entry"); + + auto callbackStart = [&]() { + ACCESSTOKEN_LOG_INFO(LABEL, "callbackStart"); + std::lock_guard lock(mutex_); + for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) { + std::shared_ptr scopePtr_ = (*it).scopePtr_; + if (scopePtr_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "scopePtr_ is nullptr"); + continue; + } + if (!CalledAccordingToTokenIdLlist(scopePtr_->tokenIDs, tokenID) || + !CalledAccordingToPermLlist(scopePtr_->permList, permName)) { + ACCESSTOKEN_LOG_INFO(LABEL, + "tokenID is %{public}u, permName is %{public}s", tokenID, permName.c_str()); + continue; + } + auto callback = iface_cast((*it).callbackObject_); + if (callback != nullptr) { + ACCESSTOKEN_LOG_INFO(LABEL, "callback excute"); + PermStateChangeInfo resInfo; + resInfo.PermStateChangeType = changeType; + resInfo.permissionName = permName; + resInfo.tokenID = tokenID; + callback->PermStateChangeCallback(resInfo); + } + } + }; + + std::packaged_task callbackTask(callbackStart); + std::future fut = callbackTask.get_future(); + std::make_unique(std::move(callbackTask))->detach(); + + ACCESSTOKEN_LOG_DEBUG(LABEL, "Waiting for the callback execution complete..."); + std::future_status status = fut.wait_for(std::chrono::seconds(MAX_TIMEOUT_SEC)); + if (status == std::future_status::timeout) { + ACCESSTOKEN_LOG_WARN(LABEL, "callbackTask callback execution timeout"); + } + ACCESSTOKEN_LOG_DEBUG(LABEL, "The callback execution is complete"); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/accesstokenmanager/main/cpp/src/callback/perm_state_callback_death_recipient.cpp b/services/accesstokenmanager/main/cpp/src/callback/perm_state_callback_death_recipient.cpp new file mode 100644 index 0000000000000000000000000000000000000000..817ac2ad1ab1b2a81f5fa962d834e79bbbc5f717 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/src/callback/perm_state_callback_death_recipient.cpp @@ -0,0 +1,48 @@ +/* + * 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 "perm_state_callback_death_recipient.h" + +#include "access_token.h" +#include "callback_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermStateCallbackDeathRecipient" +}; +} + +void PermStateCallbackDeathRecipient::OnRemoteDied(const wptr& remote) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "enter"); + if (remote == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remote object is nullptr"); + return; + } + + sptr object = remote.promote(); + if (object == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "object is nullptr"); + return; + } + CallbackManager::GetInstance().RemoveCallback(object); + ACCESSTOKEN_LOG_INFO(LABEL, "end"); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/accesstokenmanager/main/cpp/src/callback/permission_state_change_callback_proxy.cpp b/services/accesstokenmanager/main/cpp/src/callback/permission_state_change_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c3ce737ee504491c2899006065633e0992fb661 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/src/callback/permission_state_change_callback_proxy.cpp @@ -0,0 +1,68 @@ +/* + * 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 "permission_state_change_callback_proxy.h" + +#include "access_token.h" +#include "accesstoken_log.h" +#include "permission_state_change_info_parcel.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermissionStateChangeCallbackProxy" +}; +} + +PermissionStateChangeCallbackProxy::PermissionStateChangeCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) { +} + +PermissionStateChangeCallbackProxy::~PermissionStateChangeCallbackProxy() +{} + +void PermissionStateChangeCallbackProxy::PermStateChangeCallback(PermStateChangeInfo& info) +{ + MessageParcel data; + data.WriteInterfaceToken(IPermissionStateCallback::GetDescriptor()); + + PermissionStateChangeInfoParcel resultParcel; + resultParcel.changeInfo = info; + if (!data.WriteParcelable(&resultParcel)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable(result)"); + 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(IPermissionStateCallback::PERMISSION_STATE_CHANGE), 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/services/accesstokenmanager/main/cpp/src/permission/permission_manager.cpp b/services/accesstokenmanager/main/cpp/src/permission/permission_manager.cpp index 595f386a0ba0ba6999d0465b681d4acb4c140184..5cfb182608fe57736c967854081c7bc168e28682 100644 --- a/services/accesstokenmanager/main/cpp/src/permission/permission_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/permission/permission_manager.cpp @@ -18,6 +18,7 @@ #include "accesstoken_id_manager.h" #include "accesstoken_info_manager.h" #include "accesstoken_log.h" +#include "callback_manager.h" #ifdef SUPPORT_SANDBOX_APP #include "dlp_permission_set_manager.h" #endif @@ -333,7 +334,12 @@ void PermissionManager::UpdateTokenPermissionState( } } #endif - permPolicySet->UpdatePermissionStatus(permissionName, isGranted, static_cast(flag)); + bool isUpdated = permPolicySet->UpdatePermissionStatus(permissionName, isGranted, static_cast(flag)); + if (isUpdated) { + ACCESSTOKEN_LOG_INFO(LABEL, "isUpdated"); + int32_t changeType = isGranted ? GRANTED : REVOKED; + CallbackManager::GetInstance().ExcuteCallbackAsync(tokenID, permissionName, changeType); + } #ifdef TOKEN_SYNC_ENABLE TokenModifyNotifier::GetInstance().NotifyTokenModify(tokenID); @@ -382,6 +388,23 @@ void PermissionManager::RevokePermission(AccessTokenID tokenID, const std::strin UpdateTokenPermissionState(tokenID, permissionName, false, flag); } +int32_t PermissionManager::AddPermStateChangeCallback( + const PermStateChangeScope& scope, const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called"); + auto callbackScopePtr = std::make_shared(scope); + if (callbackScopePtr == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callbackScopePtr is nullptr"); + } + + return CallbackManager::GetInstance().AddCallback(callbackScopePtr, callback); +} + +int32_t PermissionManager::RemovePermStateChangeCallback(const sptr& callback) +{ + return CallbackManager::GetInstance().RemoveCallback(callback); +} + void PermissionManager::ClearUserGrantedPermissionState(AccessTokenID tokenID) { ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, tokenID: %{public}u", __func__, tokenID); diff --git a/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp b/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp index f2144a11ae37cc6a7081dd383236b63a95b1ab5b..c0b766dde6fb739ec3b18c1f2c563f8138502c8e 100644 --- a/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp +++ b/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp @@ -180,21 +180,29 @@ int PermissionPolicySet::QueryPermissionFlag(const std::string& permissionName) return PERMISSION_DEFAULT_FLAG; } -void PermissionPolicySet::UpdatePermissionStatus(const std::string& permissionName, bool isGranted, uint32_t flag) +bool PermissionPolicySet::UpdatePermissionStatus(const std::string& permissionName, bool isGranted, uint32_t flag) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "permissionName %{public}s.", permissionName.c_str()); + + bool ret = false; Utils::UniqueWriteGuard infoGuard(this->permPolicySetLock_); for (auto& perm : permStateList_) { if (perm.permissionName == permissionName) { if (perm.isGeneral) { + int32_t oldStatus = perm.grantStatus[0]; perm.grantStatus[0] = isGranted ? PERMISSION_GRANTED : PERMISSION_DENIED; uint32_t currFlag = static_cast(perm.grantFlags[0]); uint32_t newFlag = flag | (currFlag & PERMISSION_GRANTED_BY_POLICY); perm.grantFlags[0] = static_cast(newFlag); + ret = (oldStatus == perm.grantStatus[0]) ? false : true; } else { - return; + ACCESSTOKEN_LOG_WARN(LABEL, "perm isGeneral is false."); } + break; } } + + return ret; } void PermissionPolicySet::ResetUserGrantPermissionStatus(void) diff --git a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp index 9229bd58fdb2b746cd35189c88ee808de7903af5..79027dcccb49c3115e157227ae0ab47a23a89e12 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp @@ -206,6 +206,19 @@ int AccessTokenManagerService::ClearUserGrantedPermissionState(AccessTokenID tok return RET_SUCCESS; } +int32_t AccessTokenManagerService::RegisterPermStateChangeCallback( + const PermStateChangeScopeParcel& scope, const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "Entry"); + return PermissionManager::GetInstance().AddPermStateChangeCallback(scope.scope, callback); +} + +int32_t AccessTokenManagerService::UnRegisterPermStateChangeCallback(const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "Entry"); + return PermissionManager::GetInstance().RemovePermStateChangeCallback(callback); +} + AccessTokenIDEx AccessTokenManagerService::AllocHapToken(const HapInfoParcel& info, const HapPolicyParcel& policy) { ACCESSTOKEN_LOG_INFO(LABEL, "called!"); diff --git a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp index 8d621da61b8ad6fab150eb7a9707739dcad0c39c..069b0eebb0821c99d84561dd94e9513b83cb89a5 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp @@ -320,6 +320,35 @@ void AccessTokenManagerStub::GetNativeTokenInfoInner(MessageParcel& data, Messag reply.WriteInt32(result); } +void AccessTokenManagerStub::RegisterPermStateChangeCallbackInner(MessageParcel& data, MessageParcel& reply) +{ + sptr scopeParcel = data.ReadParcelable(); + if (scopeParcel == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read scopeParcel fail"); + reply.WriteInt32(RET_FAILED); + return; + } + sptr callback = data.ReadRemoteObject(); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read callback fail"); + reply.WriteInt32(RET_FAILED); + return; + } + int32_t result = this->RegisterPermStateChangeCallback(*scopeParcel, callback); + reply.WriteInt32(result); +} +void AccessTokenManagerStub::UnRegisterPermStateChangeCallbackInner(MessageParcel& data, MessageParcel& reply) +{ + sptr callback = data.ReadRemoteObject(); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read callback fail"); + reply.WriteInt32(RET_FAILED); + return; + } + int32_t result = this->UnRegisterPermStateChangeCallback(callback); + reply.WriteInt32(result); +} + #ifdef TOKEN_SYNC_ENABLE void AccessTokenManagerStub::GetHapTokenInfoFromRemoteInner(MessageParcel& data, MessageParcel& reply) { @@ -528,6 +557,11 @@ AccessTokenManagerStub::AccessTokenManagerStub() &AccessTokenManagerStub::DumpTokenInfoInner; requestFuncMap_[static_cast(IAccessTokenManager::InterfaceCode::GET_PERMISSION_OPER_STATE)] = &AccessTokenManagerStub::GetSelfPermissionsStateInner; + + requestFuncMap_[static_cast(IAccessTokenManager::InterfaceCode::REGISTER_PERM_STATE_CHANGE_CALLBACK)] = + &AccessTokenManagerStub::RegisterPermStateChangeCallbackInner; + requestFuncMap_[static_cast(IAccessTokenManager::InterfaceCode::UNREGISTER_PERM_STATE_CHANGE_CALLBACK)] = + &AccessTokenManagerStub::UnRegisterPermStateChangeCallbackInner; } AccessTokenManagerStub::~AccessTokenManagerStub() diff --git a/services/tokensyncmanager/include/remote/remote_command_executor.h b/services/tokensyncmanager/include/remote/remote_command_executor.h index f8982a9b53072d7482ed137bf86519652ecfa060..f0cf55e9a0c7279b6aa1c37ea485c4a575bc63a0 100644 --- a/services/tokensyncmanager/include/remote/remote_command_executor.h +++ b/services/tokensyncmanager/include/remote/remote_command_executor.h @@ -32,11 +32,11 @@ public: RemoteCommandExecutor(const std::string &targetNodeId); virtual ~RemoteCommandExecutor(); - const std::shared_ptr &GetChannel() const + const std::shared_ptr& GetChannel() const { return ptrChannel_; } - void SetChannel(const std::shared_ptr &ptrChannel) + void SetChannel(const std::shared_ptr& ptrChannel) { ptrChannel_ = ptrChannel; } @@ -61,7 +61,7 @@ public: * @since 1.0 * @version 1.0 */ - int ProcessOneCommand(const std::shared_ptr &ptrCommand); + int ProcessOneCommand(const std::shared_ptr& ptrCommand); /** * @brief Add one command into the buffer @@ -72,7 +72,7 @@ public: * @since 1.0 * @version 1.0 */ - int AddCommand(const std::shared_ptr &ptrCommand); + int AddCommand(const std::shared_ptr& ptrCommand); /** * @brief Process all the command in the buffer @@ -111,7 +111,7 @@ private: * @since 1.0 * @version 1.0 */ - int ExecuteRemoteCommand(const std::shared_ptr &ptrCommand, bool isRemote); + int ExecuteRemoteCommand(const std::shared_ptr& ptrCommand, bool isRemote); /** * @brief create a rpc channel if not exist. @@ -134,7 +134,7 @@ private: * @since 1.0 * @version 1.0 */ - int ClientProcessResult(const std::shared_ptr &ptrCommand); + int ClientProcessResult(const std::shared_ptr& ptrCommand); private: // target device node id(udid) diff --git a/services/tokensyncmanager/include/remote/remote_command_manager.h b/services/tokensyncmanager/include/remote/remote_command_manager.h index c1249a76cb074f7fee6cd35d1ee254ea73272c62..b94ffb870b9990cd2bb5c8c83d9744c405579869 100644 --- a/services/tokensyncmanager/include/remote/remote_command_manager.h +++ b/services/tokensyncmanager/include/remote/remote_command_manager.h @@ -62,7 +62,7 @@ public: * @since 1.0 * @version 1.0 */ - int ExecuteCommand(const std::string &udid, const std::shared_ptr &command); + int ExecuteCommand(const std::string &udid, const std::shared_ptr& command); /** * @brief Add a command to buffer. @@ -74,7 +74,7 @@ public: * @since 1.0 * @version 1.0 */ - int AddCommand(const std::string &udid, const std::shared_ptr &command); + int AddCommand(const std::string &udid, const std::shared_ptr& command); /** * @brief Execute all buffered commands for given device. diff --git a/services/tokensyncmanager/include/service/token_sync_event_handler.h b/services/tokensyncmanager/include/service/token_sync_event_handler.h index a86ca94eae2a830d9c0e91a25e30c1eef376b571..db9ceaabdedcd3e1850abbaa86dd167b82c0abce 100644 --- a/services/tokensyncmanager/include/service/token_sync_event_handler.h +++ b/services/tokensyncmanager/include/service/token_sync_event_handler.h @@ -26,7 +26,7 @@ namespace Security { namespace AccessToken { class TokenSyncEventHandler : public AppExecFwk::EventHandler { public: - explicit TokenSyncEventHandler(const std::shared_ptr &runner); + explicit TokenSyncEventHandler(const std::shared_ptr& runner); virtual ~TokenSyncEventHandler(); bool ProxyPostTask(const Callback &callback, int64_t delayTime); diff --git a/services/tokensyncmanager/src/remote/remote_command_executor.cpp b/services/tokensyncmanager/src/remote/remote_command_executor.cpp index 7f453532c5b0a1430cec2d3f5b079f92ac1f5092..050a358739077d9c4aca98d403672ecfb4234fa9 100644 --- a/services/tokensyncmanager/src/remote/remote_command_executor.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_executor.cpp @@ -56,7 +56,7 @@ const std::shared_ptr RemoteCommandExecutor::CreateChannel(const std /* * called by RemoteCommandExecutor, RemoteCommandManager */ -int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr &ptrCommand) +int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr& ptrCommand) { if (ptrCommand == nullptr) { ACCESSTOKEN_LOG_WARN( @@ -103,7 +103,7 @@ int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr &ptrCommand) +int RemoteCommandExecutor::AddCommand(const std::shared_ptr& ptrCommand) { if (ptrCommand == nullptr) { ACCESSTOKEN_LOG_DEBUG(LABEL, "targetNodeId %{public}s, attempt to add an empty command.", @@ -231,7 +231,7 @@ void RemoteCommandExecutor::ProcessBufferedCommandsWithThread() } int RemoteCommandExecutor::ExecuteRemoteCommand( - const std::shared_ptr &ptrCommand, const bool isRemote) + const std::shared_ptr& ptrCommand, const bool isRemote) { std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; ACCESSTOKEN_LOG_INFO(LABEL, @@ -297,7 +297,7 @@ void RemoteCommandExecutor::CreateChannelIfNeeded() ptrChannel_ = CreateChannel(targetNodeId_); } -int RemoteCommandExecutor::ClientProcessResult(const std::shared_ptr &ptrCommand) +int RemoteCommandExecutor::ClientProcessResult(const std::shared_ptr& ptrCommand) { std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; if (ptrCommand->remoteProtocol_.statusCode == Constant::STATUS_CODE_BEFORE_RPC) { diff --git a/services/tokensyncmanager/src/remote/remote_command_manager.cpp b/services/tokensyncmanager/src/remote/remote_command_manager.cpp index 0c222d3485722097b0a3485a9f881adccbc384fa..c9d41f27f3707ec4d9bbe2541bab3015838bdfa0 100644 --- a/services/tokensyncmanager/src/remote/remote_command_manager.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_manager.cpp @@ -50,7 +50,7 @@ void RemoteCommandManager::Init() ACCESSTOKEN_LOG_DEBUG(LABEL, "Init()"); } -int RemoteCommandManager::AddCommand(const std::string &udid, const std::shared_ptr &command) +int RemoteCommandManager::AddCommand(const std::string &udid, const std::shared_ptr& command) { if (udid.empty() || command == nullptr) { ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid, or null command"); @@ -76,7 +76,7 @@ void RemoteCommandManager::RemoveCommand(const std::string &udid) executors_.erase(udid); } -int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr &command) +int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr& command) { if (udid.empty() || command == nullptr) { ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", diff --git a/services/tokensyncmanager/src/service/token_sync_event_handler.cpp b/services/tokensyncmanager/src/service/token_sync_event_handler.cpp index 5aa524193ab10de50c494357342c67030ef29bb5..e98947dddf4fd5ad052cef3aa71430c7b0673ae6 100644 --- a/services/tokensyncmanager/src/service/token_sync_event_handler.cpp +++ b/services/tokensyncmanager/src/service/token_sync_event_handler.cpp @@ -25,7 +25,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncEventHandler"}; } TokenSyncEventHandler::TokenSyncEventHandler( - const std::shared_ptr &runner) + const std::shared_ptr& runner) : AppExecFwk::EventHandler(runner) { ACCESSTOKEN_LOG_INFO(LABEL, "enter");