1 /*
2  * Copyright (c) 2022-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 "app_status_change_callback.h"
17 #include "accesstoken_log.h"
18 #include "access_token_error.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
25     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "ApplicationStateObserverStub"
26 };
27 }
28 
ApplicationStateObserverStub()29 ApplicationStateObserverStub::ApplicationStateObserverStub()
30 {
31     ACCESSTOKEN_LOG_INFO(LABEL, "ApplicationStateObserverStub Instance create");
32 }
33 
~ApplicationStateObserverStub()34 ApplicationStateObserverStub::~ApplicationStateObserverStub()
35 {
36     ACCESSTOKEN_LOG_INFO(LABEL, "ApplicationStateObserverStub Instance destroy");
37 }
38 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t ApplicationStateObserverStub::OnRemoteRequest(
40     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42     if (data.ReadInterfaceToken() != GetDescriptor()) {
43         ACCESSTOKEN_LOG_INFO(LABEL, "ApplicationStateObserverStub: ReadInterfaceToken failed");
44         return ERROR_IPC_REQUEST_FAIL;
45     }
46     switch (static_cast<IApplicationStateObserver::Message>(code)) {
47         case IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_STATE_CHANGED: {
48             HandleOnProcessStateChanged(data, reply);
49             return NO_ERROR;
50         }
51         case IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_DIED: {
52             HandleOnProcessDied(data, reply);
53             return NO_ERROR;
54         }
55         case IApplicationStateObserver::Message::TRANSACT_ON_APP_STATE_CHANGED: {
56             HandleOnAppStateChanged(data, reply);
57             return NO_ERROR;
58         }
59         case IApplicationStateObserver::Message::TRANSACT_ON_APP_STOPPED: {
60             HandleOnAppStopped(data, reply);
61             return NO_ERROR;
62         }
63         default: {
64             ACCESSTOKEN_LOG_DEBUG(LABEL, "Default case, need check AudioListenerStub");
65             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
66         }
67     }
68     return NO_ERROR;
69 }
70 
HandleOnProcessStateChanged(MessageParcel & data,MessageParcel & reply)71 int32_t ApplicationStateObserverStub::HandleOnProcessStateChanged(MessageParcel &data, MessageParcel &reply)
72 {
73     std::unique_ptr<ProcessData> processData(data.ReadParcelable<ProcessData>());
74     if (processData == nullptr) {
75         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
76         return -1;
77     }
78 
79     OnProcessStateChanged(*processData);
80     return NO_ERROR;
81 }
82 
HandleOnProcessDied(MessageParcel & data,MessageParcel & reply)83 int32_t ApplicationStateObserverStub::HandleOnProcessDied(MessageParcel &data, MessageParcel &reply)
84 {
85     std::unique_ptr<ProcessData> processData(data.ReadParcelable<ProcessData>());
86     if (processData == nullptr) {
87         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
88         return -1;
89     }
90 
91     OnProcessDied(*processData);
92     return NO_ERROR;
93 }
94 
HandleOnAppStateChanged(MessageParcel & data,MessageParcel & reply)95 int32_t ApplicationStateObserverStub::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply)
96 {
97     std::unique_ptr<AppStateData> processData(data.ReadParcelable<AppStateData>());
98     if (processData == nullptr) {
99         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
100         return -1;
101     }
102 
103     OnAppStateChanged(*processData);
104     return NO_ERROR;
105 }
106 
HandleOnAppStopped(MessageParcel & data,MessageParcel & reply)107 int32_t ApplicationStateObserverStub::HandleOnAppStopped(MessageParcel &data, MessageParcel &reply)
108 {
109     std::unique_ptr<AppStateData> appStateData(data.ReadParcelable<AppStateData>());
110     if (appStateData == nullptr) {
111         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
112         return -1;
113     }
114 
115     OnAppStopped(*appStateData);
116     return NO_ERROR;
117 }
118 } // namespace AccessToken
119 } // namespace Security
120 } // namespace OHOS
121