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 std::recursive_mutex g_instanceMutex;
29 } // namespace
30
GetInstance()31 CameraManagerPrivacyClient& CameraManagerPrivacyClient::GetInstance()
32 {
33 static CameraManagerPrivacyClient* instance = nullptr;
34 if (instance == nullptr) {
35 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
36 if (instance == nullptr) {
37 instance = new CameraManagerPrivacyClient();
38 }
39 }
40 return *instance;
41 }
42
CameraManagerPrivacyClient()43 CameraManagerPrivacyClient::CameraManagerPrivacyClient()
44 {}
45
~CameraManagerPrivacyClient()46 CameraManagerPrivacyClient::~CameraManagerPrivacyClient()
47 {
48 std::lock_guard<std::mutex> lock(proxyMutex_);
49 ReleaseProxy();
50 }
51
MuteCameraPersist(PolicyType policyType,bool muteMode)52 int32_t CameraManagerPrivacyClient::MuteCameraPersist(PolicyType policyType, bool muteMode)
53 {
54 auto proxy = GetProxy();
55 if (proxy == nullptr) {
56 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
57 return -1;
58 }
59 return proxy->MuteCameraPersist(policyType, 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_ = sptr<CameraManagerPrivacyDeathRecipient>::MakeSptr();
89 if (serviceDeathObserver_ != nullptr) {
90 cameraManagerSa->AddDeathRecipient(serviceDeathObserver_);
91 }
92
93 proxy_ = new CameraManagerPrivacyProxy(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 ReleaseProxy();
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
ReleaseProxy()114 void CameraManagerPrivacyClient::ReleaseProxy()
115 {
116 if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
117 proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
118 }
119 proxy_ = nullptr;
120 serviceDeathObserver_ = nullptr;
121 }
122 } // namespace AccessToken
123 } // namespace Security
124 } // namespace OHOS
125
126