• 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_RETURN_RET_ELOG(streamOpCb == nullptr, nullptr, "streamOpCb is nullptr");
35     auto streamOpCbSptr = streamOpCb.promote();
36     CHECK_RETURN_RET_ELOG(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_RETURN_ELOG((isShowing == true) || (device == nullptr), "abnormal callback from privacy.");
75         auto cameraPrivacy = device->GetCameraPrivacy();
76         CHECK_RETURN_ELOG(cameraPrivacy == nullptr, "cameraPrivacy is nullptr.");
77         bool isSystemCamera = (cameraPrivacy->GetClientName().find(CAMERA_TAG) != std::string::npos)
78             && CheckSystemApp();
79         MEDIA_INFO_LOG("CameraUseStateChangeCb::StateChangeNotify ClientName: %{public}s, isSystemCamera: %{public}d",
80             cameraPrivacy->GetClientName().c_str(), isSystemCamera);
81         if (cameraPrivacy->WaitFor(isSystemCamera) == std::cv_status::timeout) {
82             MEDIA_INFO_LOG("CameraUseStateChangeCb::StateChangeNotify wait timeout");
83             bool isForeground = CameraAppManagerUtils::IsForegroundApplication(tokenId);
84             if ((isShowing == false) && (device != nullptr) && !isForeground) {
85                 auto session = CastToSession(device->GetStreamOperatorCallback());
86                 if (session) {
87                     session->ReleaseStreams();
88                     session->StopMovingPhoto();
89                 }
90                 device->CloseDevice();
91             }
92         }
93     }
94 }
95 
~CameraPrivacy()96 CameraPrivacy::~CameraPrivacy()
97 {
98     cameraUseCallbackPtr_ = nullptr;
99     permissionCallbackPtr_ = nullptr;
100 }
101 
IsAllowUsingCamera()102 bool CameraPrivacy::IsAllowUsingCamera()
103 {
104     return PrivacyKit::IsAllowedUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA);
105 }
106 
SetClientName(std::string clientName)107 void CameraPrivacy::SetClientName(std::string clientName)
108 {
109     clientName_ = clientName;
110 }
111 
RegisterPermissionCallback()112 bool CameraPrivacy::RegisterPermissionCallback()
113 {
114     Security::AccessToken::PermStateChangeScope scopeInfo;
115     scopeInfo.permList = {OHOS_PERMISSION_CAMERA};
116     scopeInfo.tokenIDs = {callerToken_};
117     int32_t res;
118     {
119         std::lock_guard<std::mutex> lock(permissionCbMutex_);
120         permissionCallbackPtr_ = std::make_shared<PermissionStatusChangeCb>(scopeInfo);
121         res = AccessTokenKit::RegisterPermStateChangeCallback(permissionCallbackPtr_);
122     }
123     MEDIA_INFO_LOG("CameraPrivacy::RegisterPermissionCallback res:%{public}d", res);
124     CHECK_PRINT_ELOG(res != CAMERA_OK, "RegisterPermissionCallback failed.");
125     return res == CAMERA_OK;
126 }
127 
UnregisterPermissionCallback()128 void CameraPrivacy::UnregisterPermissionCallback()
129 {
130     std::lock_guard<std::mutex> lock(permissionCbMutex_);
131     CHECK_RETURN_ELOG(permissionCallbackPtr_ == nullptr, "permissionCallbackPtr_ is null.");
132     MEDIA_DEBUG_LOG("UnregisterPermissionCallback unregister");
133     int32_t res = AccessTokenKit::UnRegisterPermStateChangeCallback(permissionCallbackPtr_);
134     MEDIA_INFO_LOG("CameraPrivacy::UnregisterPermissionCallback res:%{public}d", res);
135     CHECK_PRINT_ELOG(res != CAMERA_OK, "UnregisterPermissionCallback failed.");
136     permissionCallbackPtr_ = nullptr;
137 }
138 
AddCameraPermissionUsedRecord()139 bool CameraPrivacy::AddCameraPermissionUsedRecord()
140 {
141     int32_t successCout = 1;
142     int32_t failCount = 0;
143     int32_t res = PrivacyKit::AddPermissionUsedRecord(callerToken_, OHOS_PERMISSION_CAMERA, successCout, failCount);
144     MEDIA_INFO_LOG("CameraPrivacy::AddCameraPermissionUsedRecord res:%{public}d", res);
145     CHECK_PRINT_ELOG(res != CAMERA_OK, "AddCameraPermissionUsedRecord failed.");
146     return res == CAMERA_OK;
147 }
148 
StartUsingPermissionCallback()149 bool CameraPrivacy::StartUsingPermissionCallback()
150 {
151     int32_t res;
152     {
153         std::lock_guard<std::mutex> lock(cameraUseCbMutex_);
154         CHECK_RETURN_RET_ELOG(cameraUseCallbackPtr_, true, "has StartUsingPermissionCallback!");
155         cameraUseCallbackPtr_ = std::make_shared<CameraUseStateChangeCb>();
156         res = PrivacyKit::StartUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA, cameraUseCallbackPtr_, pid_);
157     }
158     MEDIA_INFO_LOG("CameraPrivacy::StartUsingPermissionCallback res:%{public}d", res);
159     bool ret = (res == CAMERA_OK || res == Security::AccessToken::ERR_EDM_POLICY_CHECK_FAILED ||
160         res == Security::AccessToken::ERR_PRIVACY_POLICY_CHECK_FAILED);
161     CHECK_PRINT_ELOG(!ret, "StartUsingPermissionCallback failed.");
162     return ret;
163 }
164 
StopUsingPermissionCallback()165 void CameraPrivacy::StopUsingPermissionCallback()
166 {
167     MEDIA_INFO_LOG("CameraPrivacy::StopUsingPermissionCallback is called, pid_: %{public}d", pid_);
168     {
169         std::lock_guard<std::mutex> lock(cameraUseCbMutex_);
170         int32_t res = PrivacyKit::StopUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA);
171         MEDIA_INFO_LOG("CameraPrivacy::StopUsingPermissionCallback res:%{public}d", res);
172         CHECK_PRINT_ELOG(res != CAMERA_OK, "StopUsingPermissionCallback failed.");
173         cameraUseCallbackPtr_ = nullptr;
174     }
175     Notify();
176 }
177 } // namespace CameraStandard
178 } // namespace OHOS