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(wptr<HCameraDevice> device,const Security::AccessToken::PermStateChangeScope & scopeInfo)33     explicit PermissionStatusChangeCb(
34         wptr<HCameraDevice> device, const Security::AccessToken::PermStateChangeScope& scopeInfo)
35         : PermStateChangeCallbackCustomize(scopeInfo), cameraDevice_(device)
36     {}
37     virtual ~PermissionStatusChangeCb() = default;
38     void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result) override;
39 
40 private:
41     wptr<HCameraDevice> cameraDevice_;
42 };
43 
44 class CameraUseStateChangeCb : public Security::AccessToken::StateCustomizedCbk {
45 public:
CameraUseStateChangeCb(wptr<HCameraDevice> device)46     explicit CameraUseStateChangeCb(wptr<HCameraDevice> device) : cameraDevice_(device) {}
47     virtual ~CameraUseStateChangeCb() = default;
48     void StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing) override;
49 
50 private:
51     wptr<HCameraDevice> cameraDevice_;
52 };
53 
54 class CameraPrivacy : public RefBase {
55 public:
CameraPrivacy(wptr<HCameraDevice> device,uint32_t callingTokenId,int32_t pid)56     explicit CameraPrivacy(wptr<HCameraDevice> device, uint32_t callingTokenId, int32_t pid)
57         : pid_(pid), callerToken_(callingTokenId), cameraDevice_(device) {}
58     ~CameraPrivacy();
59     bool RegisterPermissionCallback();
60     void UnregisterPermissionCallback();
61     bool StartUsingPermissionCallback();
62     void StopUsingPermissionCallback();
63     bool AddCameraPermissionUsedRecord();
64     bool IsAllowUsingCamera();
65 
WaitFor()66     inline std::cv_status WaitFor()
67     {
68         std::unique_lock<std::mutex> lock(canCloseMutex_);
69         return canClose_.wait_for(lock, std::chrono::milliseconds(WAIT_RELEASE_STREAM_MS));
70     }
71 
Notify()72     inline void Notify()
73     {
74         std::lock_guard<std::mutex> lock(canCloseMutex_);
75         canClose_.notify_one();
76     }
77 
78 private:
79     int32_t pid_;
80     uint32_t callerToken_;
81     wptr<HCameraDevice> cameraDevice_;
82     std::condition_variable canClose_;
83     std::mutex canCloseMutex_;
84     std::mutex permissionCbMutex_;
85     std::mutex cameraUseCbMutex_;
86     std::shared_ptr<PermissionStatusChangeCb> permissionCallbackPtr_;
87     std::shared_ptr<CameraUseStateChangeCb> cameraUseCallbackPtr_;
88 };
89 } // namespace CameraStandard
90 } // namespace OHOS
91 #endif // CAMERA_PRIVACY_H