• 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 "camera_privacy.h"
17 #include "camera_log.h"
18 #include "hcamera_device.h"
19 #include "hcapture_session.h"
20 #include "types.h"
21 
22 namespace OHOS {
23 namespace CameraStandard {
24 using OHOS::Security::AccessToken::PrivacyKit;
25 using OHOS::Security::AccessToken::AccessTokenKit;
26 
CastToSession(sptr<IStreamOperatorCallback> streamOpCb)27 sptr<HCaptureSession> CastToSession(sptr<IStreamOperatorCallback> streamOpCb)
28 {
29     if (streamOpCb == nullptr) {
30         return nullptr;
31     }
32     return static_cast<HCaptureSession*>(streamOpCb.GetRefPtr());
33 }
34 
PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo & result)35 void PermissionStatusChangeCb::PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result)
36 {
37     MEDIA_INFO_LOG("enter PermissionStatusChangeNotify permStateChangeType:%{public}d"
38         " permissionName:%{public}s", result.permStateChangeType, result.permissionName.c_str());
39     auto device = cameraDevice_.promote();
40     if ((result.permStateChangeType == 0) && (device != nullptr)) {
41         auto session = CastToSession(device->GetStreamOperatorCallback());
42         if (session) {
43             session->ReleaseStreams();
44             session->StopMovingPhoto();
45         }
46         device->CloseDevice();
47         device->OnError(DEVICE_PREEMPT, 0);
48     }
49 }
50 
StateChangeNotify(Security::AccessToken::AccessTokenID tokenId,bool isShowing)51 void CameraUseStateChangeCb::StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing)
52 {
53     MEDIA_INFO_LOG("enter CameraUseStateChangeNotify");
54     auto device = cameraDevice_.promote();
55     if ((isShowing == false) && (device != nullptr)) {
56         auto session = CastToSession(device->GetStreamOperatorCallback());
57         if (session) {
58             session->ReleaseStreams();
59             session->StopMovingPhoto();
60         }
61         device->CloseDevice();
62     }
63 }
64 
~CameraPrivacy()65 CameraPrivacy::~CameraPrivacy()
66 {
67     cameraUseCallbackPtr_ = nullptr;
68     permissionCallbackPtr_ = nullptr;
69 }
70 
IsAllowUsingCamera()71 bool CameraPrivacy::IsAllowUsingCamera()
72 {
73     return PrivacyKit::IsAllowedUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA);
74 }
75 
RegisterPermissionCallback()76 bool CameraPrivacy::RegisterPermissionCallback()
77 {
78     Security::AccessToken::PermStateChangeScope scopeInfo;
79     scopeInfo.permList = {OHOS_PERMISSION_CAMERA};
80     scopeInfo.tokenIDs = {callerToken_};
81     permissionCallbackPtr_ = std::make_shared<PermissionStatusChangeCb>(cameraDevice_, scopeInfo);
82     int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(permissionCallbackPtr_);
83     MEDIA_INFO_LOG("CameraPrivacy::RegisterPermissionCallback res:%{public}d", res);
84     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "RegisterPermissionCallback failed.");
85     return res == CAMERA_OK;
86 }
87 
UnregisterPermissionCallback()88 void CameraPrivacy::UnregisterPermissionCallback()
89 {
90     CHECK_ERROR_RETURN_LOG(permissionCallbackPtr_ == nullptr, "permissionCallbackPtr_ is null.");
91     MEDIA_DEBUG_LOG("UnregisterPermissionCallback unregister");
92     int32_t res = AccessTokenKit::UnRegisterPermStateChangeCallback(permissionCallbackPtr_);
93     MEDIA_INFO_LOG("CameraPrivacy::UnregisterPermissionCallback res:%{public}d", res);
94     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "UnregisterPermissionCallback failed.");
95     permissionCallbackPtr_ = nullptr;
96 }
97 
AddCameraPermissionUsedRecord()98 bool CameraPrivacy::AddCameraPermissionUsedRecord()
99 {
100     int32_t successCout = 1;
101     int32_t failCount = 0;
102     int32_t res = PrivacyKit::AddPermissionUsedRecord(callerToken_, OHOS_PERMISSION_CAMERA, successCout, failCount);
103     MEDIA_INFO_LOG("CameraPrivacy::AddCameraPermissionUsedRecord res:%{public}d", res);
104     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "AddCameraPermissionUsedRecord failed.");
105     return res == CAMERA_OK;
106 }
107 
StartUsingPermissionCallback()108 bool CameraPrivacy::StartUsingPermissionCallback()
109 {
110     CHECK_ERROR_RETURN_RET_LOG(cameraUseCallbackPtr_, true, "has StartUsingPermissionCallback!");
111     cameraUseCallbackPtr_ = std::make_shared<CameraUseStateChangeCb>(cameraDevice_);
112     int32_t res = PrivacyKit::StartUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA, cameraUseCallbackPtr_, pid_);
113     MEDIA_INFO_LOG("CameraPrivacy::StartUsingPermissionCallback res:%{public}d", res);
114     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "StartUsingPermissionCallback failed.");
115     return res == CAMERA_OK;
116 }
117 
StopUsingPermissionCallback()118 void CameraPrivacy::StopUsingPermissionCallback()
119 {
120     int32_t res = PrivacyKit::StopUsingPermission(callerToken_, OHOS_PERMISSION_CAMERA, pid_);
121     MEDIA_INFO_LOG("CameraPrivacy::StopUsingPermissionCallback res:%{public}d", res);
122     CHECK_ERROR_PRINT_LOG(res != CAMERA_OK, "StopUsingPermissionCallback failed.");
123     cameraUseCallbackPtr_ = nullptr;
124 }
125 } // namespace CameraStandard
126 } // namespace OHOS