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