1 /*
2 * Copyright (c) 2023 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 "event_handler.h"
27 #include "nlohmann/json.hpp"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 #undef DH_LOG_TAG
32 #define DH_LOG_TAG "ComponentPrivacy"
33
ComponentPrivacy()34 ComponentPrivacy::ComponentPrivacy()
35 {
36 DHLOGI("ComponentPrivacy ctor.");
37 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
38 eventHandler_ = std::make_shared<ComponentPrivacy::ComponentEventHandler>(runner, this);
39 }
40
~ComponentPrivacy()41 ComponentPrivacy::~ComponentPrivacy()
42 {
43 DHLOGI("ComponentPrivacy dtor.");
44 }
45
OnPrivaceResourceMessage(const ResourceEventType & type,const std::string & subtype,const std::string & networkId,bool & isSensitive,bool & isSameAccout)46 int32_t ComponentPrivacy::OnPrivaceResourceMessage(const ResourceEventType &type, const std::string &subtype,
47 const std::string &networkId, bool &isSensitive, bool &isSameAccout)
48 {
49 DHLOGI("OnPrivaceResourceMessage start.");
50 int32_t ret = DH_FWK_SUCCESS;
51 if (type == ResourceEventType::EVENT_TYPE_QUERY_RESOURCE) {
52 ret = OnResourceInfoCallback(subtype, networkId, isSensitive, isSameAccout);
53 }
54 if (type == ResourceEventType::EVENT_TYPE_PULL_UP_PAGE) {
55 if (eventHandler_ != nullptr) {
56 DHLOGI("SendEvent COMP_START_PAGE");
57 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
58 nlohmann::json tmpJson;
59 tmpJson[PRIVACY_SUBTYPE] = subtype;
60 tmpJson[PRIVACY_NETWORKID] = networkId;
61 jsonArrayMsg->push_back(tmpJson);
62 AppExecFwk::InnerEvent::Pointer msgEvent =
63 AppExecFwk::InnerEvent::Get(COMP_START_PAGE, jsonArrayMsg, 0);
64 eventHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
65 }
66 }
67 if (type == ResourceEventType::EVENT_TYPE_CLOSE_PAGE) {
68 if (eventHandler_ != nullptr) {
69 DHLOGI("SendEvent COMP_STOP_PAGE");
70 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
71 nlohmann::json tmpJson;
72 tmpJson[PRIVACY_SUBTYPE] = subtype;
73 jsonArrayMsg->push_back(tmpJson);
74 AppExecFwk::InnerEvent::Pointer msgEvent =
75 AppExecFwk::InnerEvent::Get(COMP_STOP_PAGE, jsonArrayMsg, 0);
76 eventHandler_->SendEvent(msgEvent, COMP_PRIVACY_DELAY_TIME, AppExecFwk::EventQueue::Priority::IMMEDIATE);
77 }
78 }
79 return ret;
80 }
81
OnResourceInfoCallback(const std::string & subtype,const std::string & networkId,bool & isSensitive,bool & isSameAccout)82 int32_t ComponentPrivacy::OnResourceInfoCallback(const std::string &subtype, const std::string &networkId,
83 bool &isSensitive, bool &isSameAccout)
84 {
85 DHLOGI("OnResourceInfoCallback start.");
86 std::map<std::string, bool> resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc();
87 if (resourceDesc.find(subtype) == resourceDesc.end()) {
88 DHLOGE("GetCompResourceDesc failed.");
89 return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY;
90 }
91 isSensitive = resourceDesc[subtype];
92 DmAuthForm authForm = DmAuthForm::INVALID_TYPE;
93 std::vector<DmDeviceInfo> deviceList;
94 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
95 if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) {
96 DHLOGE("DeviceList size is invalid!");
97 return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY;
98 }
99 for (const auto &deviceInfo : deviceList) {
100 if (std::string(deviceInfo.networkId) == networkId) {
101 authForm = deviceInfo.authForm;
102 break;
103 }
104 }
105 if (authForm == DmAuthForm::IDENTICAL_ACCOUNT) {
106 isSameAccout = true;
107 } else {
108 isSameAccout = false;
109 }
110 return DH_FWK_SUCCESS;
111 }
112
StartPrivacePage(const std::string & subtype,const std::string & networkId)113 int32_t ComponentPrivacy::StartPrivacePage(const std::string &subtype, const std::string &networkId)
114 {
115 DHLOGI("StartPrivacePage start.");
116 DmDeviceInfo deviceInfo;
117 DeviceManager::GetInstance().GetDeviceInfo(DH_FWK_PKG_NAME, networkId, deviceInfo);
118 std::string deviceName = std::string(deviceInfo.deviceName);
119 uint16_t deviceTypeId = deviceInfo.deviceTypeId;
120 std::string deviceType = DeviceTypeToString(deviceTypeId);
121 const std::string bundleName = "com.ohos.dhardwareui";
122 const std::string abilityName = "DHardwareUIAbility";
123 int32_t type = -1;
124 if (subtype == "mic") {
125 type = static_cast<int32_t>(DHSubtype::AUDIO_MIC);
126 } else if (subtype == "camera") {
127 type = static_cast<int32_t>(DHSubtype::CAMERA);
128 }
129 AAFwk::Want want;
130 want.SetElementName(bundleName, abilityName);
131 want.SetParam("type", type);
132 want.SetParam("srcNetworkId", networkId);
133 want.SetParam("deviceName", deviceName);
134 want.SetParam("deviceType", deviceType);
135 auto abilityManager = AAFwk::AbilityManagerClient::GetInstance();
136 if (abilityManager == nullptr) {
137 DHLOGE("AbilityManagerClient is nullptr.");
138 return ERR_DH_FWK_POINTER_IS_NULL;
139 }
140 int32_t result = abilityManager->StartAbility(want);
141 DHLOGI("performance time: StartPrivacePage result = %d", result);
142 return result;
143 }
144
StopPrivacePage(const std::string & subtype)145 int32_t ComponentPrivacy::StopPrivacePage(const std::string &subtype)
146 {
147 DHLOGI("StopPrivacePage start.");
148 int32_t type = -1;
149 if (subtype == "mic") {
150 type = static_cast<int32_t>(DHSubtype::AUDIO_MIC);
151 } else if (subtype == "camera") {
152 type = static_cast<int32_t>(DHSubtype::CAMERA);
153 }
154 const std::string bundleName = "com.ohos.dhardwareui";
155 const std::string abilityName = "DHardwareUIAbility";
156 int32_t returnCode = 24200102;
157 AAFwk::Want want;
158 want.SetElementName(bundleName, abilityName);
159 want.SetParam("type", type);
160 want.SetParam("returnCode", returnCode);
161 auto abilityManager = AAFwk::AbilityManagerClient::GetInstance();
162 if (abilityManager == nullptr) {
163 DHLOGE("AbilityManagerClient is nullptr.");
164 return ERR_DH_FWK_POINTER_IS_NULL;
165 }
166 int32_t result = abilityManager->StartAbility(want);
167 DHLOGI("performance time: StopPrivacePage result = %d", result);
168 return result;
169 }
170
DeviceTypeToString(uint16_t deviceTypeId)171 std::string ComponentPrivacy::DeviceTypeToString(uint16_t deviceTypeId)
172 {
173 DHLOGD("DeviceTypeToString start.");
174 DmDeviceType deviceType = static_cast<DmDeviceType>(deviceTypeId);
175 switch (deviceType) {
176 case DmDeviceType::DEVICE_TYPE_WIFI_CAMERA:
177 return "camera";
178 case DmDeviceType::DEVICE_TYPE_AUDIO:
179 return "audio";
180 case DmDeviceType::DEVICE_TYPE_PC:
181 return "pc";
182 case DmDeviceType::DEVICE_TYPE_PHONE:
183 return "phone";
184 case DmDeviceType::DEVICE_TYPE_PAD:
185 return "pad";
186 case DmDeviceType::DEVICE_TYPE_WATCH:
187 return "watch";
188 case DmDeviceType::DEVICE_TYPE_CAR:
189 return "car";
190 case DmDeviceType::DEVICE_TYPE_TV:
191 return "tv";
192 case DmDeviceType::DEVICE_TYPE_SMART_DISPLAY:
193 return "display";
194 case DmDeviceType::DEVICE_TYPE_2IN1:
195 return "2in1";
196 default:
197 return "unknown";
198 }
199 }
200
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)201 void ComponentPrivacy::ComponentEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
202 {
203 auto iter = eventFuncMap_.find(event->GetInnerEventId());
204 if (iter == eventFuncMap_.end()) {
205 DHLOGE("ComponentEventHandler Event Id %d is undefined.", event->GetInnerEventId());
206 return;
207 }
208 compEventFunc &func = iter->second;
209 (this->*func)(event);
210 }
211
ComponentEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,ComponentPrivacy * comPrivacy)212 ComponentPrivacy::ComponentEventHandler::ComponentEventHandler(
213 const std::shared_ptr<AppExecFwk::EventRunner> &runner, ComponentPrivacy *comPrivacy)
214 : AppExecFwk::EventHandler(runner)
215 {
216 eventFuncMap_[COMP_START_PAGE] = &ComponentEventHandler::ProcessStartPage;
217 eventFuncMap_[COMP_STOP_PAGE] = &ComponentEventHandler::ProcessStopPage;
218
219 comPrivacyObj_ = comPrivacy;
220 }
221
~ComponentEventHandler()222 ComponentPrivacy::ComponentEventHandler::~ComponentEventHandler()
223 {
224 eventFuncMap_.clear();
225 comPrivacyObj_ = nullptr;
226 }
227
ProcessStartPage(const AppExecFwk::InnerEvent::Pointer & event)228 void ComponentPrivacy::ComponentEventHandler::ProcessStartPage(
229 const AppExecFwk::InnerEvent::Pointer &event)
230 {
231 DHLOGI("ProcessStartPage enter.");
232 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
233 auto it = dataMsg->begin();
234 nlohmann::json innerMsg = *(it);
235 std::string subtype = innerMsg[PRIVACY_SUBTYPE];
236 std::string networkId = innerMsg[PRIVACY_NETWORKID];
237 comPrivacyObj_->StartPrivacePage(subtype, networkId);
238 }
239
ProcessStopPage(const AppExecFwk::InnerEvent::Pointer & event)240 void ComponentPrivacy::ComponentEventHandler::ProcessStopPage(
241 const AppExecFwk::InnerEvent::Pointer &event)
242 {
243 DHLOGI("ProcessStopPage enter.");
244 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
245 auto it = dataMsg->begin();
246 nlohmann::json innerMsg = *(it);
247 std::string subtype = innerMsg[PRIVACY_SUBTYPE];
248 comPrivacyObj_->StopPrivacePage(subtype);
249 }
250 } // namespace DistributedHardware
251 } // namespace OHOS
252