• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
16 #include <condition_variable>
17 #include <cstdint>
18 #include <mutex>
19 #include "camera_app_manager_utils.h"
20 #include "camera_privacy.h"
21 #include "camera_log.h"
22 #include "hcamera_device.h"
23 #include "hcamera_device_manager.h"
24 #include "hcapture_session.h"
25 #include "hstream_operator.h"
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 using OHOS::Security::AccessToken::PrivacyKit;
30 using OHOS::Security::AccessToken::AccessTokenKit;
31 
CastToSession(wptr<IStreamOperatorCallback> streamOpCb)32 sptr<HStreamOperator> CastToSession(wptr<IStreamOperatorCallback> streamOpCb)
33 {
34     CHECK_ERROR_RETURN_RET_LOG(streamOpCb == nullptr, nullptr, "streamOpCb is nullptr");
35     auto streamOpCbSptr = streamOpCb.promote();
36     CHECK_ERROR_RETURN_RET_LOG(streamOpCbSptr == nullptr, nullptr, "streamOpCbWptr is nullptr");
37 
38     return static_cast<HStreamOperator*>(streamOpCbSptr.GetRefPtr());
39 }
40 
PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo & result)41 void PermissionStatusChangeCb::PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result)
42 {
43     MEDIA_INFO_LOG("enter CameraUseStateChangeNotify permStateChangeType:%{public}d"
44         " permissionName:%{public}s", result.permStateChangeType, result.permissionName.c_str());
45     std::vector<sptr<HCameraDeviceHolder>> holders = HCameraDeviceManager::GetInstance()->GetActiveCameraHolders();
46     for (auto holder : holders) {
47         if (holder->GetAccessTokenId() != result.tokenID) {
48             MEDIA_INFO_LOG("PermissionStatusChangeCb::PermStateChangeCallback not current tokenId, checking continue");
49             continue;
50         }
51         auto device = holder->GetDevice();
52         if ((result.permStateChangeType == 0) && (device != nullptr)) {
53             auto session = CastToSession(device->GetStreamOperatorCallback());
54             if (session) {
55                 session->ReleaseStreams();
56                 session->StopMovingPhoto();
57             }
58             device->CloseDevice();
59             device->OnError(DEVICE_PREEMPT, 0);
60         }
61     }
62 }
63 
StateChangeNotify(Security::AccessToken::AccessTokenID tokenId,bool isShowing)64 void CameraUseStateChangeCb::StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing)
65 {
66     MEDIA_INFO_LOG("CameraUseStateChangeCb::StateChangeNotify is called, isShowing: %{public}d", isShowing);
67     std::vector<sptr<HCameraDeviceHolder>> holders = HCameraDeviceManager::GetInstance()->GetActiveCameraHolders();
68     for (auto holder : holders) {
69         if (holder->GetAccessTokenId() != tokenId) {
70             MEDIA_INFO_LOG("CameraUseStateChangeCb::StateChangeNotify not current tokenId, checking continue");
71             continue;
72         }
73         auto device = holder->GetDevice();
74         CHECK_ERROR_RETURN_LOG((isShowing == true) || (device == nullptr), "abnormal callback from privacy.");
75         auto cameraPrivacy = device->GetCameraPrivacy();
76         CHECK_ERROR_RETURN_LOG(cameraPrivacy == nullptr, "cameraPrivacy is nullptr.");
77         if (cameraPrivacy->WaitFor() == std::cv_status::timeout) {
78             MEDIA_INFO_LOG("CameraUseStateChangeCb::StateChangeNotify wait timeout");
79             bool isForeground = CameraAppManagerUtils::IsForegroundApplication(tokenId);
80             if ((isShowing == false) && (device != nullptr) && !isForeground) {
81                 auto session = CastToSession(device->GetStreamOperatorCallback());
82                 if (session) {
83                     session->ReleaseStreams();
84                     session->StopMovingPhoto();
85                 }
86                 device->CloseDevice();
87             }
88         }
89     }
90 }
91 
~CameraPrivacy()92 CameraPrivacy::~CameraPrivacy()
93 {
94     cameraUseCallbackPtr_ = nullptr;
95     permissionCallbackPtr_ = nullptr;
96 }
97 
IsAllowUsingCamera()98 bool CameraPrivacy::IsAllowUsingCamera()
99 {
100     return PrivacyKit::IsAllowedUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA);
101 }
102 
RegisterPermissionCallback()103 bool CameraPrivacy::RegisterPermissionCallback()
104 {
105     Security::AccessToken::PermStateChangeScope scopeInfo;
106     scopeInfo.permList = {OHOS_PERMISSION_CAMERA};
107     scopeInfo.tokenIDs = {callerToken_};
108     int32_t res;
109     {
110         std::lock_guard<std::mutex> lock(permissionCbMutex_);
111         permissionCallbackPtr_ = std::make_shared<PermissionStatusChangeCb>(scopeInfo);
112         res = AccessTokenKit::RegisterPermStateChangeCallback(permissionCallbackPtr_);
113     }
114     MEDIA_INFO_LOG("CameraPrivacy::RegisterPermissionCallback res:%{public}d", res);
115     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "RegisterPermissionCallback failed.");
116     return res == CAMERA_OK;
117 }
118 
UnregisterPermissionCallback()119 void CameraPrivacy::UnregisterPermissionCallback()
120 {
121     std::lock_guard<std::mutex> lock(permissionCbMutex_);
122     CHECK_ERROR_RETURN_LOG(permissionCallbackPtr_ == nullptr, "permissionCallbackPtr_ is null.");
123     MEDIA_DEBUG_LOG("UnregisterPermissionCallback unregister");
124     int32_t res = AccessTokenKit::UnRegisterPermStateChangeCallback(permissionCallbackPtr_);
125     MEDIA_INFO_LOG("CameraPrivacy::UnregisterPermissionCallback res:%{public}d", res);
126     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "UnregisterPermissionCallback failed.");
127     permissionCallbackPtr_ = nullptr;
128 }
129 
AddCameraPermissionUsedRecord()130 bool CameraPrivacy::AddCameraPermissionUsedRecord()
131 {
132     int32_t successCout = 1;
133     int32_t failCount = 0;
134     int32_t res = PrivacyKit::AddPermissionUsedRecord(callerToken_, OHOS_PERMISSION_CAMERA, successCout, failCount);
135     MEDIA_INFO_LOG("CameraPrivacy::AddCameraPermissionUsedRecord res:%{public}d", res);
136     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "AddCameraPermissionUsedRecord failed.");
137     return res == CAMERA_OK;
138 }
139 
StartUsingPermissionCallback()140 bool CameraPrivacy::StartUsingPermissionCallback()
141 {
142     int32_t res;
143     {
144         std::lock_guard<std::mutex> lock(cameraUseCbMutex_);
145         CHECK_ERROR_RETURN_RET_LOG(cameraUseCallbackPtr_, true, "has StartUsingPermissionCallback!");
146         cameraUseCallbackPtr_ = std::make_shared<CameraUseStateChangeCb>();
147         res = PrivacyKit::StartUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA, cameraUseCallbackPtr_, pid_);
148     }
149     MEDIA_INFO_LOG("CameraPrivacy::StartUsingPermissionCallback res:%{public}d", res);
150     bool ret = (res == CAMERA_OK || res == Security::AccessToken::ERR_EDM_POLICY_CHECK_FAILED ||
151         res == Security::AccessToken::ERR_PRIVACY_POLICY_CHECK_FAILED);
152     CHECK_ERROR_PRINT_LOG(!ret, "StartUsingPermissionCallback failed.");
153     return ret;
154 }
155 
StopUsingPermissionCallback()156 void CameraPrivacy::StopUsingPermissionCallback()
157 {
158     MEDIA_INFO_LOG("CameraPrivacy::StopUsingPermissionCallback is called, pid_: %{public}d", pid_);
159     {
160         std::lock_guard<std::mutex> lock(cameraUseCbMutex_);
161         int32_t res = PrivacyKit::StopUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA, pid_);
162         MEDIA_INFO_LOG("CameraPrivacy::StopUsingPermissionCallback res:%{public}d", res);
163         CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "StopUsingPermissionCallback failed.");
164         cameraUseCallbackPtr_ = nullptr;
165     }
166     Notify();
167 }
168 } // namespace CameraStandard
169 } // namespace OHOS