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 "audio_manager_privacy_client.h"
16 #include <unistd.h>
17
18 #include "accesstoken_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "AudioManagerPrivacyClient"
28 };
29 std::recursive_mutex g_instanceMutex;
30 } // namespace
31
GetInstance()32 AudioManagerPrivacyClient& AudioManagerPrivacyClient::GetInstance()
33 {
34 static AudioManagerPrivacyClient* instance = nullptr;
35 if (instance == nullptr) {
36 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
37 if (instance == nullptr) {
38 instance = new AudioManagerPrivacyClient();
39 }
40 }
41 return *instance;
42 }
43
AudioManagerPrivacyClient()44 AudioManagerPrivacyClient::AudioManagerPrivacyClient()
45 {}
46
~AudioManagerPrivacyClient()47 AudioManagerPrivacyClient::~AudioManagerPrivacyClient()
48 {
49 std::lock_guard<std::mutex> lock(proxyMutex_);
50 ReleaseProxy();
51 }
52
SetMicrophoneMutePersistent(const bool isMute,const PolicyType type)53 int32_t AudioManagerPrivacyClient::SetMicrophoneMutePersistent(const bool isMute, const PolicyType type)
54 {
55 auto proxy = GetProxy();
56 if (proxy == nullptr) {
57 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
58 return -1;
59 }
60 return proxy->SetMicrophoneMutePersistent(isMute, type);
61 }
62
GetPersistentMicMuteState()63 bool AudioManagerPrivacyClient::GetPersistentMicMuteState()
64 {
65 auto proxy = GetProxy();
66 if (proxy == nullptr) {
67 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
68 return false;
69 }
70 return proxy->GetPersistentMicMuteState();
71 }
72
InitProxy()73 void AudioManagerPrivacyClient::InitProxy()
74 {
75 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 if (sam == nullptr) {
77 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
78 return;
79 }
80 auto audioManagerSa = sam->GetSystemAbility(AUDIO_POLICY_SERVICE_ID);
81 if (audioManagerSa == nullptr) {
82 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
83 AUDIO_POLICY_SERVICE_ID);
84 return;
85 }
86
87 serviceDeathObserver_ = sptr<AudioMgrDeathRecipient>::MakeSptr();
88 if (serviceDeathObserver_ != nullptr) {
89 audioManagerSa->AddDeathRecipient(serviceDeathObserver_);
90 }
91
92 proxy_ = new AudioManagerPrivacyProxy(audioManagerSa);
93 if (proxy_ == nullptr) {
94 ACCESSTOKEN_LOG_ERROR(LABEL, "Iface_cast get null");
95 }
96 }
97
OnRemoteDiedHandle()98 void AudioManagerPrivacyClient::OnRemoteDiedHandle()
99 {
100 std::lock_guard<std::mutex> lock(proxyMutex_);
101 ReleaseProxy();
102 }
103
GetProxy()104 sptr<IAudioPolicy> AudioManagerPrivacyClient::GetProxy()
105 {
106 std::lock_guard<std::mutex> lock(proxyMutex_);
107 if (proxy_ == nullptr) {
108 InitProxy();
109 }
110 return proxy_;
111 }
112
ReleaseProxy()113 void AudioManagerPrivacyClient::ReleaseProxy()
114 {
115 if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
116 proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
117 }
118 proxy_ = nullptr;
119 serviceDeathObserver_ = nullptr;
120 }
121 } // namespace AccessToken
122 } // namespace Security
123 } // namespace OHOS
124
125