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_FOREGROUND_APPLICATION_CHANGED: {
48 HandleOnForegroundApplicationChanged(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 default: {
56 ACCESSTOKEN_LOG_DEBUG(LABEL, "default case, need check AudioListenerStub");
57 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58 }
59 }
60 return NO_ERROR;
61 }
62
HandleOnForegroundApplicationChanged(MessageParcel & data,MessageParcel & reply)63 int32_t ApplicationStateObserverStub::HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply)
64 {
65 std::unique_ptr<AppStateData> processData(data.ReadParcelable<AppStateData>());
66 if (processData == nullptr) {
67 ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
68 return -1;
69 }
70
71 OnForegroundApplicationChanged(*processData);
72 return NO_ERROR;
73 }
74
HandleOnProcessDied(MessageParcel & data,MessageParcel & reply)75 int32_t ApplicationStateObserverStub::HandleOnProcessDied(MessageParcel &data, MessageParcel &reply)
76 {
77 std::unique_ptr<ProcessData> processData(data.ReadParcelable<ProcessData>());
78 if (processData == nullptr) {
79 ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
80 return -1;
81 }
82
83 OnProcessDied(*processData);
84 return NO_ERROR;
85 }
86 } // namespace AccessToken
87 } // namespace Security
88 } // namespace OHOS
89