• 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 #define LOG_TAG "CloudServiceStub"
16 #include "cloud_service_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "itypes_util.h"
20 #include "log_print.h"
21 #include "permission/permission_validator.h"
22 #include "utils/anonymous.h"
23 #include "tokenid_kit.h"
24 namespace OHOS::CloudData {
25 using namespace DistributedData;
26 using namespace OHOS::Security::AccessToken;
27 const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = {
28     &CloudServiceStub::OnEnableCloud,
29     &CloudServiceStub::OnDisableCloud,
30     &CloudServiceStub::OnChangeAppSwitch,
31     &CloudServiceStub::OnClean,
32     &CloudServiceStub::OnNotifyDataChange,
33 };
34 
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply)35 int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply)
36 {
37     ZLOGI("code:%{public}u callingPid:%{public}u", code, IPCSkeleton::GetCallingPid());
38     std::u16string local = CloudServiceStub::GetDescriptor();
39     std::u16string remote = data.ReadInterfaceToken();
40     if (local != remote) {
41         ZLOGE("local is not equal to remote");
42         return -1;
43     }
44 
45     if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) {
46         ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
47         return -1;
48     }
49 
50     if (!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())) {
51         ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
52         auto result = static_cast<int32_t>(PERMISSION_DENIED);
53         return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
54     }
55 
56     if (!DistributedKv::PermissionValidator::GetInstance().IsCloudConfigPermit(IPCSkeleton::GetCallingTokenID())) {
57         ZLOGE("cloud config permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
58         auto result = static_cast<int32_t>(CLOUD_CONFIG_PERMISSION_DENIED);
59         return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
60     }
61 
62     std::string id;
63     if (!ITypesUtil::Unmarshal(data, id)) {
64         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
65         return IPC_STUB_INVALID_DATA_ERR;
66     }
67     return (this->*HANDLERS[code])(id, data, reply);
68 }
69 
OnEnableCloud(const std::string & id,MessageParcel & data,MessageParcel & reply)70 int32_t CloudServiceStub::OnEnableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply)
71 {
72     std::map<std::string, int32_t> switches;
73     if (!ITypesUtil::Unmarshal(data, switches)) {
74         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
75         return IPC_STUB_INVALID_DATA_ERR;
76     }
77     auto result = EnableCloud(id, switches);
78     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
79 }
80 
OnDisableCloud(const std::string & id,MessageParcel & data,MessageParcel & reply)81 int32_t CloudServiceStub::OnDisableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply)
82 {
83     auto result = DisableCloud(id);
84     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
85 }
86 
OnChangeAppSwitch(const std::string & id,MessageParcel & data,MessageParcel & reply)87 int32_t CloudServiceStub::OnChangeAppSwitch(const std::string &id, MessageParcel &data, MessageParcel &reply)
88 {
89     std::string bundleName;
90     int32_t appSwitch = SWITCH_OFF;
91     if (!ITypesUtil::Unmarshal(data, bundleName, appSwitch)) {
92         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
93         return IPC_STUB_INVALID_DATA_ERR;
94     }
95     auto result = ChangeAppSwitch(id, bundleName, appSwitch);
96     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
97 }
98 
OnClean(const std::string & id,MessageParcel & data,MessageParcel & reply)99 int32_t CloudServiceStub::OnClean(const std::string &id, MessageParcel &data, MessageParcel &reply)
100 {
101     std::map<std::string, int32_t> actions;
102     if (!ITypesUtil::Unmarshal(data, actions)) {
103         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
104         return IPC_STUB_INVALID_DATA_ERR;
105     }
106     auto result = Clean(id, actions);
107     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
108 }
109 
OnNotifyDataChange(const std::string & id,MessageParcel & data,MessageParcel & reply)110 int32_t CloudServiceStub::OnNotifyDataChange(const std::string &id, MessageParcel &data, MessageParcel &reply)
111 {
112     std::string bundleName;
113     if (!ITypesUtil::Unmarshal(data, bundleName)) {
114         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
115         return IPC_STUB_INVALID_DATA_ERR;
116     }
117     auto result = NotifyDataChange(id, bundleName);
118     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
119 }
120 } // namespace OHOS::CloudData
121