1 /*
2 * Copyright (c) 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 #include "camera_manager_privacy_client.h"
16
17 #include "accesstoken_log.h"
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "CameraManagerPrivacyClient"
27 };
28 } // namespace
29
GetInstance()30 CameraManagerPrivacyClient& CameraManagerPrivacyClient::GetInstance()
31 {
32 static CameraManagerPrivacyClient instance;
33 return instance;
34 }
35
CameraManagerPrivacyClient()36 CameraManagerPrivacyClient::CameraManagerPrivacyClient()
37 {}
38
~CameraManagerPrivacyClient()39 CameraManagerPrivacyClient::~CameraManagerPrivacyClient()
40 {}
41
SetMuteCallback(const sptr<ICameraMuteServiceCallback> & callback)42 int32_t CameraManagerPrivacyClient::SetMuteCallback(const sptr<ICameraMuteServiceCallback>& callback)
43 {
44 auto proxy = GetProxy();
45 if (proxy == nullptr) {
46 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
47 return -1;
48 }
49 return proxy->SetMuteCallback(callback);
50 }
51
MuteCamera(bool muteMode)52 int32_t CameraManagerPrivacyClient::MuteCamera(bool muteMode)
53 {
54 auto proxy = GetProxy();
55 if (proxy == nullptr) {
56 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
57 return false;
58 }
59 return proxy->MuteCamera(muteMode);
60 }
61
IsCameraMuted()62 bool CameraManagerPrivacyClient::IsCameraMuted()
63 {
64 auto proxy = GetProxy();
65 if (proxy == nullptr) {
66 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
67 return false;
68 }
69 bool muteMode = false;
70 proxy->IsCameraMuted(muteMode);
71 return muteMode;
72 }
73
InitProxy()74 void CameraManagerPrivacyClient::InitProxy()
75 {
76 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77 if (sam == nullptr) {
78 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
79 return;
80 }
81 auto cameraManagerSa = sam->GetSystemAbility(CAMERA_SERVICE_ID);
82 if (cameraManagerSa == nullptr) {
83 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
84 CAMERA_SERVICE_ID);
85 return;
86 }
87
88 serviceDeathObserver_ = new (std::nothrow) CameraManagerPrivacyDeathRecipient();
89 if (serviceDeathObserver_ != nullptr) {
90 cameraManagerSa->AddDeathRecipient(serviceDeathObserver_);
91 }
92
93 proxy_ = iface_cast<ICameraService>(cameraManagerSa);
94 if (proxy_ == nullptr) {
95 ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
96 }
97 }
98
OnRemoteDiedHandle()99 void CameraManagerPrivacyClient::OnRemoteDiedHandle()
100 {
101 std::lock_guard<std::mutex> lock(proxyMutex_);
102 proxy_ = nullptr;
103 }
104
GetProxy()105 sptr<ICameraService> CameraManagerPrivacyClient::GetProxy()
106 {
107 std::lock_guard<std::mutex> lock(proxyMutex_);
108 if (proxy_ == nullptr) {
109 InitProxy();
110 }
111 return proxy_;
112 }
113 } // namespace AccessToken
114 } // namespace Security
115 } // namespace OHOS
116
117