• 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     uint32_t type = 0;
49     if (!data.ReadUint32(type)) {
50         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
51     }
52 
53     int64_t value = 0;
54     if (!data.ReadInt64(value)) {
55         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
56     }
57 
58     std::string payload;
59     if (!data.ReadString(payload)) {
60         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
61     }
62     if (payload.empty()) {
63         return ERR_OK;
64     }
65     ReportData(type, value, StringToJson(payload));
66     return ERR_OK;
67 }
68 
QueryIntervalInner(MessageParcel & data,MessageParcel & reply)69 int32_t ConcurrentTaskServiceStub::QueryIntervalInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
70 {
71     if (!IsValidToken(data)) {
72         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
73     }
74     int item;
75     if (!data.ReadInt32(item)) {
76         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
77     }
78     IntervalReply queryRs;
79     queryRs.rtgId = -1;
80     queryRs.paramA = -1;
81     queryRs.paramB = -1;
82     queryRs.paramC = -1;
83     QueryInterval(item, queryRs);
84     if (!reply.WriteInt32(queryRs.rtgId)) {
85         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
86     }
87     if (!reply.WriteInt32(queryRs.paramA)) {
88         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
89     }
90     if (!reply.WriteInt32(queryRs.paramB)) {
91         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
92     }
93     if (!reply.WriteInt32(queryRs.paramC)) {
94         return ERR_CONCURRENT_TASK_PARCEL_ERROR;
95     }
96     return ERR_OK;
97 }
98 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)99 int32_t ConcurrentTaskServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
100     MessageParcel& reply, MessageOption& option)
101 {
102     auto uid = IPCSkeleton::GetCallingUid();
103     auto pid = IPCSkeleton::GetCallingPid();
104     CONCUR_LOGD("ConcurrentTaskServiceStub::OnRemoteRequest, code = %{public}u, flags = %{public}d,"
105         " uid = %{public}d pid = %{public}d", code, option.GetFlags(), uid, pid);
106 
107     auto itFunc = funcMap_.find(code);
108     if (itFunc != funcMap_.end()) {
109         auto requestFunc = itFunc->second;
110         if (requestFunc) {
111             return requestFunc(data, reply);
112         }
113     }
114     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
115 }
116 
StringToJson(const std::string & payload)117 Json::Value ConcurrentTaskServiceStub::StringToJson(const std::string& payload)
118 {
119     bool res;
120     Json::CharReaderBuilder readerBuilder;
121     JSONCPP_STRING errs;
122     std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
123     Json::Value root;
124     if (!IsAsciiString(payload)) {
125         CONCUR_LOGE("Payload is not ascii string");
126         return root;
127     }
128     try {
129         res = jsonReader->parse(payload.c_str(), payload.c_str() + payload.length(), &root, &errs);
130     } catch (...) {
131         CONCUR_LOGE("Unexpected json parse");
132         return root;
133     }
134     if (!res || !errs.empty()) {
135         CONCUR_LOGE("ConcurentTaskServiceStub::payload = %{public}s Incorrect JSON format ", payload.c_str());
136     }
137     return root;
138 }
139 
Init()140 void ConcurrentTaskServiceStub::Init()
141 {
142     funcMap_ = {
143         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::REPORT_DATA),
144             [this](auto& data, auto& reply) {return ReportDataInner(data, reply); } },
145         { static_cast<uint32_t>(ConcurrentTaskInterfaceCode::QUERY_INTERVAL),
146             [this](auto& data, auto& reply) {return QueryIntervalInner(data, reply); } },
147     };
148 }
149 } // namespace ResourceSchedule
150 } // namespace OHOS
151