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 
16 #include "system_process_status_change_stub.h"
17 
18 #include "errors.h"
19 #include "ipc_object_stub.h"
20 #include "ipc_types.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "refbase.h"
24 #include "sam_log.h"
25 
26 namespace OHOS {
SystemProcessStatusChangeStub()27 SystemProcessStatusChangeStub::SystemProcessStatusChangeStub()
28 {
29     memberFuncMap_[ON_SYSTEM_PROCESS_STARTED] =
30         SystemProcessStatusChangeStub::LocalSystemProcessStarted;
31     memberFuncMap_[ON_SYSTEM_PROCESS_STOPPED] =
32         SystemProcessStatusChangeStub::LocalSystemProcessStopped;
33 }
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t SystemProcessStatusChangeStub::OnRemoteRequest(uint32_t code,
36     MessageParcel& data, MessageParcel& reply, MessageOption& option)
37 {
38     HILOGD("ProcStaChange,code:%{public}u,flags:%{public}d", code, option.GetFlags());
39     if (!EnforceInterceToken(data)) {
40         HILOGW("check interface token failed!");
41         return ERR_PERMISSION_DENIED;
42     }
43     auto iter = memberFuncMap_.find(code);
44     if (iter != memberFuncMap_.end()) {
45         return iter->second(this, data, reply);
46     }
47     HILOGW("unknown request code!");
48     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49 }
50 
OnSystemProcessStartedInner(MessageParcel & data,MessageParcel & reply)51 int32_t SystemProcessStatusChangeStub::OnSystemProcessStartedInner(MessageParcel& data, MessageParcel& reply)
52 {
53     SystemProcessInfo systemProcessInfo;
54     systemProcessInfo.processName = data.ReadString();
55     if (systemProcessInfo.processName.empty()) {
56         HILOGW("read processName failed!");
57         return ERR_NULL_OBJECT;
58     }
59     systemProcessInfo.pid = data.ReadInt32();
60     HILOGI("onProcStart,pid:%{public}d", systemProcessInfo.pid);
61     OnSystemProcessStarted(systemProcessInfo);
62     return ERR_NONE;
63 }
64 
OnSystemProcessStoppedInner(MessageParcel & data,MessageParcel & reply)65 int32_t SystemProcessStatusChangeStub::OnSystemProcessStoppedInner(MessageParcel& data, MessageParcel& reply)
66 {
67     SystemProcessInfo systemProcessInfo;
68     systemProcessInfo.processName = data.ReadString();
69     if (systemProcessInfo.processName.empty()) {
70         HILOGW("read processName failed!");
71         return ERR_NULL_OBJECT;
72     }
73     systemProcessInfo.pid = data.ReadInt32();
74     HILOGI("onProcStop,pid:%{public}d", systemProcessInfo.pid);
75     OnSystemProcessStopped(systemProcessInfo);
76     return ERR_NONE;
77 }
78 
EnforceInterceToken(MessageParcel & data)79 bool SystemProcessStatusChangeStub::EnforceInterceToken(MessageParcel& data)
80 {
81     std::u16string interfaceToken = data.ReadInterfaceToken();
82     return interfaceToken == GetDescriptor();
83 }
84 }
85