1 /*
2 * Copyright (c) 2024 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_manager_adapter.h"
17 #include "accesstoken_common_log.h"
18 #include <iremote_proxy.h>
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25
26 #ifdef CAMERA_FRAMEWORK_ENABLE
27 static constexpr int32_t CAMERA_IS_CAMERA_MUTED_IPC_CODE = 13;
28 #endif
29
GetInstance()30 CameraManagerAdapter& CameraManagerAdapter::GetInstance()
31 {
32 static CameraManagerAdapter *instance = new (std::nothrow) CameraManagerAdapter();
33 return *instance;
34 }
35
CameraManagerAdapter()36 CameraManagerAdapter::CameraManagerAdapter()
37 {}
38
~CameraManagerAdapter()39 CameraManagerAdapter::~CameraManagerAdapter()
40 {}
41
IsCameraMuted()42 bool CameraManagerAdapter::IsCameraMuted()
43 {
44 #ifndef CAMERA_FRAMEWORK_ENABLE
45 LOGI(PRI_DOMAIN, PRI_TAG, "camera framework is not support.");
46 return false;
47 #else
48 auto proxy = GetProxy();
49 if (proxy == nullptr) {
50 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to GetProxy.");
51 return false;
52 }
53
54 MessageParcel data;
55 MessageParcel reply;
56 MessageOption option;
57
58 std::u16string CAMERA_MGR_DESCRIPTOR = u"OHOS.CameraStandard.ICameraService";
59 if (!data.WriteInterfaceToken(CAMERA_MGR_DESCRIPTOR)) {
60 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to write WriteInterfaceToken.");
61 return false;
62 }
63 if (!data.WriteInt32(0)) {
64 return false;
65 }
66 int32_t error = proxy->SendRequest(CAMERA_IS_CAMERA_MUTED_IPC_CODE, data, reply, option);
67 if (error != NO_ERROR) {
68 LOGE(PRI_DOMAIN, PRI_TAG, "SendRequest error: %{public}d", error);
69 return false;
70 }
71 error = reply.ReadInt32();
72 if (error != NO_ERROR) {
73 return false;
74 }
75
76 bool isMuteMode = reply.ReadInt32() == 1 ? true : false;
77 return isMuteMode;
78 #endif
79 }
80
81 #ifdef CAMERA_FRAMEWORK_ENABLE
InitProxy()82 void CameraManagerAdapter::InitProxy()
83 {
84 if (proxy_ != nullptr && (!proxy_->IsObjectDead())) {
85 return;
86 }
87 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (systemManager == nullptr) {
89 LOGE(PRI_DOMAIN, PRI_TAG, "Fail to get system ability registry.");
90 return;
91 }
92 sptr<IRemoteObject> remoteObj = systemManager->CheckSystemAbility(CAMERA_SERVICE_ID);
93 if (remoteObj == nullptr) {
94 LOGE(PRI_DOMAIN, PRI_TAG, "Fail to connect ability manager service.");
95 return;
96 }
97
98 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) CameraManagerDeathRecipient());
99 if (deathRecipient_ == nullptr) {
100 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to create CameraManagerDeathRecipient!");
101 return;
102 }
103 if ((remoteObj->IsProxyObject()) && (!remoteObj->AddDeathRecipient(deathRecipient_))) {
104 LOGE(PRI_DOMAIN, PRI_TAG, "Add death recipient to AbilityManagerService failed.");
105 return;
106 }
107 proxy_ = remoteObj;
108 }
109
GetProxy()110 sptr<IRemoteObject> CameraManagerAdapter::GetProxy()
111 {
112 std::lock_guard<std::mutex> lock(proxyMutex_);
113 if (proxy_ == nullptr || proxy_->IsObjectDead()) {
114 InitProxy();
115 }
116 return proxy_;
117 }
118
ReleaseProxy(const wptr<IRemoteObject> & remote)119 void CameraManagerAdapter::ReleaseProxy(const wptr<IRemoteObject>& remote)
120 {
121 std::lock_guard<std::mutex> lock(proxyMutex_);
122 if ((proxy_ != nullptr) && (proxy_ == remote.promote())) {
123 proxy_->RemoveDeathRecipient(deathRecipient_);
124 proxy_ = nullptr;
125 deathRecipient_ = nullptr;
126 }
127 }
128
OnRemoteDied(const wptr<IRemoteObject> & remote)129 void CameraManagerAdapter::CameraManagerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
130 {
131 LOGE(PRI_DOMAIN, PRI_TAG, "CameraManagerDeathRecipient handle remote died.");
132 CameraManagerAdapter::GetInstance().ReleaseProxy(remote);
133 }
134 #endif
135 } // namespace AccessToken
136 } // namespace Security
137 } // namespace OHOS
138