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 "snapshot_skip_plugin.h"
17 
18 #include "mock_session_manager_service_interface.h"
19 
20 #include "array_string_serializer.h"
21 #include "edm_ipc_interface_code.h"
22 #include "edm_sys_manager.h"
23 #include "plugin_manager.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS {
27 namespace EDM {
28 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(SnapshotSkipPlugin::GetPlugin());
29 
InitPlugin(std::shared_ptr<IPluginTemplate<SnapshotSkipPlugin, std::vector<std::string>>> ptr)30 void SnapshotSkipPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<SnapshotSkipPlugin, std::vector<std::string>>> ptr)
31 {
32     EDMLOGI("SnapshotSkipPlugin InitPlugin...");
33     ptr->InitAttribute(EdmInterfaceCode::SNAPSHOT_SKIP, "snapshot_skip",
34         "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
35     ptr->SetSerializer(ArrayStringSerializer::GetInstance());
36     ptr->SetOnHandlePolicyListener(&SnapshotSkipPlugin::OnSetPolicy, FuncOperateType::SET);
37     ptr->SetOnHandlePolicyListener(&SnapshotSkipPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
38 }
39 
OnSetPolicy(std::vector<std::string> &data, std::vector<std::string> &currentData, int32_t userId)40 ErrCode SnapshotSkipPlugin::OnSetPolicy(std::vector<std::string> &data,
41     std::vector<std::string> &currentData, int32_t userId)
42 {
43     EDMLOGD("SnapshotSkipPlugin OnSetPolicy");
44     if (data.empty()) {
45         EDMLOGW("SnapshotSkipPlugin OnSetPolicy data is empty.");
46         return ERR_OK;
47     }
48     if (data.size() > EdmConstants::DISALLOW_LIST_FOR_ACCOUNT_MAX_SIZE) {
49         EDMLOGE("SnapshotSkipPlugin OnSetPolicy input data is too large.");
50         return EdmReturnErrCode::PARAM_ERROR;
51     }
52     std::vector<std::string> mergeData = ArrayStringSerializer::GetInstance()->SetUnionPolicyData(data, currentData);
53     if (mergeData.size() > EdmConstants::DISALLOW_LIST_FOR_ACCOUNT_MAX_SIZE) {
54         EDMLOGE("SnapshotSkipPlugin OnSetPolicy merge data is too large.");
55         return EdmReturnErrCode::PARAM_ERROR;
56     }
57     int32_t ret = SetSnapshotSkipByUserIdAndBundleNameList(userId, mergeData);
58     if (FAILED(ret)) {
59         EDMLOGE("SnapshotSkipPlugin OnRemovePolicy failed %{public}d", ret);
60         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
61     }
62     currentData = mergeData;
63     return ERR_OK;
64 }
65 
OnRemovePolicy(std::vector<std::string> &data, std::vector<std::string> &currentData, int32_t userId)66 ErrCode SnapshotSkipPlugin::OnRemovePolicy(std::vector<std::string> &data,
67     std::vector<std::string> &currentData, int32_t userId)
68 {
69     if (data.empty()) {
70         EDMLOGW("SnapshotSkipPlugin OnRemovePolicy data is empty.");
71         return ERR_OK;
72     }
73     if (data.size() > EdmConstants::DISALLOW_LIST_FOR_ACCOUNT_MAX_SIZE) {
74         EDMLOGE("SnapshotSkipPlugin OnRemovePolicy input data is too large.");
75         return EdmReturnErrCode::PARAM_ERROR;
76     }
77     std::vector<std::string> mergeData =
78         ArrayStringSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
79     int32_t ret = SetSnapshotSkipByUserIdAndBundleNameList(userId, mergeData);
80     if (FAILED(ret)) {
81         EDMLOGE("SnapshotSkipPlugin OnRemovePolicy failed %{public}d", ret);
82         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
83     }
84     currentData = mergeData;
85     return ERR_OK;
86 }
87 
SetSnapshotSkipByUserIdAndBundleNameList(int32_t userId, const std::vector<std::string> &mergeData)88 int32_t SnapshotSkipPlugin::SetSnapshotSkipByUserIdAndBundleNameList(int32_t userId,
89     const std::vector<std::string> &mergeData)
90 {
91     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(WINDOW_MANAGER_SERVICE_ID);
92     if (remoteObject == nullptr) {
93         EDMLOGE("SetSnapshotSkipByUserIdAndBundleNameList wms obj get fial");
94         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
95     }
96     auto mockSessionManagerServiceProxy = iface_cast<OHOS::Rosen::IMockSessionManagerInterface>(remoteObject);
97     if (mockSessionManagerServiceProxy == nullptr) {
98         EDMLOGE("SetSnapshotSkipByUserIdAndBundleNameList wms obj cast fial");
99         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
100     }
101     return mockSessionManagerServiceProxy->SetSnapshotSkipByUserIdAndBundleNames(userId, mergeData);
102 }
103 
OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId)104 ErrCode SnapshotSkipPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data,
105     MessageParcel &reply, int32_t userId)
106 {
107     EDMLOGI("SnapshotSkipPlugin OnGetPolicy policyData :");
108     std::vector<std::string> bundles;
109     ArrayStringSerializer::GetInstance()->Deserialize(policyData, bundles);
110     reply.WriteInt32(ERR_OK);
111     reply.WriteInt32(bundles.size());
112     reply.WriteStringVector(bundles);
113     return ERR_OK;
114 }
115 } // namespace EDM
116 } // namespace OHOS
117