• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "disable_usb_plugin.h"
17 
18 #include "bool_serializer.h"
19 #include "edm_constants.h"
20 #include "edm_ipc_interface_code.h"
21 #include "edm_utils.h"
22 #include "iplugin_manager.h"
23 #include "usb_srv_client.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisableUsbPlugin::GetPlugin());
28 
InitPlugin(std::shared_ptr<IPluginTemplate<DisableUsbPlugin,bool>> ptr)29 void DisableUsbPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<DisableUsbPlugin, bool>> ptr)
30 {
31     EDMLOGD("DisableUsbPlugin InitPlugin...");
32     ptr->InitAttribute(EdmInterfaceCode::DISABLE_USB, "disable_usb",
33         "ohos.permission.ENTERPRISE_MANAGE_USB", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
34     ptr->SetSerializer(BoolSerializer::GetInstance());
35     ptr->SetOnHandlePolicyListener(&DisableUsbPlugin::OnSetPolicy, FuncOperateType::SET);
36     ptr->SetOnAdminRemoveListener(&DisableUsbPlugin::OnAdminRemove);
37 }
38 
OnSetPolicy(bool & data)39 ErrCode DisableUsbPlugin::OnSetPolicy(bool &data)
40 {
41     EDMLOGI("DisableUsbPlugin OnSetPolicy...disable = %{public}d", data);
42     auto policyManager = IPolicyManager::GetInstance();
43     std::string allowUsbDevicePolicy;
44     policyManager->GetPolicy("", "allowed_usb_devices", allowUsbDevicePolicy);
45     std::string usbStoragePolicy;
46     policyManager->GetPolicy("", "usb_read_only", usbStoragePolicy);
47     if (data && (!allowUsbDevicePolicy.empty() ||
48         usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_DISABLED) ||
49         usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_READ_ONLY))) {
50         EDMLOGE("DisableUsbPlugin OnSetPolicy: CONFLICT! allowedUsbDevice: %{public}s, usbStoragePolicy: %{public}s",
51             allowUsbDevicePolicy.c_str(), usbStoragePolicy.c_str());
52         return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
53     }
54 
55     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
56     int32_t usbRet = srvClient.ManageGlobalInterface(data);
57     if (usbRet != ERR_OK) {
58         EDMLOGE("DisableUsbPlugin OnSetPolicy: ManageGlobalInterface failed! ret:%{public}d", usbRet);
59         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
60     }
61     return ERR_OK;
62 }
63 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)64 ErrCode DisableUsbPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
65     int32_t userId)
66 {
67     EDMLOGI("DisableUsbPlugin OnGetPolicy %{public}s...", policyData.c_str());
68     bool isDisabled = false;
69     pluginInstance_->serializer_->Deserialize(policyData, isDisabled);
70     reply.WriteInt32(ERR_OK);
71     reply.WriteBool(isDisabled);
72     return ERR_OK;
73 }
74 
OnAdminRemove(const std::string & adminName,bool & data,int32_t userId)75 ErrCode DisableUsbPlugin::OnAdminRemove(const std::string &adminName, bool &data, int32_t userId)
76 {
77     EDMLOGI("DisableUsbPlugin OnAdminRemove %{public}d...", data);
78     if (!data) {
79         return ERR_OK;
80     }
81     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
82     int32_t usbRet = srvClient.ManageGlobalInterface(!data);
83     if (usbRet != ERR_OK) {
84         EDMLOGE("DisableUsbPlugin OnSetPolicy: ManageGlobalInterface failed! ret:%{public}d", usbRet);
85         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
86     }
87     return ERR_OK;
88 }
89 } // namespace EDM
90 } // namespace OHOS
91