1 /*
2 * Copyright (c) 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 "start_specified_ability_response_stub.h"
17 #include "appexecfwk_errors.h"
18 #include "hilog_wrapper.h"
19 #include "ipc_types.h"
20 #include "iremote_object.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 using namespace std::placeholders;
StartSpecifiedAbilityResponseStub()25 StartSpecifiedAbilityResponseStub::StartSpecifiedAbilityResponseStub()
26 {
27 auto a = std::bind(&StartSpecifiedAbilityResponseStub::HandleOnAcceptWantResponse, this, _1, _2);
28 responseFuncMap_.emplace(static_cast<uint32_t>(
29 IStartSpecifiedAbilityResponse::Message::ON_ACCEPT_WANT_RESPONSE), std::move(a));
30 auto b = std::bind(&StartSpecifiedAbilityResponseStub::HandleOnTimeoutResponse, this, _1, _2);
31 responseFuncMap_.emplace(static_cast<uint32_t>(
32 IStartSpecifiedAbilityResponse::Message::ON_TIMEOUT_RESPONSE), std::move(b));
33 }
34
~StartSpecifiedAbilityResponseStub()35 StartSpecifiedAbilityResponseStub::~StartSpecifiedAbilityResponseStub()
36 {
37 responseFuncMap_.clear();
38 }
39
HandleOnAcceptWantResponse(MessageParcel & data,MessageParcel & reply)40 int32_t StartSpecifiedAbilityResponseStub::HandleOnAcceptWantResponse(MessageParcel &data, MessageParcel &reply)
41 {
42 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
43 if (want == nullptr) {
44 HILOG_ERROR("want is nullptr");
45 return ERR_INVALID_VALUE;
46 }
47
48 auto flag = Str16ToStr8(data.ReadString16());
49 OnAcceptWantResponse(*want, flag);
50 delete want;
51 return NO_ERROR;
52 }
53
HandleOnTimeoutResponse(MessageParcel & data,MessageParcel & reply)54 int32_t StartSpecifiedAbilityResponseStub::HandleOnTimeoutResponse(MessageParcel &data, MessageParcel &reply)
55 {
56 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
57 if (want == nullptr) {
58 HILOG_ERROR("want is nullptr");
59 return ERR_INVALID_VALUE;
60 }
61
62 OnTimeoutResponse(*want);
63 delete want;
64 return NO_ERROR;
65 }
66
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int StartSpecifiedAbilityResponseStub::OnRemoteRequest(
68 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
69 {
70 HILOG_INFO("StartSpecifiedAbilityResponseStub::OnReceived, code = %{public}u, flags= %{public}d.",
71 code, option.GetFlags());
72 std::u16string descriptor = StartSpecifiedAbilityResponseStub::GetDescriptor();
73 std::u16string remoteDescriptor = data.ReadInterfaceToken();
74 if (descriptor != remoteDescriptor) {
75 HILOG_ERROR("local descriptor is not equal to remote");
76 return ERR_INVALID_STATE;
77 }
78
79 auto itFunc = responseFuncMap_.find(code);
80 if (itFunc != responseFuncMap_.end()) {
81 auto func = itFunc->second;
82 if (func != nullptr) {
83 return func(data, reply);
84 }
85 }
86 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
87 }
88 } // namespace AppExecFwk
89 } // namespace OHOS
90