• 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 "res_sched_service_stub.h"
17 #include "res_sched_log.h"
18 #include "res_sched_errors.h"
19 #include "ipc_skeleton.h"
20 #include "ipc_util.h"
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
24 namespace {
IsValidToken(MessageParcel & data)25     bool IsValidToken(MessageParcel& data)
26     {
27         std::u16string descriptor = ResSchedServiceStub::GetDescriptor();
28         std::u16string remoteDescriptor = data.ReadInterfaceToken();
29         return descriptor == remoteDescriptor;
30     }
31 }
32 
ResSchedServiceStub()33 ResSchedServiceStub::ResSchedServiceStub()
34 {
35     Init();
36 }
37 
~ResSchedServiceStub()38 ResSchedServiceStub::~ResSchedServiceStub()
39 {
40     funcMap_.clear();
41 }
42 
ReportDataInner(MessageParcel & data,MessageParcel & reply)43 int32_t ResSchedServiceStub::ReportDataInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)
44 {
45     if (!IsValidToken(data)) {
46         return ERR_RES_SCHED_PARCEL_ERROR;
47     }
48     uint32_t type = 0;
49     READ_PARCEL(data, Uint32, type, ERR_RES_SCHED_PARCEL_ERROR, ResSchedServiceStub);
50 
51     int64_t value = 0;
52     READ_PARCEL(data, Int64, value, ERR_RES_SCHED_PARCEL_ERROR, ResSchedServiceStub);
53 
54     std::string payload;
55     READ_PARCEL(data, String, payload, ERR_RES_SCHED_PARCEL_ERROR, ResSchedServiceStub);
56 
57     ReportData(type, value, StringToJsonObj(payload));
58     return ERR_OK;
59 }
60 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)61 int32_t ResSchedServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
62     MessageParcel &reply, MessageOption &option)
63 {
64     auto uid = IPCSkeleton::GetCallingUid();
65     RESSCHED_LOGD("ResSchedServiceStub::OnRemoteRequest, code = %{public}u, flags = %{public}d,"
66         " uid = %{public}d", code, option.GetFlags(), uid);
67 
68     auto itFunc = funcMap_.find(code);
69     if (itFunc != funcMap_.end()) {
70         auto requestFunc = itFunc->second;
71         if (requestFunc) {
72             return requestFunc(data, reply);
73         }
74     }
75     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76 }
77 
StringToJsonObj(const std::string & payload)78 nlohmann::json ResSchedServiceStub::StringToJsonObj(const std::string& payload)
79 {
80     nlohmann::json jsonObj = nlohmann::json::object();
81     if (payload.empty()) {
82         return jsonObj;
83     }
84     nlohmann::json jsonTmp = nlohmann::json::parse(payload, nullptr, false);
85     if (jsonTmp.is_discarded()) {
86         RESSCHED_LOGE("%{public}s parse payload to json failed: %{public}s", __func__, payload.c_str());
87         return jsonObj;
88     }
89     if (!jsonTmp.is_object()) {
90         RESSCHED_LOGE("%{public}s payload converted result is not a jsonObj: %{public}s", __func__, payload.c_str());
91         return jsonObj;
92     }
93     return jsonTmp;
94 }
95 
Init()96 void ResSchedServiceStub::Init()
97 {
98     funcMap_ = {
99         { REPORT_DATA,
100             [this](auto& data, auto& reply) {return ReportDataInner(data, reply); } },
101     };
102 }
103 } // namespace ResourceSchedule
104 } // namespace OHOS
105