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