From 18d7d27128923fdf4c6fcefa344e2d44acfd480f Mon Sep 17 00:00:00 2001 From: lsq Date: Sat, 23 Jul 2022 16:31:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9D=83=E9=99=90=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lsq Change-Id: I3e59f4b180ac3a6271465f1fd57419102ce390fa Signed-off-by: lsq --- frameworks/privacy/BUILD.gn | 1 + .../include/i_perm_active_status_callback.h | 42 ++++ .../privacy/include/i_privacy_manager.h | 8 +- .../include/perm_active_response_parcel.h | 40 ++++ .../src/perm_active_response_parcel.cpp | 50 +++++ interfaces/innerkits/privacy/BUILD.gn | 3 + .../include/active_change_response_info.h | 47 ++++ .../perm_active_status_change_callback.h | 45 ++++ .../perm_active_status_change_callback_stub.h | 38 ++++ ...perm_active_status_change_customized_cbk.h | 45 ++++ .../innerkits/privacy/include/privacy_kit.h | 3 + .../perm_active_status_change_callback.cpp | 49 +++++ ...erm_active_status_change_callback_stub.cpp | 58 +++++ ...rm_active_status_change_customized_cbk.cpp | 37 ++++ .../innerkits/privacy/src/privacy_kit.cpp | 12 ++ .../privacy/src/privacy_manager_client.cpp | 78 +++++++ .../privacy/src/privacy_manager_client.h | 11 + .../privacy/src/privacy_manager_proxy.cpp | 71 ++++++ .../privacy/src/privacy_manager_proxy.h | 2 + .../test/unittest/src/privacy_kit_test.cpp | 202 +++++++++++++++++- services/privacymanager/BUILD.gn | 4 + ...m_active_status_callback_death_recipient.h | 35 +++ ...erm_active_status_change_callback_manage.h | 63 ++++++ ...perm_active_status_change_callback_proxy.h | 40 ++++ .../record/permission_record_manager.h | 7 +- .../include/service/privacy_manager_service.h | 3 + .../include/service/privacy_manager_stub.h | 3 +- ...active_status_callback_death_recipient.cpp | 48 +++++ ...m_active_status_change_callback_manage.cpp | 149 +++++++++++++ ...rm_active_status_change_callback_proxy.cpp | 67 ++++++ .../src/record/permission_record_manager.cpp | 77 +++++++ .../src/service/privacy_manager_service.cpp | 15 +- .../src/service/privacy_manager_stub.cpp | 43 ++++ 33 files changed, 1389 insertions(+), 7 deletions(-) create mode 100644 frameworks/privacy/include/i_perm_active_status_callback.h create mode 100644 frameworks/privacy/include/perm_active_response_parcel.h create mode 100644 frameworks/privacy/src/perm_active_response_parcel.cpp create mode 100644 interfaces/innerkits/privacy/include/active_change_response_info.h create mode 100644 interfaces/innerkits/privacy/include/perm_active_status_change_callback.h create mode 100644 interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h create mode 100644 interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h create mode 100644 interfaces/innerkits/privacy/src/perm_active_status_change_callback.cpp create mode 100644 interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp create mode 100644 interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.cpp create mode 100644 services/privacymanager/include/active/perm_active_status_callback_death_recipient.h create mode 100644 services/privacymanager/include/active/perm_active_status_change_callback_manage.h create mode 100644 services/privacymanager/include/active/perm_active_status_change_callback_proxy.h create mode 100644 services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp create mode 100644 services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp create mode 100644 services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp diff --git a/frameworks/privacy/BUILD.gn b/frameworks/privacy/BUILD.gn index e0106a79d..8b434daf1 100644 --- a/frameworks/privacy/BUILD.gn +++ b/frameworks/privacy/BUILD.gn @@ -30,6 +30,7 @@ ohos_shared_library("privacy_communication_adapter_cxx") { "src/permission_used_record_parcel.cpp", "src/permission_used_request_parcel.cpp", "src/permission_used_result_parcel.cpp", + "src/perm_active_response_parcel.cpp", "src/used_record_detail_parcel.cpp", ] diff --git a/frameworks/privacy/include/i_perm_active_status_callback.h b/frameworks/privacy/include/i_perm_active_status_callback.h new file mode 100644 index 000000000..8d164cad0 --- /dev/null +++ b/frameworks/privacy/include/i_perm_active_status_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 I_PERMI_ACTIVE_STATUS_CALLBACK_H +#define I_PERMI_ACTIVE_STATUS_CALLBACK_H + +#include + +#include "access_token.h" +#include "active_change_response_info.h" +#include "iremote_broker.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class IPermActiveStatusChangeCbk : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.accesstoken.IPermActiveStatusChangeCbk"); + + virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result) = 0; + + enum { + PERM_ACTIVE_STATUS_CHANGE = 0, + }; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // PERMI_ACTIVE_STATUS_CALLBACK_H \ No newline at end of file diff --git a/frameworks/privacy/include/i_privacy_manager.h b/frameworks/privacy/include/i_privacy_manager.h index 8ee5f3fba..137f58864 100644 --- a/frameworks/privacy/include/i_privacy_manager.h +++ b/frameworks/privacy/include/i_privacy_manager.h @@ -45,7 +45,9 @@ public: virtual int32_t GetPermissionUsedRecords( const PermissionUsedRequestParcel& request, const sptr& callback) = 0; virtual std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName) = 0; - + virtual int32_t RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback) = 0; + virtual int32_t UnRegisterPermActiveStatusCallback(const sptr& callback) = 0; enum class InterfaceCode { ADD_PERMISSION_USED_RECORD = 0xf001, START_USING_PERMISSION = 0xf002, @@ -53,7 +55,9 @@ public: DELETE_PERMISSION_USED_RECORDS = 0xf004, GET_PERMISSION_USED_RECORDS = 0xf005, GET_PERMISSION_USED_RECORDS_ASYNC = 0xf006, - DUMP_RECORD_INFO = 0xf007 + DUMP_RECORD_INFO = 0xf007, + REGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK = 0xf008, + UNREGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK = 0xf009, }; }; } // namespace AccessToken diff --git a/frameworks/privacy/include/perm_active_response_parcel.h b/frameworks/privacy/include/perm_active_response_parcel.h new file mode 100644 index 000000000..b870dd23d --- /dev/null +++ b/frameworks/privacy/include/perm_active_response_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_USED_RECORD_PRACEL_H +#define PERMISSION_USED_RECORD_PRACEL_H + +#include "parcel.h" +#include "active_change_response_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +struct ActiveChangeResponseParcel final : public Parcelable { + ActiveChangeResponseParcel() = default; + + ~ActiveChangeResponseParcel() override = default; + + bool Marshalling(Parcel& out) const override; + + static ActiveChangeResponseParcel* Unmarshalling(Parcel& in); + + ActiveChangeResponse changeResponse; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // PERMISSION_USED_RECORD_PRACEL_H diff --git a/frameworks/privacy/src/perm_active_response_parcel.cpp b/frameworks/privacy/src/perm_active_response_parcel.cpp new file mode 100644 index 000000000..9a475dd85 --- /dev/null +++ b/frameworks/privacy/src/perm_active_response_parcel.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 "perm_active_response_parcel.h" +#include "parcel_utils.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +bool ActiveChangeResponseParcel::Marshalling(Parcel& out) const +{ + RETURN_IF_FALSE(out.WriteUint32(this->changeResponse.tokenID)); + RETURN_IF_FALSE(out.WriteString(this->changeResponse.permissionName)); + RETURN_IF_FALSE(out.WriteString(this->changeResponse.deviceId)); + RETURN_IF_FALSE(out.WriteInt32(this->changeResponse.type)); + return true; +} + +ActiveChangeResponseParcel* ActiveChangeResponseParcel::Unmarshalling(Parcel& in) +{ + auto* activeChangeResponseParcel = new (std::nothrow) ActiveChangeResponseParcel(); + if (activeChangeResponseParcel == nullptr) { + return nullptr; + } + + RELEASE_IF_FALSE(in.ReadUint32(activeChangeResponseParcel->changeResponse.tokenID), activeChangeResponseParcel); + RELEASE_IF_FALSE(in.ReadString(activeChangeResponseParcel->changeResponse.permissionName), + activeChangeResponseParcel); + RELEASE_IF_FALSE(in.ReadString(activeChangeResponseParcel->changeResponse.deviceId), activeChangeResponseParcel); + + int32_t type; + RELEASE_IF_FALSE(in.ReadInt32(type), activeChangeResponseParcel); + activeChangeResponseParcel->changeResponse.type = static_cast(type); + return activeChangeResponseParcel; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/privacy/BUILD.gn b/interfaces/innerkits/privacy/BUILD.gn index 11e4dac19..f0b391208 100644 --- a/interfaces/innerkits/privacy/BUILD.gn +++ b/interfaces/innerkits/privacy/BUILD.gn @@ -38,6 +38,9 @@ if (is_standard_system) { ] sources = [ + "src/perm_active_status_change_callback.cpp", + "src/perm_active_status_change_callback_stub.cpp", + "src/perm_active_status_change_customized_cbk.cpp", "src/privacy_death_recipient.cpp", "src/privacy_kit.cpp", "src/privacy_manager_client.cpp", diff --git a/interfaces/innerkits/privacy/include/active_change_response_info.h b/interfaces/innerkits/privacy/include/active_change_response_info.h new file mode 100644 index 000000000..71f734462 --- /dev/null +++ b/interfaces/innerkits/privacy/include/active_change_response_info.h @@ -0,0 +1,47 @@ +/* + * 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 ACTIVE_CHANGE_RESPONSE_INFO_H +#define ACTIVE_CHANGE_RESPONSE_INFO_H + +#include +#include + +#include "access_token.h" + +namespace OHOS { +namespace Security { +namespace AccessToken{ +#define TOKENID_LIST_SIZE_MAX 1024 +#define PERM_LIST_SIZE_MAX 1024 + +enum ActiveChangeType { + PERM_INACTIVE = 0, + PERM_ACTIVE_IN_FOREGROUND = 1, + PERM_ACTIVE_IN_BACKGRONGD = 2, +}; + +struct ActiveChangeResponse { + AccessTokenID tokenID; + std::string permissionName; + std::string deviceId; + ActiveChangeType type; +}; + +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // ACTIVE_CHANGE_RESPONSE_INFO_H \ No newline at end of file diff --git a/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h b/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h new file mode 100644 index 000000000..02ddc121f --- /dev/null +++ b/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h @@ -0,0 +1,45 @@ +/* + * 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_ACTIVE_STATUS_CHANGE_CALLBACK_H +#define PERM_ACTIVE_STATUS_CHANGE_CALLBACK_H + +#include +#include + +#include "active_change_response_info.h" +#include "perm_active_status_change_callback_stub.h" +#include "perm_active_status_change_customized_cbk.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermActiveStatusChangeCallback : public PermActiveStatusChangeCallbackStub { +public: + explicit PermActiveStatusChangeCallback(const std::shared_ptr &subscriber); + ~PermActiveStatusChangeCallback() override; + + void ActiveStatusChangeCallback(ActiveChangeResponse& result) override; + + void Stop(); + +private: + std::shared_ptr customizedCallback_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // PERM_ACTIVE_STATUS_CHANGE_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h b/interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h new file mode 100644 index 000000000..71838bd3a --- /dev/null +++ b/interfaces/innerkits/privacy/include/perm_active_status_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 PERM_ACTIVE_STATUS_CHANGE_CALLBACK_STUB_H +#define PERM_ACTIVE_STATUS_CHANGE_CALLBACK_STUB_H + + +#include "i_perm_active_status_callback.h" + +#include "iremote_stub.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermActiveStatusChangeCallbackStub : public IRemoteStub { +public: + PermActiveStatusChangeCallbackStub() = default; + virtual ~PermActiveStatusChangeCallbackStub() = default; + + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERM_ACTIVE_STATUS_CHANGE_CALLBACK_STUB_H diff --git a/interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h b/interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h new file mode 100644 index 000000000..e5a765476 --- /dev/null +++ b/interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h @@ -0,0 +1,45 @@ +/* + * 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_ACTIVE_STATUS_CHANGE_CUSTOMIZED_CBK_H +#define PERM_ACTIVE_STATUS_CHANGE_CUSTOMIZED_CBK_H + +#include +#include + +#include "access_token.h" +#include "active_change_response_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermActiveStatusCustomizedCbk { +public: + PermActiveStatusCustomizedCbk(); + explicit PermActiveStatusCustomizedCbk(const std::vector& permList); + virtual ~PermActiveStatusCustomizedCbk(); + + virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result) = 0; + + void GetPermList(std::vector& permList) const; + +private: + std::vector permList_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // PERM_ACTIVE_STATUS_CHANGE_CUSTOMIZED_CBK_H \ No newline at end of file diff --git a/interfaces/innerkits/privacy/include/privacy_kit.h b/interfaces/innerkits/privacy/include/privacy_kit.h index b3fb9a4d1..3c7e34cc7 100644 --- a/interfaces/innerkits/privacy/include/privacy_kit.h +++ b/interfaces/innerkits/privacy/include/privacy_kit.h @@ -22,6 +22,7 @@ #include "on_permission_used_record_callback.h" #include "permission_used_request.h" #include "permission_used_result.h" +#include "perm_active_status_change_customized_cbk.h" namespace OHOS { namespace Security { @@ -37,6 +38,8 @@ public: static int32_t GetPermissionUsedRecords( const PermissionUsedRequest& request, const sptr& callback); static std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName); + static int32_t RegisterPermActiveStatusCallback(const std::shared_ptr& callback); + static int32_t UnRegisterPermActiveStatusCallback(const std::shared_ptr& callback); }; } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/privacy/src/perm_active_status_change_callback.cpp b/interfaces/innerkits/privacy/src/perm_active_status_change_callback.cpp new file mode 100644 index 000000000..633a2e37a --- /dev/null +++ b/interfaces/innerkits/privacy/src/perm_active_status_change_callback.cpp @@ -0,0 +1,49 @@ +/* + * 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_active_status_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, "PermissionActiveStatusChangeCallback" +}; +PermActiveStatusChangeCallback::PermActiveStatusChangeCallback( + const std::shared_ptr &customizedCallback) + : customizedCallback_(customizedCallback) +{} + +PermActiveStatusChangeCallback::~PermActiveStatusChangeCallback() +{} + +void PermActiveStatusChangeCallback::ActiveStatusChangeCallback(ActiveChangeResponse& result) +{ + if (customizedCallback_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "customizedCallback_ is nullptr"); + return; + } + + customizedCallback_->ActiveStatusChangeCallback(result); +} + +void PermActiveStatusChangeCallback::Stop() +{} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp b/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp new file mode 100644 index 000000000..3fcfcb3ec --- /dev/null +++ b/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "perm_active_status_change_callback_stub.h" + +#include "accesstoken_log.h" +#include "perm_active_response_parcel.h" +#include "string_ex.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermActiveStatusChangeCallbackStub" +}; +} + +int32_t PermActiveStatusChangeCallbackStub::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 != IPermActiveStatusChangeCbk::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 == IPermActiveStatusChangeCbk::PERM_ACTIVE_STATUS_CHANGE) { + sptr resultSptr = data.ReadParcelable(); + if (resultSptr == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail"); + return RET_FAILED; + } + + ActiveStatusChangeCallback(resultSptr->changeResponse); + } else { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return RET_SUCCESS; +} + +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.cpp b/interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.cpp new file mode 100644 index 000000000..a87e15486 --- /dev/null +++ b/interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.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_active_status_change_customized_cbk.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +PermActiveStatusCustomizedCbk::PermActiveStatusCustomizedCbk() +{} + +PermActiveStatusCustomizedCbk::PermActiveStatusCustomizedCbk( + const std::vector& permList) : permList_(permList) // 此处是否有问题 +{} + +PermActiveStatusCustomizedCbk::~PermActiveStatusCustomizedCbk() +{} + +void PermActiveStatusCustomizedCbk::GetPermList(std::vector& permList) const +{ + permList = permList_; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/privacy/src/privacy_kit.cpp b/interfaces/innerkits/privacy/src/privacy_kit.cpp index 0b9557bdd..2e91a684d 100644 --- a/interfaces/innerkits/privacy/src/privacy_kit.cpp +++ b/interfaces/innerkits/privacy/src/privacy_kit.cpp @@ -78,6 +78,18 @@ std::string PrivacyKit::DumpRecordInfo(const std::string& bundleName, const std: bundleName.c_str(), permissionName.c_str()); return PrivacyManagerClient::GetInstance().DumpRecordInfo(bundleName, permissionName); } + +int32_t PrivacyKit::RegisterPermActiveStatusCallback(const std::shared_ptr& callback) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry"); + return PrivacyManagerClient::GetInstance().RegisterPermActiveStatusCallback(callback); +} + +int32_t PrivacyKit::UnRegisterPermActiveStatusCallback(const std::shared_ptr& callback) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry"); + return PrivacyManagerClient::GetInstance().UnRegisterPermActiveStatusCallback(callback); +} } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp index 495f5b42a..f46fb627a 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp +++ b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp @@ -14,6 +14,7 @@ */ #include "privacy_manager_client.h" +#include #include "accesstoken_log.h" #include "data_validator.h" #include "iservice_registry.h" @@ -142,6 +143,83 @@ std::string PrivacyManagerClient::DumpRecordInfo(const std::string& bundleName, return proxy->DumpRecordInfo(bundleName, permissionName); } +int32_t PrivacyManagerClient::CreateActiveStatusChangeCbk( + const std::shared_ptr& callback, sptr& callbackObject) +{ + std::lock_guard lock(activeCbkMutex_); + + auto goalCallback = activeCbkMap_.find(callback); + if (goalCallback != activeCbkMap_.end()) { + callbackObject = goalCallback->second->AsObject(); + ACCESSTOKEN_LOG_ERROR(LABEL, "activeCbkMap_ already has such callback"); + return ERROR; + } else { + if (activeCbkMap_.size() == 200) { + ACCESSTOKEN_LOG_ERROR(LABEL, "the maximum number of subscribers has been reached"); + return ERROR; + } + sptr callbackWraped = + new (std::nothrow) PermActiveStatusChangeCallback(callback); + if (!callbackWraped) { + ACCESSTOKEN_LOG_ERROR(LABEL, "memory allocation for callbackWraped failed!"); + return ERROR; + } + ACCESSTOKEN_LOG_INFO(LABEL, "callbackObject added"); + callbackObject = callbackWraped->AsObject(); + activeCbkMap_[callback] = callbackWraped; + } + return RET_SUCCESS; +} + +int32_t PrivacyManagerClient::RegisterPermActiveStatusCallback( + const std::shared_ptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called!"); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "customizedCb is nullptr"); + return ERROR; + } + + sptr callbackObject = nullptr; + int32_t result = CreateActiveStatusChangeCbk(callback, callbackObject); + if (result != RET_SUCCESS) { + return result; + } + + auto proxy = GetProxy(); + if (proxy == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null"); + return ERROR; + } + std::vector permList; + callback->GetPermList(permList); + + return proxy->RegisterPermActiveStatusCallback(permList, callbackObject); +} + +int32_t PrivacyManagerClient::UnRegisterPermActiveStatusCallback( + const std::shared_ptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s: called!", __func__); + + std::lock_guard lock(activeCbkMutex_); + auto goalCallback = activeCbkMap_.find(callback); + if (goalCallback == activeCbkMap_.end()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "goalCallback already is not exist"); + return ERROR; + } + + auto proxy = GetProxy(); + if (proxy == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null"); + return ERROR; + } + + int32_t result = proxy->UnRegisterPermActiveStatusCallback(goalCallback->second->AsObject()); + activeCbkMap_.erase(goalCallback); + return result; +} + void PrivacyManagerClient::InitProxy() { std::lock_guard lock(proxyMutex_); diff --git a/interfaces/innerkits/privacy/src/privacy_manager_client.h b/interfaces/innerkits/privacy/src/privacy_manager_client.h index c43bd9dd1..28b4995ee 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_client.h +++ b/interfaces/innerkits/privacy/src/privacy_manager_client.h @@ -16,11 +16,14 @@ #ifndef PRIVACY_MANAGER_CLIENT_H #define PRIVACY_MANAGER_CLIENT_H +#include #include #include #include #include "i_privacy_manager.h" +#include "perm_active_status_change_callback.h" +#include "perm_active_status_change_customized_cbk.h" #include "privacy_death_recipient.h" namespace OHOS { @@ -41,6 +44,10 @@ public: int32_t GetPermissionUsedRecords( const PermissionUsedRequest& request, const sptr& callback); std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName); + int32_t RegisterPermActiveStatusCallback(const std::shared_ptr& callback); + int32_t UnRegisterPermActiveStatusCallback(const std::shared_ptr& callback); + int32_t CreateActiveStatusChangeCbk( + const std::shared_ptr& callback, sptr& callbackObject); void OnRemoteDiedHandle(); private: @@ -52,6 +59,10 @@ private: sptr serviceDeathObserver_ = nullptr; void InitProxy(); sptr GetProxy(); + +private: + std::mutex activeCbkMutex_; + std::map, sptr> activeCbkMap_; }; } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp b/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp index b8224d258..38cef8401 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp +++ b/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp @@ -211,6 +211,77 @@ std::string PrivacyManagerProxy::DumpRecordInfo(const std::string& bundleName, c return dumpInfo; } +int32_t PrivacyManagerProxy::RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called."); + MessageParcel data; + data.WriteInterfaceToken(IPrivacyManager::GetDescriptor()); + + uint32_t listSize = permList.size(); + if (!data.WriteUint32(listSize)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write listSize"); + return ERROR; + } + for (int i = 0; i < listSize; i++) { + if (!data.WriteString(permList[i])) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permList[%{public}d], %{public}s", i, permList[i].c_str()); + return ERROR; + } + } + + if (!data.WriteRemoteObject(callback)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object."); + return ERROR; + } + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null."); + return ERROR; + } + int32_t requestResult = remote->SendRequest( + static_cast(IPrivacyManager::InterfaceCode::REGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK), + data, reply, option); + if (requestResult != NO_ERROR) { + ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d.", requestResult); + return ERROR; + } + + int32_t result = reply.ReadInt32(); + return result; +} + +int32_t PrivacyManagerProxy::UnRegisterPermActiveStatusCallback(const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called."); + MessageParcel data; + data.WriteInterfaceToken(IPrivacyManager::GetDescriptor()); + + if (!data.WriteRemoteObject(callback)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object."); + return ERROR; + } + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null."); + return ERROR; + } + int32_t requestResult = remote->SendRequest( + static_cast(IPrivacyManager::InterfaceCode::UNREGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK), + data, reply, option); + if (requestResult != NO_ERROR) { + ACCESSTOKEN_LOG_ERROR(LABEL, "request fail, result: %{public}d.", requestResult); + return ERROR; + } + + int32_t result = reply.ReadInt32(); + return result; +} + bool PrivacyManagerProxy::SendRequest( IPrivacyManager::InterfaceCode code, MessageParcel& data, MessageParcel& reply) { diff --git a/interfaces/innerkits/privacy/src/privacy_manager_proxy.h b/interfaces/innerkits/privacy/src/privacy_manager_proxy.h index 1ed65c128..c3c2ef1a9 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_proxy.h +++ b/interfaces/innerkits/privacy/src/privacy_manager_proxy.h @@ -39,6 +39,8 @@ public: int32_t GetPermissionUsedRecords(const PermissionUsedRequestParcel& request, const sptr& callback) override; std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName) override; + int32_t RegisterPermActiveStatusCallback(std::vector& permList, const sptr& callback) override; + int32_t UnRegisterPermActiveStatusCallback(const sptr& callback) override; private: bool SendRequest(IPrivacyManager::InterfaceCode code, MessageParcel& data, MessageParcel& reply); diff --git a/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp b/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp index 4c5b9de02..97edbdd9f 100644 --- a/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp +++ b/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp @@ -51,6 +51,13 @@ static HapInfoParams g_InfoParmsB = { .appIDDesc = "privacy_test.bundleB" }; +static HapInfoParams g_InfoParmsE = { + .userID = 1, + .bundleName = "ohos.privacy_test.bundleE", + .instIndex = 0, + .appIDDesc = "privacy_test.bundleE" +}; + static AccessTokenID g_selfTokenId = 0; static AccessTokenID g_TokenId_A = 0; static AccessTokenID g_TokenId_B = 0; @@ -75,8 +82,12 @@ void PrivacyKitTest::SetUp() g_TokenId_B = AccessTokenKit::GetHapTokenID(g_InfoParmsB.userID, g_InfoParmsB.bundleName, g_InfoParmsB.instIndex); + AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(g_InfoParmsE.userID, + g_InfoParmsE.bundleName, + g_InfoParmsE.instIndex); + AccessTokenKit::DeleteToken(tokenId); - AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.permissionmanager", 0); + tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.permissionmanager", 0); SetSelfTokenID(tokenId); } @@ -92,6 +103,11 @@ void PrivacyKitTest::TearDown() g_InfoParmsB.bundleName, g_InfoParmsB.instIndex); AccessTokenKit::DeleteToken(tokenId); + + tokenId = AccessTokenKit::GetHapTokenID(g_InfoParmsE.userID, + g_InfoParmsE.bundleName, + g_InfoParmsE.instIndex); + AccessTokenKit::DeleteToken(tokenId); } std::string PrivacyKitTest::GetLocalDeviceUdid() @@ -536,4 +552,188 @@ HWTEST_F(PrivacyKitTest, GetPermissionUsedRecordsAsync002, TestSize.Level1) BuildQueryRequest(g_TokenId_A, GetLocalDeviceUdid(), "", permissionList, request); OHOS::sptr callback(new TestCallBack()); ASSERT_EQ(RET_NO_ERROR, PrivacyKit::GetPermissionUsedRecords(request, callback)); +} + +class CbCustomizeTest1 : public PermActiveStatusCustomizedCbk { +public: + explicit CbCustomizeTest1(const std::vector &permList) + : PermActiveStatusCustomizedCbk(permList) + { + GTEST_LOG_(INFO) << "CbCustomizeTest1 create"; + } + + ~CbCustomizeTest1() + {} + + virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result) + { + type_ = result.type; + GTEST_LOG_(INFO) << "CbCustomizeTest1 ActiveChangeResponse"; + GTEST_LOG_(INFO) << "tokenid " << result.tokenID; + GTEST_LOG_(INFO) << "permissionName " << result.permissionName; + GTEST_LOG_(INFO) << "deviceId " << result.deviceId; + GTEST_LOG_(INFO) << "type " << result.type; + } + + ActiveChangeType type_ = PERM_INACTIVE; +}; + +class CbCustomizeTest2 : public PermActiveStatusCustomizedCbk { +public: + explicit CbCustomizeTest2(const std::vector &permList) + : PermActiveStatusCustomizedCbk(permList) + { + GTEST_LOG_(INFO) << "CbCustomizeTest2 create"; + } + + ~CbCustomizeTest2() + {} + + virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result) + { + type_ = result.type; + GTEST_LOG_(INFO) << "CbCustomizeTest2 ActiveChangeResponse"; + GTEST_LOG_(INFO) << "tokenid " << result.tokenID; + GTEST_LOG_(INFO) << "permissionName " << result.permissionName; + GTEST_LOG_(INFO) << "deviceId " << result.deviceId; + GTEST_LOG_(INFO) << "type " << result.type; + } + + ActiveChangeType type_; +}; + +/** + * @tc.name: RegisterPermActiveStatusCallback001 + * @tc.desc: RegisterPermActiveStatusCallback with valid permission. + * @tc.type: FUNC + * @tc.require:Issue Number + */ +HWTEST_F(PrivacyKitTest, RegisterPermActiveStatusCallback001, TestSize.Level1) +{ + std::vector permList = {"ohos.permission.CAMERA"}; + + auto callbackPtr = std::make_shared(permList); + callbackPtr->type_ = PERM_INACTIVE; + + int32_t res = PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr); + + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, + .isGeneral = true, + .resDeviceID = {"local"} + }; + static HapPolicyParams infoManagerTestPolicyPrams = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA} + }; + + AccessTokenIDEx tokenIdEx = {0}; + AccessTokenID tokenID; + tokenIdEx = AccessTokenKit::AllocHapToken(g_InfoParmsE, infoManagerTestPolicyPrams); + + tokenID = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(0, tokenID); + + ATokenTypeEnum ret = AccessTokenKit::GetTokenTypeFlag(tokenID); + ASSERT_EQ(ret, TOKEN_HAP); + + res = PrivacyKit::StartUsingPermission(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(RET_SUCCESS, res); + + ASSERT_EQ(PERM_ACTIVE_IN_FOREGROUND, callbackPtr->type_); + + res = PrivacyKit::StopUsingPermission(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(PERM_INACTIVE, callbackPtr->type_); + + res = PrivacyKit::UnRegisterPermActiveStatusCallback(callbackPtr); + ASSERT_EQ(RET_SUCCESS, res); + callbackPtr->type_ = PERM_INACTIVE; + + res = PrivacyKit::StartUsingPermission(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(PERM_INACTIVE, callbackPtr->type_); + + res = PrivacyKit::StopUsingPermission(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(PERM_INACTIVE, callbackPtr->type_); + + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); +} + + +/** + * @tc.name: RegisterPermActiveStatusCallback002 + * @tc.desc: RegisterPermActiveStatusCallback with valid permission. + * @tc.type: FUNC + * @tc.require:Issue Number + */ + +HWTEST_F(PrivacyKitTest, RegisterPermActiveStatusCallback002, TestSize.Level1) +{ + std::vector permList1 = {"ohos.permission.CAMERA"}; + auto callbackPtr1 = std::make_shared(permList1); + callbackPtr1->type_ = PERM_INACTIVE; + + std::vector permList2 = {"ohos.permission.GET_BUNDLE_INFO"}; + auto callbackPtr2 = std::make_shared(permList2); + callbackPtr2->type_ = PERM_INACTIVE; + + int32_t res = PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr1); + res = PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr2); + + static PermissionStateFull infoManagerTestStateA = { + .permissionName = "ohos.permission.CAMERA", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, + .isGeneral = true, + .resDeviceID = {"local"} + }; + static PermissionStateFull infoManagerTestStateB = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO", + .grantFlags = {1}, + .grantStatus = {PERMISSION_GRANTED}, + .isGeneral = true, + .resDeviceID = {"local"} + }; + static HapPolicyParams infoManagerTestPolicyPrams = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {}, + .permStateList = {infoManagerTestStateA, infoManagerTestStateB} + }; + + AccessTokenIDEx tokenIdEx = {0}; + AccessTokenID tokenID; + tokenIdEx = AccessTokenKit::AllocHapToken(g_InfoParmsE, infoManagerTestPolicyPrams); + + tokenID = tokenIdEx.tokenIdExStruct.tokenID; + ASSERT_NE(0, tokenID); + + res = PrivacyKit::StartUsingPermission(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(RET_SUCCESS, res); + + ASSERT_EQ(PERM_ACTIVE_IN_FOREGROUND, callbackPtr1->type_); + ASSERT_EQ(PERM_INACTIVE, callbackPtr2->type_); + + res = PrivacyKit::StopUsingPermission(tokenID, "ohos.permission.CAMERA"); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(PERM_INACTIVE, callbackPtr1->type_); + + res = PrivacyKit::StartUsingPermission(tokenID, "ohos.permission.GET_BUNDLE_INFO"); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(PERM_INACTIVE, callbackPtr1->type_); + ASSERT_EQ(PERM_ACTIVE_IN_FOREGROUND, callbackPtr2->type_); + + res = PrivacyKit::StopUsingPermission(tokenID, "ohos.permission.GET_BUNDLE_INFO"); + ASSERT_EQ(RET_SUCCESS, res); + ASSERT_EQ(PERM_INACTIVE, callbackPtr2->type_); + + res = AccessTokenKit::DeleteToken(tokenID); + ASSERT_EQ(RET_SUCCESS, res); } \ No newline at end of file diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 5d00d6c65..9fb39cdd4 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -26,6 +26,7 @@ if (is_standard_system) { part_name = "access_token" include_dirs = [ + "include/active", "include/common", "include/database", "include/record", @@ -39,6 +40,9 @@ if (is_standard_system) { ] sources = [ + "src/active/perm_active_status_callback_death_recipient.cpp", + "src/active/perm_active_status_change_callback_manage.cpp", + "src/active/perm_active_status_change_callback_proxy.cpp", "src/common/constant.cpp", "src/common/time_util.cpp", "src/common/to_string.cpp", diff --git a/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h b/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h new file mode 100644 index 000000000..48a56a61f --- /dev/null +++ b/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h @@ -0,0 +1,35 @@ +/* + * 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_ACTIVE_STATUS_CALLBACK_DEATH_RECIPIENT_H +#define PERM_ACTIVE_STATUS_CALLBACK_DEATH_RECIPIENT_H + + +#include "iremote_object.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermActiveCallbackDeathRecipient : public IRemoteObject::DeathRecipient { +public: + PermActiveCallbackDeathRecipient() = default; + virtual ~PermActiveCallbackDeathRecipient() = default; + + virtual void OnRemoteDied(const wptr &remote); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERM_ACTIVE_STATUS_CALLBACK_DEATH_RECIPIENT_H diff --git a/services/privacymanager/include/active/perm_active_status_change_callback_manage.h b/services/privacymanager/include/active/perm_active_status_change_callback_manage.h new file mode 100644 index 000000000..0dd95a85f --- /dev/null +++ b/services/privacymanager/include/active/perm_active_status_change_callback_manage.h @@ -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. + */ + +#ifndef PERM_ACTIVE_STATUS_CHANGE_CALLBACK_MANAGE_H +#define PERM_ACTIVE_STATUS_CHANGE_CALLBACK_MANAGE_H + +#include +#include + +#include "access_token.h" +#include "accesstoken_log.h" +#include "perm_active_status_callback_death_recipient.h" +#include "perm_active_status_change_callback_proxy.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { + +struct CallbackData { + CallbackData() : permList_(), callbackObject_(nullptr) + {} + CallbackData(const std::vector& permList, sptr callback) + : permList_(permList), callbackObject_(callback) + {} + + std::vector permList_; + sptr callbackObject_; +}; + +class ActiveStatusCallbackManager { +public: + virtual ~ActiveStatusCallbackManager(); + ActiveStatusCallbackManager(); + static ActiveStatusCallbackManager& GetInstance(); + + int32_t AddCallback( + const std::vector& permList, const sptr& callback); + int32_t RemoveCallback(const sptr& callback); + bool NeedCalled(const std::vector& permList, const std::string& permName); + void ExcuteCallbackAsync( + AccessTokenID tokenID, const std::string& permName, const std::string& deviceId, ActiveChangeType changeType); + +private: + std::mutex mutex_; + std::vector callbackDataList_; + sptr callbackDeathRecipient_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERM_ACTIVE_STATUS_CHANGE_CALLBACK_MANAGE_H diff --git a/services/privacymanager/include/active/perm_active_status_change_callback_proxy.h b/services/privacymanager/include/active/perm_active_status_change_callback_proxy.h new file mode 100644 index 000000000..61695aac0 --- /dev/null +++ b/services/privacymanager/include/active/perm_active_status_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 PERM_ACTIVE_STATUS_CHANGE_CALLBACK_PROXY_H +#define PERM_ACTIVE_STATUS_CHANGE_CALLBACK_PROXY_H + + +#include "i_perm_active_status_callback.h" + +#include "iremote_proxy.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class PermActiveStatusChangeCallbackProxy : public IRemoteProxy { +public: + explicit PermActiveStatusChangeCallbackProxy(const sptr& impl); + ~PermActiveStatusChangeCallbackProxy() override; + + virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // PERM_ACTIVE_STATUS_CHANGE_CALLBACK_PROXY_H diff --git a/services/privacymanager/include/record/permission_record_manager.h b/services/privacymanager/include/record/permission_record_manager.h index 727599d11..bc8b417b2 100644 --- a/services/privacymanager/include/record/permission_record_manager.h +++ b/services/privacymanager/include/record/permission_record_manager.h @@ -46,13 +46,18 @@ public: int32_t GetPermissionUsedRecordsAsync( const PermissionUsedRequest& request, const sptr& callback); std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName); + int32_t StartUsingPermission(AccessTokenID tokenID, const std::string& permissionName); + int32_t StopUsingPermission(AccessTokenID tokenID, const std::string& permissionName); + int32_t RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback); + int32_t UnRegisterPermActiveStatusCallback(const sptr& callback); + bool GetPermissionVisitor(AccessTokenID tokenID, PermissionVisitor& visitor); private: PermissionRecordManager(); DISALLOW_COPY_AND_MOVE(PermissionRecordManager); bool AddVisitor(AccessTokenID tokenID, int32_t& visitorId); - bool GetPermissionVisitor(AccessTokenID tokenID, PermissionVisitor& visitor); bool AddRecord(int32_t visitorId, const std::string& permissionName, int32_t successCount, int32_t failCount); bool GetPermissionsRecord(int32_t visitorId, const std::string& permissionName, int32_t successCount, int32_t failCount, PermissionRecord& record); diff --git a/services/privacymanager/include/service/privacy_manager_service.h b/services/privacymanager/include/service/privacy_manager_service.h index d2c89b6ce..116249c7f 100644 --- a/services/privacymanager/include/service/privacy_manager_service.h +++ b/services/privacymanager/include/service/privacy_manager_service.h @@ -46,6 +46,9 @@ public: int32_t GetPermissionUsedRecords( const PermissionUsedRequestParcel& request, const sptr& callback) override; std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName) override; + int32_t RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback) override; + int32_t UnRegisterPermActiveStatusCallback(const sptr& callback) override; private: bool Initialize() const; diff --git a/services/privacymanager/include/service/privacy_manager_stub.h b/services/privacymanager/include/service/privacy_manager_stub.h index 1a8ce60ba..17f592436 100644 --- a/services/privacymanager/include/service/privacy_manager_stub.h +++ b/services/privacymanager/include/service/privacy_manager_stub.h @@ -39,7 +39,8 @@ private: void GetPermissionUsedRecordsInner(MessageParcel& data, MessageParcel& reply); void GetPermissionUsedRecordsAsyncInner(MessageParcel& data, MessageParcel& reply); void DumpRecordInfoInner(MessageParcel& data, MessageParcel& reply); - + void RegisterPermActiveStatusCallbackInner(MessageParcel& data, MessageParcel& reply); + void UnRegisterPermActiveStatusCallbackInner(MessageParcel& data, MessageParcel& reply); bool IsAccessTokenCalling() const; static const int32_t ACCESSTOKEN_UID = 3020; }; diff --git a/services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp b/services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp new file mode 100644 index 000000000..773dcc6a5 --- /dev/null +++ b/services/privacymanager/src/active/perm_active_status_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_active_status_callback_death_recipient.h" + +#include "access_token.h" +#include "perm_active_status_change_callback_manage.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermActiveCallbackDeathRecipient" +}; +} +void PermActiveCallbackDeathRecipient::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; + } + ActiveStatusCallbackManager::GetInstance().RemoveCallback(object); + ACCESSTOKEN_LOG_ERROR(LABEL, "end"); +} + +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp b/services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp new file mode 100644 index 000000000..c8b873455 --- /dev/null +++ b/services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp @@ -0,0 +1,149 @@ +/* + * 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_active_status_change_callback_manage.h" + +#include +#include +#include + +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermActiveStatusChangeCallbackManage" +}; +const time_t MAX_TIMEOUT_SEC = 30; +} + +ActiveStatusCallbackManager& ActiveStatusCallbackManager::GetInstance() +{ + static ActiveStatusCallbackManager instance; + return instance; +} + +ActiveStatusCallbackManager::ActiveStatusCallbackManager() : + callbackDeathRecipient_(sptr(new (std::nothrow) PermActiveCallbackDeathRecipient())) +{ +} + +ActiveStatusCallbackManager::~ActiveStatusCallbackManager() +{ +} + +int32_t ActiveStatusCallbackManager::AddCallback( + const std::vector& permList, const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "callback %{public}p ", (IRemoteObject *)callback); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "input is nullptr"); + return RET_FAILED; + } + + callback->AddDeathRecipient(callbackDeathRecipient_); + + CallbackData recordInstance; + recordInstance.callbackObject_ = callback; + recordInstance.permList_ = permList; + + std::lock_guard lock(mutex_); + callbackDataList_.emplace_back(recordInstance); + + ACCESSTOKEN_LOG_INFO(LABEL, "recordInstance is added"); + return RET_SUCCESS; +} + +int32_t ActiveStatusCallbackManager::RemoveCallback(const sptr& callback) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called"); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback is nullptr"); + return RET_FAILED; + } + + std::lock_guard lock(mutex_); + + for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) { + if (callback == (*it).callbackObject_) { + ACCESSTOKEN_LOG_INFO(LABEL, "find callback"); + if (callbackDeathRecipient_ != nullptr) { + callback->RemoveDeathRecipient(callbackDeathRecipient_); + } + (*it).callbackObject_ = nullptr; + callbackDataList_.erase(it); + break; + } + } + return RET_SUCCESS; +} + +bool ActiveStatusCallbackManager::NeedCalled(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 ActiveStatusCallbackManager::ExcuteCallbackAsync( + AccessTokenID tokenID, const std::string& permName, const std::string& deviceId, ActiveChangeType changeType) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "entry"); + + auto callbackFunc = [&]() { + ACCESSTOKEN_LOG_INFO(LABEL, "callbackStart"); + std::lock_guard lock(mutex_); + for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) { + std::vector permList = (*it).permList_; + if (!NeedCalled(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) { + ActiveChangeResponse resInfo; + resInfo.type = changeType; + resInfo.permissionName = permName; + resInfo.tokenID = tokenID; + resInfo.deviceId = deviceId; + ACCESSTOKEN_LOG_INFO(LABEL, "callback excute changeType %{public}d", changeType); + callback->ActiveStatusChangeCallback(resInfo); + } + } + }; + + std::packaged_task callbackTask(callbackFunc); + std::future fut = callbackTask.get_future(); + std::make_unique(std::move(callbackTask))->detach(); + + ACCESSTOKEN_LOG_INFO(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_INFO(LABEL, "The callback execution is complete"); + +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp b/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp new file mode 100644 index 000000000..eac34f1b6 --- /dev/null +++ b/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp @@ -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. + */ + +#include "perm_active_status_change_callback_proxy.h" + +#include "accesstoken_log.h" +#include "perm_active_response_parcel.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermActiveStatusChangeCallbackProxy" +}; +} + +PermActiveStatusChangeCallbackProxy::PermActiveStatusChangeCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) { +} + +PermActiveStatusChangeCallbackProxy::~PermActiveStatusChangeCallbackProxy() +{} + +void PermActiveStatusChangeCallbackProxy::ActiveStatusChangeCallback(ActiveChangeResponse& result) +{ + MessageParcel data; + data.WriteInterfaceToken(IPermActiveStatusChangeCbk::GetDescriptor()); + + ActiveChangeResponseParcel resultParcel; + resultParcel.changeResponse = result; + if (!data.WriteParcelable(&resultParcel)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable"); + 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(IPermActiveStatusChangeCbk::PERM_ACTIVE_STATUS_CHANGE), data, reply, option); + if (requestResult != NO_ERROR) { + ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult); + return; + } + + ACCESSTOKEN_LOG_INFO(LABEL, "SendRequest success"); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/privacymanager/src/record/permission_record_manager.cpp b/services/privacymanager/src/record/permission_record_manager.cpp index 024790c7d..cba2f296c 100644 --- a/services/privacymanager/src/record/permission_record_manager.cpp +++ b/services/privacymanager/src/record/permission_record_manager.cpp @@ -23,6 +23,7 @@ #include "field_const.h" #include "permission_record_repository.h" #include "permission_visitor_repository.h" +#include "perm_active_status_change_callback_manage.h" #include "time_util.h" #include "to_string.h" @@ -422,6 +423,82 @@ std::string PermissionRecordManager::DumpRecordInfo(const std::string& bundleNam return dumpInfo; } +static void RegisterFrontAndBackGroundlistener() +{ + // 给注册前后台监听打桩 + // 此处需要实现一个callback函数用于被注册 +} + +static void UnRegisterFrontAndBackGroundlistener() +{ + +} + +// 被注册的函数 +int32_t FrontAndBackGroundlistener( + AccessTokenID tokenID, const std::string& permissionName, int32_t statusType) +{ + PermissionVisitor visitor; + bool res = PermissionRecordManager::GetInstance().GetPermissionVisitor(tokenID, visitor); + if (!res) { + return Constant::FAILURE; + } + ActiveStatusCallbackManager::GetInstance().ExcuteCallbackAsync( + tokenID, permissionName, visitor.deviceId, static_cast(statusType)); + return Constant::SUCCESS; +} + +int32_t PermissionRecordManager::StartUsingPermission(AccessTokenID tokenID, const std::string& permissionName) +{ + // 记录开始使用 + + // 此处是否结合全局变量的使用来判断是否开始记录 + + // 注册前后台监听 + RegisterFrontAndBackGroundlistener(); + + // 触发给systemUi当前的权限在前台使用 + PermissionVisitor visitor; + bool res = GetPermissionVisitor(tokenID, visitor); + if (!res) { + return Constant::FAILURE; + } + + ActiveStatusCallbackManager::GetInstance().ExcuteCallbackAsync( + tokenID, permissionName, visitor.deviceId, PERM_ACTIVE_IN_FOREGROUND); + return Constant::SUCCESS; +} + +int32_t PermissionRecordManager::StopUsingPermission(AccessTokenID tokenID, const std::string& permissionName) +{ + // 记录结束使用 + + // 注册前后台监听 + UnRegisterFrontAndBackGroundlistener(); + + // 触发给systemUi当前的权限在前台使用 + PermissionVisitor visitor; + bool res = GetPermissionVisitor(tokenID, visitor); + if (!res) { + return Constant::FAILURE; + } + + ActiveStatusCallbackManager::GetInstance().ExcuteCallbackAsync( + tokenID, permissionName, visitor.deviceId, PERM_INACTIVE); + return Constant::SUCCESS; +} + +int32_t PermissionRecordManager::RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback) +{ + return ActiveStatusCallbackManager::GetInstance().AddCallback(permList, callback); +} + +int32_t PermissionRecordManager::UnRegisterPermActiveStatusCallback(const sptr& callback) +{ + return ActiveStatusCallbackManager::GetInstance().RemoveCallback(callback); +} + bool PermissionRecordManager::IsLocalDevice(const std::string& deviceId) { if (deviceId == "0") { // local diff --git a/services/privacymanager/src/service/privacy_manager_service.cpp b/services/privacymanager/src/service/privacy_manager_service.cpp index 1f9cecd49..92e690931 100644 --- a/services/privacymanager/src/service/privacy_manager_service.cpp +++ b/services/privacymanager/src/service/privacy_manager_service.cpp @@ -83,14 +83,14 @@ int32_t PrivacyManagerService::StartUsingPermission(AccessTokenID tokenID, const { ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, permission: %{public}s", tokenID, permissionName.c_str()); - return Constant::SUCCESS; + return PermissionRecordManager::GetInstance().StartUsingPermission(tokenID, permissionName); } int32_t PrivacyManagerService::StopUsingPermission(AccessTokenID tokenID, const std::string& permissionName) { ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, permission: %{public}s", tokenID, permissionName.c_str()); - return Constant::SUCCESS; + return PermissionRecordManager::GetInstance().StopUsingPermission(tokenID, permissionName); } int32_t PrivacyManagerService::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) @@ -125,6 +125,17 @@ std::string PrivacyManagerService::DumpRecordInfo(const std::string& bundleName, return PermissionRecordManager::GetInstance().DumpRecordInfo(bundleName, permissionName); } +int32_t PrivacyManagerService::RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback) +{ + return PermissionRecordManager::GetInstance().RegisterPermActiveStatusCallback(permList, callback); +} + +int32_t PrivacyManagerService::UnRegisterPermActiveStatusCallback(const sptr& callback) +{ + return PermissionRecordManager::GetInstance().UnRegisterPermActiveStatusCallback(callback); +} + bool PrivacyManagerService::Initialize() const { PermissionRecordManager::GetInstance().Init(); diff --git a/services/privacymanager/src/service/privacy_manager_stub.cpp b/services/privacymanager/src/service/privacy_manager_stub.cpp index bd1dd4fe7..37c1ded5c 100644 --- a/services/privacymanager/src/service/privacy_manager_stub.cpp +++ b/services/privacymanager/src/service/privacy_manager_stub.cpp @@ -60,6 +60,12 @@ int32_t PrivacyManagerStub::OnRemoteRequest( case static_cast(IPrivacyManager::InterfaceCode::DUMP_RECORD_INFO): DumpRecordInfoInner(data, reply); break; + case static_cast(IPrivacyManager::InterfaceCode::REGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK): + RegisterPermActiveStatusCallbackInner(data, reply); + break; + case static_cast(IPrivacyManager::InterfaceCode::UNREGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK): + UnRegisterPermActiveStatusCallbackInner(data, reply); + break; default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -158,6 +164,43 @@ void PrivacyManagerStub::DumpRecordInfoInner(MessageParcel& data, MessageParcel& reply.WriteString(dumpInfo); } +void PrivacyManagerStub::RegisterPermActiveStatusCallbackInner(MessageParcel& data, MessageParcel& reply) +{ + uint32_t permListSize = data.ReadUint32(); + if (permListSize > 200) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read permListSize fail"); + reply.WriteInt32(RET_FAILED); + return; + } + std::vector permList; + for (uint32_t i = 0; i < permListSize; i++) { + std::string perm = data.ReadString(); + permList.emplace_back(perm); + } + sptr callback = data.ReadRemoteObject(); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read ReadRemoteObject fail"); + reply.WriteInt32(RET_FAILED); + return; + } + ACCESSTOKEN_LOG_INFO(LABEL, "callback %{public}p ", (IRemoteObject *)callback); + int32_t result = this->RegisterPermActiveStatusCallback(permList, callback); + reply.WriteInt32(result); +} + +void PrivacyManagerStub::UnRegisterPermActiveStatusCallbackInner(MessageParcel& data, MessageParcel& reply) +{ + sptr callback = data.ReadRemoteObject(); + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "read scopeParcel fail"); + reply.WriteInt32(RET_FAILED); + return; + } + ACCESSTOKEN_LOG_INFO(LABEL, "callback %{public}p ", (IRemoteObject *)callback); + int32_t result = this->UnRegisterPermActiveStatusCallback(callback); + reply.WriteInt32(result); +} + bool PrivacyManagerStub::IsAccessTokenCalling() const { int32_t callingUid = IPCSkeleton::GetCallingUid(); -- Gitee From c429d3b4ecd965b2235cf6f53202e09615dc78de Mon Sep 17 00:00:00 2001 From: lsq Date: Sat, 23 Jul 2022 17:09:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lsq Change-Id: I609e765fc1bd883c6c9227d16939cde1e97e0386 --- frameworks/privacy/BUILD.gn | 2 +- .../include/i_perm_active_status_callback.h | 4 +- .../include/perm_active_response_parcel.h | 8 +-- .../src/perm_active_response_parcel.cpp | 4 +- interfaces/innerkits/privacy/BUILD.gn | 2 +- .../include/active_change_response_info.h | 6 +-- .../perm_active_status_change_callback.h | 2 +- .../perm_active_status_change_callback_stub.h | 2 +- ....h => perm_active_status_customized_cbk.h} | 6 +-- .../innerkits/privacy/include/privacy_kit.h | 2 +- ...erm_active_status_change_callback_stub.cpp | 5 +- ... => perm_active_status_customized_cbk.cpp} | 4 +- .../privacy/src/privacy_manager_client.cpp | 18 ++++--- .../privacy/src/privacy_manager_client.h | 2 +- .../privacy/src/privacy_manager_proxy.cpp | 25 +++++++--- .../privacy/src/privacy_manager_proxy.h | 3 +- .../test/unittest/src/privacy_kit_test.cpp | 9 ++-- services/privacymanager/BUILD.gn | 2 +- ...age.h => active_status_callback_manager.h} | 7 ++- ...m_active_status_callback_death_recipient.h | 6 +-- ...perm_active_status_change_callback_proxy.h | 2 +- ...cpp => active_status_callback_manager.cpp} | 23 +++++---- ...active_status_callback_death_recipient.cpp | 9 ++-- ...rm_active_status_change_callback_proxy.cpp | 6 +-- .../src/record/permission_record_manager.cpp | 50 ++----------------- .../src/service/privacy_manager_stub.cpp | 6 +-- 26 files changed, 96 insertions(+), 119 deletions(-) rename interfaces/innerkits/privacy/include/{perm_active_status_change_customized_cbk.h => perm_active_status_customized_cbk.h} (86%) rename interfaces/innerkits/privacy/src/{perm_active_status_change_customized_cbk.cpp => perm_active_status_customized_cbk.cpp} (90%) rename services/privacymanager/include/active/{perm_active_status_change_callback_manage.h => active_status_callback_manager.h} (92%) rename services/privacymanager/src/active/{perm_active_status_change_callback_manage.cpp => active_status_callback_manager.cpp} (85%) diff --git a/frameworks/privacy/BUILD.gn b/frameworks/privacy/BUILD.gn index 8b434daf1..c050de318 100644 --- a/frameworks/privacy/BUILD.gn +++ b/frameworks/privacy/BUILD.gn @@ -27,10 +27,10 @@ ohos_shared_library("privacy_communication_adapter_cxx") { sources = [ "src/bundle_used_record_parcel.cpp", + "src/perm_active_response_parcel.cpp", "src/permission_used_record_parcel.cpp", "src/permission_used_request_parcel.cpp", "src/permission_used_result_parcel.cpp", - "src/perm_active_response_parcel.cpp", "src/used_record_detail_parcel.cpp", ] diff --git a/frameworks/privacy/include/i_perm_active_status_callback.h b/frameworks/privacy/include/i_perm_active_status_callback.h index 8d164cad0..5f1e441c5 100644 --- a/frameworks/privacy/include/i_perm_active_status_callback.h +++ b/frameworks/privacy/include/i_perm_active_status_callback.h @@ -25,9 +25,9 @@ namespace OHOS { namespace Security { namespace AccessToken { -class IPermActiveStatusChangeCbk : public IRemoteBroker { +class IPermActiveStatusCallback : public IRemoteBroker { public: - DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.accesstoken.IPermActiveStatusChangeCbk"); + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.accesstoken.IPermActiveStatusCallback"); virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result) = 0; diff --git a/frameworks/privacy/include/perm_active_response_parcel.h b/frameworks/privacy/include/perm_active_response_parcel.h index b870dd23d..706d9fa4b 100644 --- a/frameworks/privacy/include/perm_active_response_parcel.h +++ b/frameworks/privacy/include/perm_active_response_parcel.h @@ -13,11 +13,11 @@ * limitations under the License. */ -#ifndef PERMISSION_USED_RECORD_PRACEL_H -#define PERMISSION_USED_RECORD_PRACEL_H +#ifndef PERM_ACTIVE_RESPONES_PARCEL_H +#define PERM_ACTIVE_RESPONES_PARCEL_H -#include "parcel.h" #include "active_change_response_info.h" +#include "parcel.h" namespace OHOS { namespace Security { @@ -37,4 +37,4 @@ struct ActiveChangeResponseParcel final : public Parcelable { } // namespace Security } // namespace OHOS -#endif // PERMISSION_USED_RECORD_PRACEL_H +#endif // PERM_ACTIVE_RESPONES_PARCEL_H diff --git a/frameworks/privacy/src/perm_active_response_parcel.cpp b/frameworks/privacy/src/perm_active_response_parcel.cpp index 9a475dd85..49617f9d7 100644 --- a/frameworks/privacy/src/perm_active_response_parcel.cpp +++ b/frameworks/privacy/src/perm_active_response_parcel.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "perm_active_response_parcel.h" #include "parcel_utils.h" +#include "perm_active_response_parcel.h" namespace OHOS { namespace Security { @@ -36,7 +36,7 @@ ActiveChangeResponseParcel* ActiveChangeResponseParcel::Unmarshalling(Parcel& in } RELEASE_IF_FALSE(in.ReadUint32(activeChangeResponseParcel->changeResponse.tokenID), activeChangeResponseParcel); - RELEASE_IF_FALSE(in.ReadString(activeChangeResponseParcel->changeResponse.permissionName), + RELEASE_IF_FALSE(in.ReadString(activeChangeResponseParcel->changeResponse.permissionName), activeChangeResponseParcel); RELEASE_IF_FALSE(in.ReadString(activeChangeResponseParcel->changeResponse.deviceId), activeChangeResponseParcel); diff --git a/interfaces/innerkits/privacy/BUILD.gn b/interfaces/innerkits/privacy/BUILD.gn index f0b391208..ff25990d7 100644 --- a/interfaces/innerkits/privacy/BUILD.gn +++ b/interfaces/innerkits/privacy/BUILD.gn @@ -40,7 +40,7 @@ if (is_standard_system) { sources = [ "src/perm_active_status_change_callback.cpp", "src/perm_active_status_change_callback_stub.cpp", - "src/perm_active_status_change_customized_cbk.cpp", + "src/perm_active_status_customized_cbk.cpp", "src/privacy_death_recipient.cpp", "src/privacy_kit.cpp", "src/privacy_manager_client.cpp", diff --git a/interfaces/innerkits/privacy/include/active_change_response_info.h b/interfaces/innerkits/privacy/include/active_change_response_info.h index 71f734462..f2072fa20 100644 --- a/interfaces/innerkits/privacy/include/active_change_response_info.h +++ b/interfaces/innerkits/privacy/include/active_change_response_info.h @@ -23,10 +23,7 @@ namespace OHOS { namespace Security { -namespace AccessToken{ -#define TOKENID_LIST_SIZE_MAX 1024 -#define PERM_LIST_SIZE_MAX 1024 - +namespace AccessToken { enum ActiveChangeType { PERM_INACTIVE = 0, PERM_ACTIVE_IN_FOREGROUND = 1, @@ -39,7 +36,6 @@ struct ActiveChangeResponse { std::string deviceId; ActiveChangeType type; }; - } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h b/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h index 02ddc121f..0d51ea2ba 100644 --- a/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h +++ b/interfaces/innerkits/privacy/include/perm_active_status_change_callback.h @@ -21,7 +21,7 @@ #include "active_change_response_info.h" #include "perm_active_status_change_callback_stub.h" -#include "perm_active_status_change_customized_cbk.h" +#include "perm_active_status_customized_cbk.h" namespace OHOS { namespace Security { diff --git a/interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h b/interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h index 71838bd3a..cc9a6c81a 100644 --- a/interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h +++ b/interfaces/innerkits/privacy/include/perm_active_status_change_callback_stub.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Security { namespace AccessToken { -class PermActiveStatusChangeCallbackStub : public IRemoteStub { +class PermActiveStatusChangeCallbackStub : public IRemoteStub { public: PermActiveStatusChangeCallbackStub() = default; virtual ~PermActiveStatusChangeCallbackStub() = default; diff --git a/interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h b/interfaces/innerkits/privacy/include/perm_active_status_customized_cbk.h similarity index 86% rename from interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h rename to interfaces/innerkits/privacy/include/perm_active_status_customized_cbk.h index e5a765476..ac4519d75 100644 --- a/interfaces/innerkits/privacy/include/perm_active_status_change_customized_cbk.h +++ b/interfaces/innerkits/privacy/include/perm_active_status_customized_cbk.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef PERM_ACTIVE_STATUS_CHANGE_CUSTOMIZED_CBK_H -#define PERM_ACTIVE_STATUS_CHANGE_CUSTOMIZED_CBK_H +#ifndef PERM_ACTIVE_STATUS_CUSTOMIZED_CBK_H +#define PERM_ACTIVE_STATUS_CUSTOMIZED_CBK_H #include #include @@ -42,4 +42,4 @@ private: } // namespace Security } // namespace OHOS -#endif // PERM_ACTIVE_STATUS_CHANGE_CUSTOMIZED_CBK_H \ No newline at end of file +#endif // PERM_ACTIVE_STATUS_CUSTOMIZED_CBK_H \ No newline at end of file diff --git a/interfaces/innerkits/privacy/include/privacy_kit.h b/interfaces/innerkits/privacy/include/privacy_kit.h index 3c7e34cc7..1279757e7 100644 --- a/interfaces/innerkits/privacy/include/privacy_kit.h +++ b/interfaces/innerkits/privacy/include/privacy_kit.h @@ -22,7 +22,7 @@ #include "on_permission_used_record_callback.h" #include "permission_used_request.h" #include "permission_used_result.h" -#include "perm_active_status_change_customized_cbk.h" +#include "perm_active_status_customized_cbk.h" namespace OHOS { namespace Security { diff --git a/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp b/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp index 3fcfcb3ec..fa4d4e035 100644 --- a/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp +++ b/interfaces/innerkits/privacy/src/perm_active_status_change_callback_stub.cpp @@ -33,13 +33,13 @@ int32_t PermActiveStatusChangeCallbackStub::OnRemoteRequest( { ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, code: 0x%{public}x", code); std::u16string descriptor = data.ReadInterfaceToken(); - if (descriptor != IPermActiveStatusChangeCbk::GetDescriptor()) { + if (descriptor != IPermActiveStatusCallback::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 == IPermActiveStatusChangeCbk::PERM_ACTIVE_STATUS_CHANGE) { + if (msgCode == IPermActiveStatusCallback::PERM_ACTIVE_STATUS_CHANGE) { sptr resultSptr = data.ReadParcelable(); if (resultSptr == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail"); @@ -52,7 +52,6 @@ int32_t PermActiveStatusChangeCallbackStub::OnRemoteRequest( } return RET_SUCCESS; } - } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.cpp b/interfaces/innerkits/privacy/src/perm_active_status_customized_cbk.cpp similarity index 90% rename from interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.cpp rename to interfaces/innerkits/privacy/src/perm_active_status_customized_cbk.cpp index a87e15486..c4521f8f8 100644 --- a/interfaces/innerkits/privacy/src/perm_active_status_change_customized_cbk.cpp +++ b/interfaces/innerkits/privacy/src/perm_active_status_customized_cbk.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "perm_active_status_change_customized_cbk.h" +#include "perm_active_status_customized_cbk.h" namespace OHOS { namespace Security { @@ -22,7 +22,7 @@ PermActiveStatusCustomizedCbk::PermActiveStatusCustomizedCbk() {} PermActiveStatusCustomizedCbk::PermActiveStatusCustomizedCbk( - const std::vector& permList) : permList_(permList) // 此处是否有问题 + const std::vector& permList) : permList_(permList) {} PermActiveStatusCustomizedCbk::~PermActiveStatusCustomizedCbk() diff --git a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp index f46fb627a..769dc785a 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp +++ b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp @@ -30,6 +30,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { } // namespace const static int32_t ERROR = -1; +const static int32_t MAX_PERM_LIST_SIZE = 200; PrivacyManagerClient& PrivacyManagerClient::GetInstance() { @@ -148,17 +149,18 @@ int32_t PrivacyManagerClient::CreateActiveStatusChangeCbk( { std::lock_guard lock(activeCbkMutex_); + if (activeCbkMap_.size() == MAX_PERM_LIST_SIZE) { + ACCESSTOKEN_LOG_ERROR(LABEL, "the maximum number of subscribers has been reached"); + return ERROR; + } + auto goalCallback = activeCbkMap_.find(callback); if (goalCallback != activeCbkMap_.end()) { callbackObject = goalCallback->second->AsObject(); ACCESSTOKEN_LOG_ERROR(LABEL, "activeCbkMap_ already has such callback"); return ERROR; } else { - if (activeCbkMap_.size() == 200) { - ACCESSTOKEN_LOG_ERROR(LABEL, "the maximum number of subscribers has been reached"); - return ERROR; - } - sptr callbackWraped = + sptr callbackWraped = new (std::nothrow) PermActiveStatusChangeCallback(callback); if (!callbackWraped) { ACCESSTOKEN_LOG_ERROR(LABEL, "memory allocation for callbackWraped failed!"); @@ -200,7 +202,7 @@ int32_t PrivacyManagerClient::RegisterPermActiveStatusCallback( int32_t PrivacyManagerClient::UnRegisterPermActiveStatusCallback( const std::shared_ptr& callback) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s: called!", __func__); + ACCESSTOKEN_LOG_INFO(LABEL, "called!"); std::lock_guard lock(activeCbkMutex_); auto goalCallback = activeCbkMap_.find(callback); @@ -216,7 +218,9 @@ int32_t PrivacyManagerClient::UnRegisterPermActiveStatusCallback( } int32_t result = proxy->UnRegisterPermActiveStatusCallback(goalCallback->second->AsObject()); - activeCbkMap_.erase(goalCallback); + if (result == RET_SUCCESS) { + activeCbkMap_.erase(goalCallback); + } return result; } diff --git a/interfaces/innerkits/privacy/src/privacy_manager_client.h b/interfaces/innerkits/privacy/src/privacy_manager_client.h index 28b4995ee..ebae78818 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_client.h +++ b/interfaces/innerkits/privacy/src/privacy_manager_client.h @@ -23,7 +23,7 @@ #include "i_privacy_manager.h" #include "perm_active_status_change_callback.h" -#include "perm_active_status_change_customized_cbk.h" +#include "perm_active_status_customized_cbk.h" #include "privacy_death_recipient.h" namespace OHOS { diff --git a/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp b/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp index 38cef8401..c8f7c149e 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp +++ b/interfaces/innerkits/privacy/src/privacy_manager_proxy.cpp @@ -216,14 +216,17 @@ int32_t PrivacyManagerProxy::RegisterPermActiveStatusCallback( { ACCESSTOKEN_LOG_INFO(LABEL, "called."); MessageParcel data; - data.WriteInterfaceToken(IPrivacyManager::GetDescriptor()); + if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken."); + return ERROR; + } uint32_t listSize = permList.size(); if (!data.WriteUint32(listSize)) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write listSize"); return ERROR; } - for (int i = 0; i < listSize; i++) { + for (uint32_t i = 0; i < listSize; i++) { if (!data.WriteString(permList[i])) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permList[%{public}d], %{public}s", i, permList[i].c_str()); return ERROR; @@ -249,7 +252,11 @@ int32_t PrivacyManagerProxy::RegisterPermActiveStatusCallback( return ERROR; } - int32_t result = reply.ReadInt32(); + int32_t result; + if (!reply.ReadInt32(result)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 fail"); + return ERROR; + } return result; } @@ -257,8 +264,10 @@ int32_t PrivacyManagerProxy::UnRegisterPermActiveStatusCallback(const sptr& callback) override; std::string DumpRecordInfo(const std::string& bundleName, const std::string& permissionName) override; - int32_t RegisterPermActiveStatusCallback(std::vector& permList, const sptr& callback) override; + int32_t RegisterPermActiveStatusCallback( + std::vector& permList, const sptr& callback) override; int32_t UnRegisterPermActiveStatusCallback(const sptr& callback) override; private: diff --git a/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp b/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp index 97edbdd9f..4f7ed70a6 100644 --- a/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp +++ b/interfaces/innerkits/privacy/test/unittest/src/privacy_kit_test.cpp @@ -73,6 +73,7 @@ void PrivacyKitTest::TearDownTestCase() void PrivacyKitTest::SetUp() { + AccessTokenID tokenId; AccessTokenKit::AllocHapToken(g_InfoParmsA, g_PolicyPramsA); AccessTokenKit::AllocHapToken(g_InfoParmsB, g_PolicyPramsB); @@ -82,12 +83,12 @@ void PrivacyKitTest::SetUp() g_TokenId_B = AccessTokenKit::GetHapTokenID(g_InfoParmsB.userID, g_InfoParmsB.bundleName, g_InfoParmsB.instIndex); - AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(g_InfoParmsE.userID, - g_InfoParmsE.bundleName, - g_InfoParmsE.instIndex); + tokenId = AccessTokenKit::GetHapTokenID(g_InfoParmsE.userID, + g_InfoParmsE.bundleName, + g_InfoParmsE.instIndex); AccessTokenKit::DeleteToken(tokenId); - tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.permissionmanager", 0); + tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.permissionmanager", 0); // 100 is userID SetSelfTokenID(tokenId); } diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 9fb39cdd4..b3b68f067 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -40,8 +40,8 @@ if (is_standard_system) { ] sources = [ + "src/active/active_status_callback_manager.cpp", "src/active/perm_active_status_callback_death_recipient.cpp", - "src/active/perm_active_status_change_callback_manage.cpp", "src/active/perm_active_status_change_callback_proxy.cpp", "src/common/constant.cpp", "src/common/time_util.cpp", diff --git a/services/privacymanager/include/active/perm_active_status_change_callback_manage.h b/services/privacymanager/include/active/active_status_callback_manager.h similarity index 92% rename from services/privacymanager/include/active/perm_active_status_change_callback_manage.h rename to services/privacymanager/include/active/active_status_callback_manager.h index 0dd95a85f..36673248e 100644 --- a/services/privacymanager/include/active/perm_active_status_change_callback_manage.h +++ b/services/privacymanager/include/active/active_status_callback_manager.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef PERM_ACTIVE_STATUS_CHANGE_CALLBACK_MANAGE_H -#define PERM_ACTIVE_STATUS_CHANGE_CALLBACK_MANAGE_H +#ifndef ACTIVE_STATUS_CHANGE_CALLBACK_MANAGER_H +#define ACTIVE_STATUS_CHANGE_CALLBACK_MANAGER_H #include #include @@ -27,7 +27,6 @@ namespace OHOS { namespace Security { namespace AccessToken { - struct CallbackData { CallbackData() : permList_(), callbackObject_(nullptr) {} @@ -60,4 +59,4 @@ private: } // namespace AccessToken } // namespace Security } // namespace OHOS -#endif // PERM_ACTIVE_STATUS_CHANGE_CALLBACK_MANAGE_H +#endif // ACTIVE_STATUS_CHANGE_CALLBACK_MANAGER_H diff --git a/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h b/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h index 48a56a61f..c69e05e3e 100644 --- a/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h +++ b/services/privacymanager/include/active/perm_active_status_callback_death_recipient.h @@ -22,10 +22,10 @@ namespace OHOS { namespace Security { namespace AccessToken { -class PermActiveCallbackDeathRecipient : public IRemoteObject::DeathRecipient { +class PermActiveStatusCallbackDeathRecipient : public IRemoteObject::DeathRecipient { public: - PermActiveCallbackDeathRecipient() = default; - virtual ~PermActiveCallbackDeathRecipient() = default; + PermActiveStatusCallbackDeathRecipient() = default; + virtual ~PermActiveStatusCallbackDeathRecipient() = default; virtual void OnRemoteDied(const wptr &remote); }; diff --git a/services/privacymanager/include/active/perm_active_status_change_callback_proxy.h b/services/privacymanager/include/active/perm_active_status_change_callback_proxy.h index 61695aac0..db358c94b 100644 --- a/services/privacymanager/include/active/perm_active_status_change_callback_proxy.h +++ b/services/privacymanager/include/active/perm_active_status_change_callback_proxy.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Security { namespace AccessToken { -class PermActiveStatusChangeCallbackProxy : public IRemoteProxy { +class PermActiveStatusChangeCallbackProxy : public IRemoteProxy { public: explicit PermActiveStatusChangeCallbackProxy(const sptr& impl); ~PermActiveStatusChangeCallbackProxy() override; diff --git a/services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp b/services/privacymanager/src/active/active_status_callback_manager.cpp similarity index 85% rename from services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp rename to services/privacymanager/src/active/active_status_callback_manager.cpp index c8b873455..899413d16 100644 --- a/services/privacymanager/src/active/perm_active_status_change_callback_manage.cpp +++ b/services/privacymanager/src/active/active_status_callback_manager.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "perm_active_status_change_callback_manage.h" +#include "active_status_callback_manager.h" #include #include @@ -26,9 +26,10 @@ namespace Security { namespace AccessToken { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermActiveStatusChangeCallbackManage" + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "ActiveStatusCallbackManager" }; -const time_t MAX_TIMEOUT_SEC = 30; +static const time_t MAX_TIMEOUT_SEC = 30; +static const time_t MAX_CALLBACK_SIZE = 200; } ActiveStatusCallbackManager& ActiveStatusCallbackManager::GetInstance() @@ -37,8 +38,9 @@ ActiveStatusCallbackManager& ActiveStatusCallbackManager::GetInstance() return instance; } -ActiveStatusCallbackManager::ActiveStatusCallbackManager() : - callbackDeathRecipient_(sptr(new (std::nothrow) PermActiveCallbackDeathRecipient())) +ActiveStatusCallbackManager::ActiveStatusCallbackManager() + : callbackDeathRecipient_(sptr( + new (std::nothrow) PermActiveStatusCallbackDeathRecipient())) { } @@ -62,6 +64,10 @@ int32_t ActiveStatusCallbackManager::AddCallback( recordInstance.permList_ = permList; std::lock_guard lock(mutex_); + if (callbackDataList_.size() > MAX_CALLBACK_SIZE) { + ACCESSTOKEN_LOG_ERROR(LABEL, "list size has reached max value"); + return RET_FAILED; + } callbackDataList_.emplace_back(recordInstance); ACCESSTOKEN_LOG_INFO(LABEL, "recordInstance is added"); @@ -116,10 +122,10 @@ void ActiveStatusCallbackManager::ExcuteCallbackAsync( for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) { std::vector permList = (*it).permList_; if (!NeedCalled(permList, permName)) { - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID is %{public}u, permName is %{public}s", tokenID, permName.c_str()); - continue; + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID %{public}u, permName %{public}s", tokenID, permName.c_str()); + continue; } - auto callback = iface_cast((*it).callbackObject_); + auto callback = iface_cast((*it).callbackObject_); if (callback != nullptr) { ActiveChangeResponse resInfo; resInfo.type = changeType; @@ -142,7 +148,6 @@ void ActiveStatusCallbackManager::ExcuteCallbackAsync( ACCESSTOKEN_LOG_WARN(LABEL, "callbackTask callback execution timeout"); } ACCESSTOKEN_LOG_INFO(LABEL, "The callback execution is complete"); - } } // namespace AccessToken } // namespace Security diff --git a/services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp b/services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp index 773dcc6a5..1acb1bdec 100644 --- a/services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp +++ b/services/privacymanager/src/active/perm_active_status_callback_death_recipient.cpp @@ -16,17 +16,17 @@ #include "perm_active_status_callback_death_recipient.h" #include "access_token.h" -#include "perm_active_status_change_callback_manage.h" +#include "active_status_callback_manager.h" namespace OHOS { namespace Security { namespace AccessToken { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermActiveCallbackDeathRecipient" + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PermActiveStatusCallbackDeathRecipient" }; } -void PermActiveCallbackDeathRecipient::OnRemoteDied(const wptr &remote) +void PermActiveStatusCallbackDeathRecipient::OnRemoteDied(const wptr &remote) { ACCESSTOKEN_LOG_INFO(LABEL, "enter"); if (remote == nullptr) { @@ -40,9 +40,8 @@ void PermActiveCallbackDeathRecipient::OnRemoteDied(const wptr &r return; } ActiveStatusCallbackManager::GetInstance().RemoveCallback(object); - ACCESSTOKEN_LOG_ERROR(LABEL, "end"); + ACCESSTOKEN_LOG_INFO(LABEL, "end"); } - } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp b/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp index eac34f1b6..5b41136ed 100644 --- a/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp +++ b/services/privacymanager/src/active/perm_active_status_change_callback_proxy.cpp @@ -28,7 +28,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { } PermActiveStatusChangeCallbackProxy::PermActiveStatusChangeCallbackProxy(const sptr& impl) - : IRemoteProxy(impl) { + : IRemoteProxy(impl) { } PermActiveStatusChangeCallbackProxy::~PermActiveStatusChangeCallbackProxy() @@ -37,7 +37,7 @@ PermActiveStatusChangeCallbackProxy::~PermActiveStatusChangeCallbackProxy() void PermActiveStatusChangeCallbackProxy::ActiveStatusChangeCallback(ActiveChangeResponse& result) { MessageParcel data; - data.WriteInterfaceToken(IPermActiveStatusChangeCbk::GetDescriptor()); + data.WriteInterfaceToken(IPermActiveStatusCallback::GetDescriptor()); ActiveChangeResponseParcel resultParcel; resultParcel.changeResponse = result; @@ -54,7 +54,7 @@ void PermActiveStatusChangeCallbackProxy::ActiveStatusChangeCallback(ActiveChang return; } int32_t requestResult = remote->SendRequest( - static_cast(IPermActiveStatusChangeCbk::PERM_ACTIVE_STATUS_CHANGE), data, reply, option); + static_cast(IPermActiveStatusCallback::PERM_ACTIVE_STATUS_CHANGE), data, reply, option); if (requestResult != NO_ERROR) { ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult); return; diff --git a/services/privacymanager/src/record/permission_record_manager.cpp b/services/privacymanager/src/record/permission_record_manager.cpp index cba2f296c..6d71af8ca 100644 --- a/services/privacymanager/src/record/permission_record_manager.cpp +++ b/services/privacymanager/src/record/permission_record_manager.cpp @@ -23,7 +23,7 @@ #include "field_const.h" #include "permission_record_repository.h" #include "permission_visitor_repository.h" -#include "perm_active_status_change_callback_manage.h" +#include "active_status_callback_manager.h" #include "time_util.h" #include "to_string.h" @@ -423,47 +423,13 @@ std::string PermissionRecordManager::DumpRecordInfo(const std::string& bundleNam return dumpInfo; } -static void RegisterFrontAndBackGroundlistener() -{ - // 给注册前后台监听打桩 - // 此处需要实现一个callback函数用于被注册 -} - -static void UnRegisterFrontAndBackGroundlistener() -{ - -} - -// 被注册的函数 -int32_t FrontAndBackGroundlistener( - AccessTokenID tokenID, const std::string& permissionName, int32_t statusType) -{ - PermissionVisitor visitor; - bool res = PermissionRecordManager::GetInstance().GetPermissionVisitor(tokenID, visitor); - if (!res) { - return Constant::FAILURE; - } - ActiveStatusCallbackManager::GetInstance().ExcuteCallbackAsync( - tokenID, permissionName, visitor.deviceId, static_cast(statusType)); - return Constant::SUCCESS; -} - int32_t PermissionRecordManager::StartUsingPermission(AccessTokenID tokenID, const std::string& permissionName) { - // 记录开始使用 - - // 此处是否结合全局变量的使用来判断是否开始记录 - - // 注册前后台监听 - RegisterFrontAndBackGroundlistener(); - - // 触发给systemUi当前的权限在前台使用 + // to do PermissionVisitor visitor; - bool res = GetPermissionVisitor(tokenID, visitor); - if (!res) { + if (!GetPermissionVisitor(tokenID, visitor)) { return Constant::FAILURE; } - ActiveStatusCallbackManager::GetInstance().ExcuteCallbackAsync( tokenID, permissionName, visitor.deviceId, PERM_ACTIVE_IN_FOREGROUND); return Constant::SUCCESS; @@ -471,15 +437,9 @@ int32_t PermissionRecordManager::StartUsingPermission(AccessTokenID tokenID, con int32_t PermissionRecordManager::StopUsingPermission(AccessTokenID tokenID, const std::string& permissionName) { - // 记录结束使用 - - // 注册前后台监听 - UnRegisterFrontAndBackGroundlistener(); - - // 触发给systemUi当前的权限在前台使用 + // to do PermissionVisitor visitor; - bool res = GetPermissionVisitor(tokenID, visitor); - if (!res) { + if (!GetPermissionVisitor(tokenID, visitor)) { return Constant::FAILURE; } diff --git a/services/privacymanager/src/service/privacy_manager_stub.cpp b/services/privacymanager/src/service/privacy_manager_stub.cpp index 37c1ded5c..10a767c8f 100644 --- a/services/privacymanager/src/service/privacy_manager_stub.cpp +++ b/services/privacymanager/src/service/privacy_manager_stub.cpp @@ -28,6 +28,7 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PrivacyManagerStub" }; +static const uint32_t PERM_LIST_SIZE_MAX = 1024; } int32_t PrivacyManagerStub::OnRemoteRequest( @@ -167,7 +168,7 @@ void PrivacyManagerStub::DumpRecordInfoInner(MessageParcel& data, MessageParcel& void PrivacyManagerStub::RegisterPermActiveStatusCallbackInner(MessageParcel& data, MessageParcel& reply) { uint32_t permListSize = data.ReadUint32(); - if (permListSize > 200) { + if (permListSize > PERM_LIST_SIZE_MAX) { ACCESSTOKEN_LOG_ERROR(LABEL, "read permListSize fail"); reply.WriteInt32(RET_FAILED); return; @@ -183,7 +184,6 @@ void PrivacyManagerStub::RegisterPermActiveStatusCallbackInner(MessageParcel& da reply.WriteInt32(RET_FAILED); return; } - ACCESSTOKEN_LOG_INFO(LABEL, "callback %{public}p ", (IRemoteObject *)callback); int32_t result = this->RegisterPermActiveStatusCallback(permList, callback); reply.WriteInt32(result); } @@ -196,7 +196,7 @@ void PrivacyManagerStub::UnRegisterPermActiveStatusCallbackInner(MessageParcel& reply.WriteInt32(RET_FAILED); return; } - ACCESSTOKEN_LOG_INFO(LABEL, "callback %{public}p ", (IRemoteObject *)callback); + int32_t result = this->UnRegisterPermActiveStatusCallback(callback); reply.WriteInt32(result); } -- Gitee