1 /*
2  * Copyright (c) 2023-2024 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_foreground_state_observer_stub.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "hilog_tag_wrapper.h"
20 #include "ipc_types.h"
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
AppForegroundStateObserverStub()25 AppForegroundStateObserverStub::AppForegroundStateObserverStub() {}
26 
~AppForegroundStateObserverStub()27 AppForegroundStateObserverStub::~AppForegroundStateObserverStub() {}
28 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int32_t AppForegroundStateObserverStub::OnRemoteRequest(
30     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     TAG_LOGD(AAFwkTag::APPMGR, "called");
33     std::u16string descriptor = AppForegroundStateObserverStub::GetDescriptor();
34     std::u16string remoteDescriptor = data.ReadInterfaceToken();
35     if (descriptor != remoteDescriptor) {
36         TAG_LOGE(AAFwkTag::APPMGR, "Local descriptor is not equal to remote.");
37         return ERR_INVALID_STATE;
38     }
39 
40     if (code == static_cast<uint32_t>(IAppForegroundStateObserver::Message::ON_APP_STATE_CHANGED)) {
41         return HandleOnAppStateChanged(data, reply);
42     }
43 
44     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45 }
46 
HandleOnAppStateChanged(MessageParcel & data,MessageParcel & reply)47 int32_t AppForegroundStateObserverStub::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply)
48 {
49     std::unique_ptr<AppStateData> processData(data.ReadParcelable<AppStateData>());
50     if (processData == nullptr) {
51         TAG_LOGE(AAFwkTag::APPMGR, "ProcessData is null.");
52         return ERR_APPEXECFWK_PARCEL_ERROR;
53     }
54 
55     OnAppStateChanged(*processData);
56     return NO_ERROR;
57 }
58 
AppForegroundStateObserverRecipient(RemoteDiedHandler handler)59 AppForegroundStateObserverRecipient::AppForegroundStateObserverRecipient(RemoteDiedHandler handler) : handler_(handler)
60 {}
61 
OnRemoteDied(const wptr<IRemoteObject> & remote)62 void AppForegroundStateObserverRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
63 {
64     TAG_LOGE(AAFwkTag::APPMGR, "Remote died.");
65     if (handler_) {
66         handler_(remote);
67     }
68 }
69 } // namespace AppExecFwk
70 } // namespace OHOS