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