1 /*
2 * Copyright (c) 2021-2022 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_state_callback_host.h"
17
18 #include "appexecfwk_errors.h"
19 #include "hitrace_meter.h"
20 #include "hilog_wrapper.h"
21 #include "ipc_types.h"
22 #include "iremote_object.h"
23
24 #include "app_state_callback_proxy.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
AppStateCallbackHost()28 AppStateCallbackHost::AppStateCallbackHost()
29 {
30 memberFuncMap_[static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_STATE_CHANGED)] =
31 &AppStateCallbackHost::HandleOnAppStateChanged;
32 memberFuncMap_[static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_ABILITY_REQUEST_DONE)] =
33 &AppStateCallbackHost::HandleOnAbilityRequestDone;
34 }
35
~AppStateCallbackHost()36 AppStateCallbackHost::~AppStateCallbackHost()
37 {
38 memberFuncMap_.clear();
39 }
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int AppStateCallbackHost::OnRemoteRequest(
42 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44 HILOG_INFO("AppStateCallbackHost::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
45 std::u16string descriptor = AppStateCallbackHost::GetDescriptor();
46 std::u16string remoteDescriptor = data.ReadInterfaceToken();
47 if (descriptor != remoteDescriptor) {
48 HILOG_ERROR("local descriptor is not equal to remote");
49 return ERR_INVALID_STATE;
50 }
51
52 auto itFunc = memberFuncMap_.find(code);
53 if (itFunc != memberFuncMap_.end()) {
54 auto memberFunc = itFunc->second;
55 if (memberFunc != nullptr) {
56 return (this->*memberFunc)(data, reply);
57 }
58 }
59 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61
OnAbilityRequestDone(const sptr<IRemoteObject> &,const AbilityState)62 void AppStateCallbackHost::OnAbilityRequestDone(const sptr<IRemoteObject> &, const AbilityState)
63 {
64 HILOG_DEBUG("called");
65 }
66
OnAppStateChanged(const AppProcessData &)67 void AppStateCallbackHost::OnAppStateChanged(const AppProcessData &)
68 {
69 HILOG_DEBUG("called");
70 }
71
HandleOnAppStateChanged(MessageParcel & data,MessageParcel & reply)72 int32_t AppStateCallbackHost::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply)
73 {
74 HITRACE_METER(HITRACE_TAG_APP);
75 std::unique_ptr<AppProcessData> processData(data.ReadParcelable<AppProcessData>());
76 if (!processData) {
77 HILOG_ERROR("ReadParcelable<AppProcessData> failed");
78 return ERR_APPEXECFWK_PARCEL_ERROR;
79 }
80
81 OnAppStateChanged(*processData);
82 return NO_ERROR;
83 }
84
HandleOnAbilityRequestDone(MessageParcel & data,MessageParcel & reply)85 int32_t AppStateCallbackHost::HandleOnAbilityRequestDone(MessageParcel &data, MessageParcel &reply)
86 {
87 HITRACE_METER(HITRACE_TAG_APP);
88 sptr<IRemoteObject> obj = nullptr;
89 if (data.ReadBool()) {
90 obj = data.ReadRemoteObject();
91 }
92 int32_t state = data.ReadInt32();
93 OnAbilityRequestDone(obj, static_cast<AbilityState>(state));
94 return NO_ERROR;
95 }
96 } // namespace AppExecFwk
97 } // namespace OHOS
98