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