1 /*
2 * Copyright (c) 2024-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 "clipboard_policy_query.h"
17
18 #include "clipboard_policy_serializer.h"
19 #include "edm_bundle_manager_impl.h"
20 #include "edm_log.h"
21
22 namespace OHOS {
23 namespace EDM {
GetPolicyName()24 std::string ClipboardPolicyQuery::GetPolicyName()
25 {
26 return PolicyName::POLICY_CLIPBOARD_POLICY;
27 }
28
GetPermission(IPlugin::PermissionType,const std::string & permissionTag)29 std::string ClipboardPolicyQuery::GetPermission(IPlugin::PermissionType, const std::string &permissionTag)
30 {
31 return EdmPermission::PERMISSION_ENTERPRISE_MANAGE_SECURITY;
32 }
33
QueryPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)34 ErrCode ClipboardPolicyQuery::QueryPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
35 int32_t userId)
36 {
37 EDMLOGI("ClipboardPolicyQuery OnGetPolicy");
38 std::string policy = policyData;
39 std::map<int32_t, ClipboardInfo> policyMap;
40 if (!ClipboardSerializer::GetInstance()->Deserialize(policyData, policyMap)) {
41 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
42 }
43 std::map<int32_t, ClipboardInfo> resultMap;
44 int32_t flag = data.ReadInt32();
45 ClipboardInfo info;
46 if (flag == ClipboardFunctionType::GET_HAS_TOKEN_ID) {
47 int32_t tokenId = data.ReadInt32();
48 auto it = policyMap.find(tokenId);
49 if (it != policyMap.end()) {
50 resultMap[tokenId] = policyMap[it->first];
51 } else {
52 info = {ClipboardPolicy::DEFAULT, -1, ""};
53 resultMap[tokenId] = info;
54 }
55 ClipboardSerializer::GetInstance()->Serialize(resultMap, policy);
56 }
57 if (flag == ClipboardFunctionType::GET_HAS_BUNDLE_NAME) {
58 info.bundleName = data.ReadString();
59 info.userId = data.ReadInt32();
60 auto bundleMgr = std::make_shared<EdmBundleManagerImpl>();
61 int32_t tokenId = bundleMgr->GetTokenId(info.bundleName, info.userId);
62 auto it = policyMap.find(tokenId);
63 if (it != policyMap.end()) {
64 resultMap[-1] = policyMap[it->first];
65 } else {
66 info.policy = ClipboardPolicy::DEFAULT;
67 resultMap[-1] = info;
68 }
69 ClipboardSerializer::GetInstance()->Serialize(resultMap, policy);
70 }
71 reply.WriteInt32(ERR_OK);
72 reply.WriteString(policy);
73 return ERR_OK;
74 }
75 } // namespace EDM
76 } // namespace OHOS