From b6b81519db014be89e4af599dd0d6261b358c913 Mon Sep 17 00:00:00 2001 From: cold_yixiu Date: Wed, 28 Sep 2022 11:52:08 +0800 Subject: [PATCH] add permission listener Signed-off-by: cold_yixiu --- services/camera_service/BUILD.gn | 1 + .../camera_service/include/hcapture_session.h | 10 ++++- .../include/permission_status_change_cb.h | 38 +++++++++++++++++++ .../camera_service/src/hcamera_service.cpp | 3 +- .../camera_service/src/hcapture_session.cpp | 38 ++++++++++++++++++- .../src/permission_status_change_cb.cpp | 31 +++++++++++++++ 6 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 services/camera_service/include/permission_status_change_cb.h create mode 100644 services/camera_service/src/permission_status_change_cb.cpp diff --git a/services/camera_service/BUILD.gn b/services/camera_service/BUILD.gn index e575fc1..0cd44c7 100644 --- a/services/camera_service/BUILD.gn +++ b/services/camera_service/BUILD.gn @@ -37,6 +37,7 @@ ohos_shared_library("camera_service") { "src/hstream_common.cpp", "src/hstream_metadata.cpp", "src/hstream_repeat.cpp", + "src/permission_status_change_cb.cpp", ] cflags = [ "-fPIC", diff --git a/services/camera_service/include/hcapture_session.h b/services/camera_service/include/hcapture_session.h index b165ac0..dcef4bf 100644 --- a/services/camera_service/include/hcapture_session.h +++ b/services/camera_service/include/hcapture_session.h @@ -23,6 +23,7 @@ #include "hstream_repeat.h" #include "v1_0/istream_operator_callback.h" #include "v1_0/istream_operator.h" +#include "permission_status_change_cb.h" #include #include @@ -31,6 +32,7 @@ namespace OHOS { namespace CameraStandard { using namespace OHOS::HDI::Camera::V1_0; class StreamOperatorCallback; +class PermissionStatusChangeCb; enum class CaptureSessionState { SESSION_INIT = 0, @@ -39,10 +41,11 @@ enum class CaptureSessionState { }; static const int32_t STREAMID_BEGIN = 1; +const std::string ACCESS_CAMERA = "ohos.permission.CAMERA"; class HCaptureSession : public HCaptureSessionStub { public: - HCaptureSession(sptr cameraHostManager, sptr streamOperatorCb); + HCaptureSession(sptr cameraHostManager, sptr streamOperatorCb, const uint32_t callingTokenId); ~HCaptureSession(); int32_t BeginConfig() override; @@ -57,6 +60,7 @@ public: int32_t Start() override; int32_t Stop() override; int32_t Release(pid_t pid) override; + int32_t ReleaseInner(); static void DestroyStubObjectForPid(pid_t pid); int32_t SetCallback(sptr &callback) override; @@ -88,6 +92,8 @@ private: void RestorePreviousState(sptr &device, bool isCreateReleaseStreams); void ReleaseStreams(); void ClearCaptureSession(pid_t pid); + void RegisterPermissionCallback(const uint32_t callingTokenId, const std::string permissionName); + void UnregisterPermissionCallback(const uint32_t callingTokenId); std::string GetSessionState(); CaptureSessionState curState_ = CaptureSessionState::SESSION_INIT; @@ -108,6 +114,8 @@ private: std::map sessionState_; pid_t pid_; int32_t uid_; + uint32_t callerToken_; + std::shared_ptr callbackPtr_; }; class StreamOperatorCallback : public IStreamOperatorCallback { diff --git a/services/camera_service/include/permission_status_change_cb.h b/services/camera_service/include/permission_status_change_cb.h new file mode 100644 index 0000000..e11cb04 --- /dev/null +++ b/services/camera_service/include/permission_status_change_cb.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PERMISSION_STATUS_CHANGE_CB_H +#define PERMISSION_STATUS_CHANGE_CB_H + +#include "accesstoken_kit.h" +#include "perm_state_change_callback_customize.h" +#include "hcapture_session.h" + +namespace OHOS { +namespace CameraStandard { +using namespace Security::AccessToken; +class HCaptureSession; +class PermissionStatusChangeCb : public PermStateChangeCallbackCustomize { +public: + explicit PermissionStatusChangeCb(const PermStateChangeScope &scopeInfo) + : PermStateChangeCallbackCustomize(scopeInfo) {} + ~PermissionStatusChangeCb() {} + void PermStateChangeCallback(PermStateChangeInfo& result) override; + + sptr curCaptureSession; +}; +} // namespace CameraStandard +} // namespace OHOS +#endif // PERMISSION_STATUS_CHANGE_CB_H \ No newline at end of file diff --git a/services/camera_service/src/hcamera_service.cpp b/services/camera_service/src/hcamera_service.cpp index 097f0c9..bbac486 100644 --- a/services/camera_service/src/hcamera_service.cpp +++ b/services/camera_service/src/hcamera_service.cpp @@ -178,7 +178,8 @@ int32_t HCameraService::CreateCaptureSession(sptr &session) } std::lock_guard lock(mutex_); - captureSession = new(std::nothrow) HCaptureSession(cameraHostManager_, streamOperatorCallback_); + OHOS::Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID(); + captureSession = new(std::nothrow) HCaptureSession(cameraHostManager_, streamOperatorCallback_, callerToken); if (captureSession == nullptr) { MEDIA_ERR_LOG("HCameraService::CreateCaptureSession HCaptureSession allocation failed"); return CAMERA_ALLOC_ERROR; diff --git a/services/camera_service/src/hcapture_session.cpp b/services/camera_service/src/hcapture_session.cpp index 33ba973..f9392ce 100644 --- a/services/camera_service/src/hcapture_session.cpp +++ b/services/camera_service/src/hcapture_session.cpp @@ -64,7 +64,7 @@ static std::string GetClientBundle(int uid) } HCaptureSession::HCaptureSession(sptr cameraHostManager, - sptr streamOperatorCb) + sptr streamOperatorCb, const uint32_t callingTokenId) : cameraHostManager_(cameraHostManager), streamOperatorCallback_(streamOperatorCb), sessionCallback_(nullptr) { @@ -97,6 +97,8 @@ HCaptureSession::HCaptureSession(sptr cameraHostManager, } else { session_[pid_] = this; } + callerToken_ = callingTokenId; + RegisterPermissionCallback(callingTokenId, ACCESS_CAMERA); MEDIA_DEBUG_LOG("HCaptureSession: camera stub services(%{public}zu).", session_.size()); } @@ -674,6 +676,11 @@ void HCaptureSession::ReleaseStreams() } } +int32_t HCaptureSession::ReleaseInner() +{ + return Release(pid_); +} + int32_t HCaptureSession::Release(pid_t pid) { std::lock_guard lock(sessionLock_); @@ -697,6 +704,35 @@ int32_t HCaptureSession::Release(pid_t pid) return CAMERA_OK; } +void HCaptureSession::RegisterPermissionCallback(const uint32_t callingTokenId, const std::string permissionName) +{ + PermStateChangeScope scopeInfo; + scopeInfo.permList = {permissionName}; + scopeInfo.tokenIDs = {callingTokenId}; + callbackPtr_ = std::make_shared(scopeInfo); + callbackPtr_->curCaptureSession = this; + MEDIA_DEBUG_LOG("after tokenId:%{public}d register", callingTokenId); + int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr_); + if (res != CAMERA_OK) { + MEDIA_ERR_LOG("RegisterPermStateChangeCallback failed."); + } +} + +// will not call unregister method because access system has deadlock +// will improve it after access system fix deadlock. +void HCaptureSession::UnregisterPermissionCallback(const uint32_t callingTokenId) +{ + if (callbackPtr_ == nullptr) { + MEDIA_ERR_LOG("callbackPtr_ is null."); + return; + } + int32_t res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr_); + if (res != CAMERA_OK) { + MEDIA_ERR_LOG("UnRegisterPermStateChangeCallback failed."); + } + MEDIA_DEBUG_LOG("after tokenId:%{public}d unregister", callingTokenId); +} + void HCaptureSession::DestroyStubObjectForPid(pid_t pid) { MEDIA_DEBUG_LOG("camera stub services(%{public}zu) pid(%{public}d).", session_.size(), pid); diff --git a/services/camera_service/src/permission_status_change_cb.cpp b/services/camera_service/src/permission_status_change_cb.cpp new file mode 100644 index 0000000..31872c4 --- /dev/null +++ b/services/camera_service/src/permission_status_change_cb.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "permission_status_change_cb.h" +#include "camera_util.h" +#include "camera_log.h" + +namespace OHOS { +namespace CameraStandard { +void PermissionStatusChangeCb::PermStateChangeCallback(PermStateChangeInfo& result) +{ + MEDIA_DEBUG_LOG("%{public}s changed.", result.permissionName.c_str()); + if ((result.PermStateChangeType == 0) && (curCaptureSession != nullptr)) + { + curCaptureSession->ReleaseInner(); + } +} +} // namespace CameraStandard +} // namespace OHOS \ No newline at end of file -- Gitee