1 /*
2 * Copyright (c) 2023-2025 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 "component_privacy.h"
17
18 #include "ability_manager_client.h"
19 #include "constants.h"
20 #include "component_loader.h"
21 #include "distributed_hardware_errno.h"
22 #include "distributed_hardware_log.h"
23 #include "device_manager.h"
24 #include "dm_device_info.h"
25 #include "device_type.h"
26 #include "dh_utils_tool.h"
27 #include "event_handler.h"
28 #include "cJSON.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 #undef DH_LOG_TAG
33 #define DH_LOG_TAG "ComponentPrivacy"
34
35 namespace {
36 constexpr const char *PRIVACY_SUBTYPE = "subtype";
37 constexpr const char *PRIVACY_NETWORKID = "networkId";
38 constexpr uint32_t COMP_START_PAGE = 1;
39 constexpr uint32_t COMP_STOP_PAGE = 2;
40 constexpr uint32_t COMP_PRIVACY_DELAY_TIME = 1000; // million seconds
41 }
42
ComponentPrivacy()43 ComponentPrivacy::ComponentPrivacy()
44 {
45 DHLOGI("ComponentPrivacy ctor.");
46 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
47 eventHandler_ = std::make_shared<ComponentPrivacy::ComponentEventHandler>(runner, this);
48 }
49
~ComponentPrivacy()50 ComponentPrivacy::~ComponentPrivacy()
51 {
52 DHLOGI("ComponentPrivacy dtor.");
53 }
54
OnPrivaceResourceMessage(const ResourceEventType & type,const std::string & subtype,const std::string & networkId,bool & isSensitive,bool & isSameAccout)55 int32_t ComponentPrivacy::OnPrivaceResourceMessage(const ResourceEventType &type, const std::string &subtype,
56 const std::string &networkId, bool &isSensitive, bool &isSameAccout)
57 {
58 DHLOGI("OnPrivaceResourceMessage start.");
59 int32_t ret = DH_FWK_SUCCESS;
60 if (type == ResourceEventType::EVENT_TYPE_QUERY_RESOURCE) {
61 ret = OnResourceInfoCallback(subtype, networkId, isSensitive, isSameAccout);
62 } else if (type == ResourceEventType::EVENT_TYPE_PULL_UP_PAGE) {
63 HandlePullUpPage(subtype, networkId);
64 } else if (type == ResourceEventType::EVENT_TYPE_CLOSE_PAGE) {
65 HandleClosePage(subtype);
66 }
67 return ret;
68 }
69
HandlePullUpPage(const std::string & subtype,const std::string & networkId)70 void ComponentPrivacy::HandlePullUpPage(const std::string &subtype, const std::string &networkId)
71 {
72 if (!IsIdLengthValid(networkId)) {
73 return;
74 }
75 cJSON *jsonArrayMsg = cJSON_CreateArray();
76 if (jsonArrayMsg == NULL) {
77 DHLOGE("Failed to create cJSON arrary.");
78 return;
79 }
80
81 cJSON *tmpJson = cJSON_CreateObject();
82 if (tmpJson == NULL) {
83 cJSON_Delete(jsonArrayMsg);
84 DHLOGE("Failed to create cJSON object.");
85 return;
86 }
87 if (eventHandler_ != nullptr) {
88 DHLOGI("SendEvent COMP_START_PAGE");
89 cJSON_AddStringToObject(tmpJson, PRIVACY_SUBTYPE, subtype.c_str());
90 cJSON_AddStringToObject(tmpJson, PRIVACY_NETWORKID, networkId.c_str());
91 cJSON_AddItemToArray(jsonArrayMsg, tmpJson);
92
93 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(COMP_START_PAGE,
94 std::shared_ptr<cJSON>(jsonArrayMsg, cJSON_Delete), 0);
95 eventHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
96 return;
97 }
98 cJSON_Delete(tmpJson);
99 cJSON_Delete(jsonArrayMsg);
100 }
101
HandleClosePage(const std::string & subtype)102 void ComponentPrivacy::HandleClosePage(const std::string &subtype)
103 {
104 cJSON *jsonArrayMsg = cJSON_CreateArray();
105 if (jsonArrayMsg == NULL) {
106 DHLOGE("Failed to create cJSON arrary.");
107 return;
108 }
109
110 cJSON *tmpJson = cJSON_CreateObject();
111 if (tmpJson == NULL) {
112 cJSON_Delete(jsonArrayMsg);
113 DHLOGE("Failed to create cJSON object.");
114 return;
115 }
116 if (eventHandler_ != nullptr) {
117 DHLOGI("SendEvent COMP_STOP_PAGE");
118 cJSON_AddStringToObject(tmpJson, PRIVACY_SUBTYPE, subtype.c_str());
119 cJSON_AddItemToArray(jsonArrayMsg, tmpJson);
120
121 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(COMP_STOP_PAGE,
122 std::shared_ptr<cJSON>(jsonArrayMsg, cJSON_Delete), 0);
123 eventHandler_->SendEvent(msgEvent, COMP_PRIVACY_DELAY_TIME, AppExecFwk::EventQueue::Priority::IMMEDIATE);
124 return;
125 }
126 cJSON_Delete(tmpJson);
127 cJSON_Delete(jsonArrayMsg);
128 }
129
OnResourceInfoCallback(const std::string & subtype,const std::string & networkId,bool & isSensitive,bool & isSameAccout)130 int32_t ComponentPrivacy::OnResourceInfoCallback(const std::string &subtype, const std::string &networkId,
131 bool &isSensitive, bool &isSameAccout)
132 {
133 if (!IsIdLengthValid(networkId)) {
134 return ERR_DH_FWK_PARA_INVALID;
135 }
136 DHLOGI("OnResourceInfoCallback start.");
137 std::map<std::string, bool> resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc();
138 if (resourceDesc.find(subtype) == resourceDesc.end()) {
139 DHLOGE("GetCompResourceDesc failed.");
140 return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY;
141 }
142 isSensitive = resourceDesc[subtype];
143 DmAuthForm authForm = DmAuthForm::INVALID_TYPE;
144 std::vector<DmDeviceInfo> deviceList;
145 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
146 if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) {
147 DHLOGE("DeviceList size is invalid!");
148 return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY;
149 }
150 for (const auto &deviceInfo : deviceList) {
151 if (std::string(deviceInfo.networkId) == networkId) {
152 authForm = deviceInfo.authForm;
153 break;
154 }
155 }
156 if (authForm == DmAuthForm::IDENTICAL_ACCOUNT) {
157 isSameAccout = true;
158 } else {
159 isSameAccout = false;
160 }
161 return DH_FWK_SUCCESS;
162 }
163
StartPrivacePage(const std::string & subtype,const std::string & networkId)164 int32_t ComponentPrivacy::StartPrivacePage(const std::string &subtype, const std::string &networkId)
165 {
166 if (!IsIdLengthValid(networkId)) {
167 return ERR_DH_FWK_PARA_INVALID;
168 }
169 DHLOGI("StartPrivacePage start.");
170 DmDeviceInfo deviceInfo;
171 DeviceManager::GetInstance().GetDeviceInfo(DH_FWK_PKG_NAME, networkId, deviceInfo);
172 std::string deviceName = std::string(deviceInfo.deviceName);
173 uint16_t deviceTypeId = deviceInfo.deviceTypeId;
174 std::string deviceType = DeviceTypeToString(deviceTypeId);
175 const std::string bundleName = "com.ohos.dhardwareui";
176 const std::string abilityName = "DHardwareUIAbility";
177 int32_t type = -1;
178 if (subtype == "mic") {
179 type = static_cast<int32_t>(DHSubtype::AUDIO_MIC);
180 } else if (subtype == "camera") {
181 type = static_cast<int32_t>(DHSubtype::CAMERA);
182 }
183 AAFwk::Want want;
184 want.SetElementName(bundleName, abilityName);
185 want.SetParam("type", type);
186 want.SetParam("srcNetworkId", networkId);
187 want.SetParam("deviceName", deviceName);
188 want.SetParam("deviceType", deviceType);
189 auto abilityManager = AAFwk::AbilityManagerClient::GetInstance();
190 if (abilityManager == nullptr) {
191 DHLOGE("AbilityManagerClient is nullptr.");
192 return ERR_DH_FWK_POINTER_IS_NULL;
193 }
194 int32_t result = abilityManager->StartAbility(want);
195 DHLOGI("performance time: StartPrivacePage result = %{public}d", result);
196 SetPageFlagTrue();
197 return result;
198 }
199
StopPrivacePage(const std::string & subtype)200 int32_t ComponentPrivacy::StopPrivacePage(const std::string &subtype)
201 {
202 DHLOGI("StopPrivacePage start.");
203 int32_t type = -1;
204 if (subtype == "mic") {
205 type = static_cast<int32_t>(DHSubtype::AUDIO_MIC);
206 } else if (subtype == "camera") {
207 type = static_cast<int32_t>(DHSubtype::CAMERA);
208 }
209 const std::string bundleName = "com.ohos.dhardwareui";
210 const std::string abilityName = "DHardwareUIAbility";
211 int32_t returnCode = 24200102;
212 AAFwk::Want want;
213 want.SetElementName(bundleName, abilityName);
214 want.SetParam("type", type);
215 want.SetParam("returnCode", returnCode);
216 auto abilityManager = AAFwk::AbilityManagerClient::GetInstance();
217 if (abilityManager == nullptr) {
218 DHLOGE("AbilityManagerClient is nullptr.");
219 return ERR_DH_FWK_POINTER_IS_NULL;
220 }
221 int32_t result = abilityManager->StartAbility(want);
222 DHLOGI("performance time: StopPrivacePage result = %{public}d", result);
223 SetPageFlagFalse();
224 return result;
225 }
226
DeviceTypeToString(uint16_t deviceTypeId)227 std::string ComponentPrivacy::DeviceTypeToString(uint16_t deviceTypeId)
228 {
229 DHLOGD("DeviceTypeToString start.");
230 DmDeviceType deviceType = static_cast<DmDeviceType>(deviceTypeId);
231 switch (deviceType) {
232 case DmDeviceType::DEVICE_TYPE_WIFI_CAMERA:
233 return "camera";
234 case DmDeviceType::DEVICE_TYPE_AUDIO:
235 return "audio";
236 case DmDeviceType::DEVICE_TYPE_PC:
237 return "pc";
238 case DmDeviceType::DEVICE_TYPE_PHONE:
239 return "phone";
240 case DmDeviceType::DEVICE_TYPE_PAD:
241 return "pad";
242 case DmDeviceType::DEVICE_TYPE_WATCH:
243 return "watch";
244 case DmDeviceType::DEVICE_TYPE_CAR:
245 return "car";
246 case DmDeviceType::DEVICE_TYPE_TV:
247 return "tv";
248 case DmDeviceType::DEVICE_TYPE_SMART_DISPLAY:
249 return "display";
250 case DmDeviceType::DEVICE_TYPE_2IN1:
251 return "2in1";
252 default:
253 return "unknown";
254 }
255 }
256
SetPageFlagTrue()257 void ComponentPrivacy::SetPageFlagTrue()
258 {
259 isPrivacePageOpen_.store(true);
260 }
261
SetPageFlagFalse()262 void ComponentPrivacy::SetPageFlagFalse()
263 {
264 isPrivacePageOpen_.store(false);
265 }
266
GetPageFlag()267 bool ComponentPrivacy::GetPageFlag()
268 {
269 return isPrivacePageOpen_.load();
270 }
271
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)272 void ComponentPrivacy::ComponentEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
273 {
274 if (event == nullptr) {
275 DHLOGE("event is nullptr");
276 return;
277 }
278 switch (event->GetInnerEventId()) {
279 case COMP_START_PAGE:
280 ProcessStartPage(event);
281 break;
282 case COMP_STOP_PAGE:
283 ProcessStopPage(event);
284 break;
285 default:
286 DHLOGE("ComponentEventHandler EventId %{public}d is undefined.", event->GetInnerEventId());
287 break;
288 }
289 }
290
ComponentEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,ComponentPrivacy * comPrivacy)291 ComponentPrivacy::ComponentEventHandler::ComponentEventHandler(
292 const std::shared_ptr<AppExecFwk::EventRunner> runner, ComponentPrivacy *comPrivacy)
293 : AppExecFwk::EventHandler(runner)
294 {
295 comPrivacyObj_ = comPrivacy;
296 }
297
~ComponentEventHandler()298 ComponentPrivacy::ComponentEventHandler::~ComponentEventHandler()
299 {
300 comPrivacyObj_ = nullptr;
301 }
302
ProcessStartPage(const AppExecFwk::InnerEvent::Pointer & event)303 void ComponentPrivacy::ComponentEventHandler::ProcessStartPage(const AppExecFwk::InnerEvent::Pointer &event)
304 {
305 DHLOGI("ProcessStartPage enter.");
306 std::shared_ptr<cJSON> dataMsg = event->GetSharedObject<cJSON>();
307 cJSON *innerMsg = cJSON_GetArrayItem(dataMsg.get(), 0);
308 cJSON *subtypeJson = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE);
309 if (!IsString(subtypeJson)) {
310 DHLOGE("PRIVACY_SUBTYPE is invalid!");
311 return;
312 }
313 std::string subtype = subtypeJson->valuestring;
314 cJSON *networkIdJson = cJSON_GetObjectItem(innerMsg, PRIVACY_NETWORKID);
315 if (!IsString(networkIdJson)) {
316 DHLOGE("PRIVACY_NETWORKID is invalid!");
317 return;
318 }
319 std::string networkId = networkIdJson->valuestring;
320 if (comPrivacyObj_ == nullptr) {
321 DHLOGE("comPrivacyObj_ is nullptr");
322 return;
323 }
324 comPrivacyObj_->StartPrivacePage(subtype, networkId);
325 }
326
ProcessStopPage(const AppExecFwk::InnerEvent::Pointer & event)327 void ComponentPrivacy::ComponentEventHandler::ProcessStopPage(const AppExecFwk::InnerEvent::Pointer &event)
328 {
329 DHLOGI("ProcessStopPage enter.");
330 std::shared_ptr<cJSON> dataMsg = event->GetSharedObject<cJSON>();
331 cJSON *innerMsg = cJSON_GetArrayItem(dataMsg.get(), 0);
332 if (innerMsg == NULL) {
333 DHLOGE("innerMsg is nullptr");
334 return;
335 }
336 cJSON *subtypeJson = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE);
337 if (!IsString(subtypeJson)) {
338 DHLOGE("PRIVACY_SUBTYPE is invalid!");
339 return;
340 }
341 std::string subtype = subtypeJson->valuestring;
342 if (comPrivacyObj_ == nullptr) {
343 DHLOGE("comPrivacyObj_ is nullptr");
344 return;
345 }
346 comPrivacyObj_->StopPrivacePage(subtype);
347 }
348 } // namespace DistributedHardware
349 } // namespace OHOS
350