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