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 "ability_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 {
AbilityForegroundStateObserverStub()25 AbilityForegroundStateObserverStub::AbilityForegroundStateObserverStub() {}
26 
~AbilityForegroundStateObserverStub()27 AbilityForegroundStateObserverStub::~AbilityForegroundStateObserverStub() {}
28 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int32_t AbilityForegroundStateObserverStub::OnRemoteRequest(
30     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     TAG_LOGD(AAFwkTag::APPMGR, "called");
33     std::u16string descriptor = AbilityForegroundStateObserverStub::GetDescriptor();
34     std::u16string remoteDescriptor = data.ReadInterfaceToken();
35     if (descriptor != remoteDescriptor) {
36         TAG_LOGE(AAFwkTag::APPMGR, "invalid descriptor");
37         return ERR_INVALID_STATE;
38     }
39 
40     if (code == static_cast<uint32_t>(IAbilityForegroundStateObserver::Message::ON_ABILITY_STATE_CHANGED)) {
41         return HandleOnAbilityStateChanged(data, reply);
42     }
43     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
44 }
45 
HandleOnAbilityStateChanged(MessageParcel & data,MessageParcel & reply)46 int32_t AbilityForegroundStateObserverStub::HandleOnAbilityStateChanged(MessageParcel &data, MessageParcel &reply)
47 {
48     std::unique_ptr<AbilityStateData> processData(data.ReadParcelable<AbilityStateData>());
49     if (processData == nullptr) {
50         TAG_LOGE(AAFwkTag::APPMGR, "null processData");
51         return ERR_APPEXECFWK_PARCEL_ERROR;
52     }
53 
54     OnAbilityStateChanged(*processData);
55     return NO_ERROR;
56 }
57 
AbilityForegroundStateObserverRecipient(RemoteDiedHandler handler)58 AbilityForegroundStateObserverRecipient::AbilityForegroundStateObserverRecipient(RemoteDiedHandler handler)
59     : handler_(handler)
60 {}
61 
OnRemoteDied(const wptr<IRemoteObject> & remote)62 void AbilityForegroundStateObserverRecipient::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