1 /* 2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef CAMERA_PRIVACY_H 16 #define CAMERA_PRIVACY_H 17 18 #include <refbase.h> 19 #include <memory> 20 #include "camera_util.h" 21 #include "accesstoken_kit.h" 22 #include "perm_state_change_callback_customize.h" 23 #include "privacy_error.h" 24 #include "privacy_kit.h" 25 #include "state_customized_cbk.h" 26 27 namespace OHOS { 28 namespace CameraStandard { 29 static const int32_t WAIT_RELEASE_STREAM_MS = 500; // 500ms 30 class HCameraDevice; 31 class PermissionStatusChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { 32 public: PermissionStatusChangeCb(const Security::AccessToken::PermStateChangeScope & scopeInfo)33 explicit PermissionStatusChangeCb(const Security::AccessToken::PermStateChangeScope& scopeInfo) 34 : PermStateChangeCallbackCustomize(scopeInfo) {} 35 virtual ~PermissionStatusChangeCb() = default; 36 void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result) override; 37 }; 38 39 class CameraUseStateChangeCb : public Security::AccessToken::StateCustomizedCbk { 40 public: CameraUseStateChangeCb()41 explicit CameraUseStateChangeCb() {} 42 virtual ~CameraUseStateChangeCb() = default; 43 void StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing) override; 44 }; 45 46 class CameraPrivacy : public RefBase { 47 public: CameraPrivacy(uint32_t callingTokenId,int32_t pid)48 explicit CameraPrivacy(uint32_t callingTokenId, int32_t pid) : pid_(pid), callerToken_(callingTokenId) {} 49 ~CameraPrivacy(); 50 bool RegisterPermissionCallback(); 51 void UnregisterPermissionCallback(); 52 bool StartUsingPermissionCallback(); 53 void StopUsingPermissionCallback(); 54 bool AddCameraPermissionUsedRecord(); 55 bool IsAllowUsingCamera(); 56 WaitFor()57 inline std::cv_status WaitFor() 58 { 59 std::unique_lock<std::mutex> lock(canCloseMutex_); 60 return canClose_.wait_for(lock, std::chrono::milliseconds(WAIT_RELEASE_STREAM_MS)); 61 } 62 Notify()63 inline void Notify() 64 { 65 std::lock_guard<std::mutex> lock(canCloseMutex_); 66 canClose_.notify_one(); 67 } 68 69 private: 70 int32_t pid_; 71 uint32_t callerToken_; 72 std::condition_variable canClose_; 73 std::mutex canCloseMutex_; 74 std::mutex permissionCbMutex_; 75 std::mutex cameraUseCbMutex_; 76 std::shared_ptr<PermissionStatusChangeCb> permissionCallbackPtr_; 77 std::shared_ptr<CameraUseStateChangeCb> cameraUseCallbackPtr_; 78 }; 79 } // namespace CameraStandard 80 } // namespace OHOS 81 #endif // CAMERA_PRIVACY_H