• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "concurrent_task_service_stub.h"
17 #include "concurrent_task_log.h"
18 #include "concurrent_task_errors.h"
19 #include "string_ex.h"
20 #include "ipc_skeleton.h"
21 
22 namespace OHOS {
23 namespace ConcurrentTask {
24 namespace {
IsValidToken(MessageParcel & data)25     bool IsValidToken(MessageParcel& data)
26     {
27         std::u16string descriptor = ConcurrentTaskServiceStub::GetDescriptor();
28         std::u16string remoteDescriptor = data.ReadInterfaceToken();
29         return descriptor == remoteDescriptor;
30     }
31 }
32 
ConcurrentTaskServiceStub()33 ConcurrentTaskServiceStub::ConcurrentTaskServiceStub()
34 {
35     Init();
36 }
37 
~ConcurrentTaskServiceStub()38 ConcurrentTaskServiceStub::~ConcurrentTaskServiceStub()
39 {
40     funcMap_.clear();
41 }
42 
ReportDataInner(MessageParcel & data,MessageParcel & reply)43 int32_t ConcurrentTaskServiceStub::ReportDataInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
44 {
45     if (!IsValidToken(data)) {
46         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
47     }
48 
49     uint32_t type = 0;
50     int64_t value = 0;
51     std::string payload;
52     if (!data.ReadUint32(type) || !data.ReadInt64(value) || !data.ReadString(payload)) {
53         CONCUR_LOGE("Read info failed in ReportData Stub");
54         return IPC_STUB_ERR;
55     }
56     if (payload.empty()) {
57         return ERR_OK;
58     }
59     ReportData(type, value, StringToJson(payload));
60     return ERR_OK;
61 }
62 
ReportSceneInfoInner(MessageParcel & data,MessageParcel & reply)63 int32_t ConcurrentTaskServiceStub::ReportSceneInfoInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
64 {
65     if (!IsValidToken(data)) {
66         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
67     }
68 
69     uint32_t type = 0;
70     std::string payload;
71     if (!data.ReadUint32(type) || !data.ReadString(payload)) {
72         CONCUR_LOGE("Read info failed in ReportSceneInfoInner Stub");
73         return IPC_STUB_ERR;
74     }
75     if (payload.empty()) {
76         return ERR_OK;
77     }
78     ReportSceneInfo(type, StringToJson(payload));
79     return ERR_OK;
80 }
81 
QueryIntervalInner(MessageParcel & data,MessageParcel & reply)82 int32_t ConcurrentTaskServiceStub::QueryIntervalInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
83 {
84     if (!IsValidToken(data)) {
85         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
86     }
87     int item;
88     IntervalReply queryRs;
89     queryRs.rtgId = -1;
90     queryRs.tid = -1;
91     queryRs.paramA = -1;
92     queryRs.paramB = -1;
93     if (!data.ReadInt32(item) || !data.ReadInt32(queryRs.tid) || !data.ReadInt32(queryRs.paramA)) {
94         CONCUR_LOGE("Read info failed in QueryInterval Stub");
95         return IPC_STUB_ERR;
96     }
97     QueryInterval(item, queryRs);
98     if (!reply.WriteInt32(queryRs.rtgId) || !reply.WriteInt32(queryRs.tid)
99         || !reply.WriteInt32(queryRs.paramA) || !reply.WriteInt32(queryRs.paramB)
100         || !reply.WriteString(queryRs.bundleName)) {
101         CONCUR_LOGE("Write info failed in QueryInterval Stub");
102         return IPC_STUB_ERR;
103     }
104     return ERR_OK;
105 }
106 
QueryDeadlineInner(MessageParcel & data,MessageParcel & reply)107 int32_t ConcurrentTaskServiceStub::QueryDeadlineInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
108 {
109     if (!IsValidToken(data)) {
110         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
111     }
112     int queryItem;
113     DeadlineReply ddlReply;
114     std::string payload;
115     if (!data.ReadInt32(queryItem) || !data.ReadString(payload)) {
116         CONCUR_LOGE("Read info failed in QueryDeadline Stub");
117         return IPC_STUB_ERR;
118     }
119     if (payload.empty()) {
120         return ERR_OK;
121     }
122     QueryDeadline(queryItem, ddlReply, StringToJson(payload));
123     return ERR_OK;
124 }
125 
RequestAuthInner(MessageParcel & data,MessageParcel & reply)126 int32_t ConcurrentTaskServiceStub::RequestAuthInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
127 {
128     if (!IsValidToken(data)) {
129         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
130     }
131 
132     std::string payload;
133     if (!data.ReadString(payload)) {
134         CONCUR_LOGE("Read info failed in RequestAuth Stub");
135         return IPC_STUB_ERR;
136     }
137     if (payload.empty()) {
138         return ERR_OK;
139     }
140     RequestAuth(StringToJson(payload));
141     return ERR_OK;
142 }
143 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)144 int32_t ConcurrentTaskServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
145     MessageParcel& reply, MessageOption& option)
146 {
147     auto uid = IPCSkeleton::GetCallingUid();
148     auto pid = IPCSkeleton::GetCallingPid();
149     CONCUR_LOGD("ConcurrentTaskServiceStub::OnRemoteRequest, code = %{public}u, flags = %{public}d,"
150         " uid = %{public}d pid = %{public}d", code, option.GetFlags(), uid, pid);
151 
152     auto itFunc = funcMap_.find(code);
153     if (itFunc != funcMap_.end()) {
154         auto requestFunc = itFunc->second;
155         if (requestFunc) {
156             return requestFunc(data, reply);
157         }
158     }
159     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
160 }
161 
StringToJson(const std::string & payload)162 Json::Value ConcurrentTaskServiceStub::StringToJson(const std::string& payload)
163 {
164     bool res;
165     Json::CharReaderBuilder readerBuilder;
166     JSONCPP_STRING errs;
167     std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
168     Json::Value root;
169     if (!IsAsciiString(payload)) {
170         CONCUR_LOGE("Payload is not ascii string");
171         return root;
172     }
173     try {
174         res = jsonReader->parse(payload.c_str(), payload.c_str() + payload.length(), &root, &errs);
175     } catch (...) {
176         CONCUR_LOGE("Unexpected json parse");
177         return root;
178     }
179     if (!res || !errs.empty()) {
180         CONCUR_LOGE("ConcurentTaskServiceStub::payload = %{public}s Incorrect JSON format ", payload.c_str());
181     }
182     return root;
183 }
184 
Init()185 void ConcurrentTaskServiceStub::Init()
186 {
187     funcMap_ = {
188         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::REPORT_DATA),
189             [this](auto& data, auto& reply) {return ReportDataInner(data, reply); } },
190         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::REPORT_SCENE_INFO),
191             [this](auto& data, auto& reply) {return ReportSceneInfoInner(data, reply); } },
192         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::QUERY_INTERVAL),
193             [this](auto& data, auto& reply) {return QueryIntervalInner(data, reply); } },
194         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::QUERY_DEADLINE),
195             [this](auto& data, auto& reply) {return QueryDeadlineInner(data, reply); } },
196         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::REQUEST_AUTH),
197             [this](auto& data, auto& reply) {return RequestAuthInner(data, reply); } },
198     };
199 }
200 } // namespace ResourceSchedule
201 } // namespace OHOS
202