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 handleOnAcceptWantResponse =
28 std::bind(&StartSpecifiedAbilityResponseStub::HandleOnAcceptWantResponse, this, _1, _2);
29 responseFuncMap_.emplace(static_cast<uint32_t>(
30 IStartSpecifiedAbilityResponse::Message::ON_ACCEPT_WANT_RESPONSE),
31 std::move(handleOnAcceptWantResponse));
32
33 auto handleOnTimeoutResponse =
34 std::bind(&StartSpecifiedAbilityResponseStub::HandleOnTimeoutResponse, this, _1, _2);
35 responseFuncMap_.emplace(static_cast<uint32_t>(
36 IStartSpecifiedAbilityResponse::Message::ON_TIMEOUT_RESPONSE),
37 std::move(handleOnTimeoutResponse));
38
39 auto handleOnNewProcessRequestResponse =
40 std::bind(&StartSpecifiedAbilityResponseStub::HandleOnNewProcessRequestResponse, this, _1, _2);
41 responseFuncMap_.emplace(static_cast<uint32_t>(
42 IStartSpecifiedAbilityResponse::Message::ON_NEW_PROCESS_REQUEST_RESPONSE),
43 std::move(handleOnNewProcessRequestResponse));
44
45 auto handleOnNewProcessRequestTimeoutResponse =
46 std::bind(&StartSpecifiedAbilityResponseStub::HandleOnNewProcessRequestTimeoutResponse, this, _1, _2);
47 responseFuncMap_.emplace(static_cast<uint32_t>(
48 IStartSpecifiedAbilityResponse::Message::ON_NEW_PROCESS_REQUEST_TIMEOUT_RESPONSE),
49 std::move(handleOnNewProcessRequestTimeoutResponse));
50 }
51
~StartSpecifiedAbilityResponseStub()52 StartSpecifiedAbilityResponseStub::~StartSpecifiedAbilityResponseStub()
53 {
54 responseFuncMap_.clear();
55 }
56
HandleOnAcceptWantResponse(MessageParcel & data,MessageParcel & reply)57 int32_t StartSpecifiedAbilityResponseStub::HandleOnAcceptWantResponse(MessageParcel &data, MessageParcel &reply)
58 {
59 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
60 if (want == nullptr) {
61 HILOG_ERROR("want is nullptr");
62 return ERR_INVALID_VALUE;
63 }
64
65 auto flag = Str16ToStr8(data.ReadString16());
66 OnAcceptWantResponse(*want, flag);
67 delete want;
68 return NO_ERROR;
69 }
70
HandleOnTimeoutResponse(MessageParcel & data,MessageParcel & reply)71 int32_t StartSpecifiedAbilityResponseStub::HandleOnTimeoutResponse(MessageParcel &data, MessageParcel &reply)
72 {
73 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
74 if (want == nullptr) {
75 HILOG_ERROR("want is nullptr");
76 return ERR_INVALID_VALUE;
77 }
78
79 OnTimeoutResponse(*want);
80 delete want;
81 return NO_ERROR;
82 }
83
HandleOnNewProcessRequestResponse(MessageParcel & data,MessageParcel & reply)84 int32_t StartSpecifiedAbilityResponseStub::HandleOnNewProcessRequestResponse(MessageParcel &data, MessageParcel &reply)
85 {
86 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
87 if (want == nullptr) {
88 HILOG_ERROR("want is nullptr");
89 return ERR_INVALID_VALUE;
90 }
91
92 auto flag = Str16ToStr8(data.ReadString16());
93 OnNewProcessRequestResponse(*want, flag);
94 delete want;
95 return NO_ERROR;
96 }
97
HandleOnNewProcessRequestTimeoutResponse(MessageParcel & data,MessageParcel & reply)98 int32_t StartSpecifiedAbilityResponseStub::HandleOnNewProcessRequestTimeoutResponse(MessageParcel &data,
99 MessageParcel &reply)
100 {
101 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
102 if (want == nullptr) {
103 HILOG_ERROR("want is nullptr");
104 return ERR_INVALID_VALUE;
105 }
106
107 OnNewProcessRequestTimeoutResponse(*want);
108 delete want;
109 return NO_ERROR;
110 }
111
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)112 int StartSpecifiedAbilityResponseStub::OnRemoteRequest(
113 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
114 {
115 HILOG_INFO("StartSpecifiedAbilityResponseStub::OnReceived, code = %{public}u, flags= %{public}d.",
116 code, option.GetFlags());
117 std::u16string descriptor = StartSpecifiedAbilityResponseStub::GetDescriptor();
118 std::u16string remoteDescriptor = data.ReadInterfaceToken();
119 if (descriptor != remoteDescriptor) {
120 HILOG_ERROR("local descriptor is not equal to remote");
121 return ERR_INVALID_STATE;
122 }
123
124 auto itFunc = responseFuncMap_.find(code);
125 if (itFunc != responseFuncMap_.end()) {
126 auto func = itFunc->second;
127 if (func != nullptr) {
128 return func(data, reply);
129 }
130 }
131 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
132 }
133 } // namespace AppExecFwk
134 } // namespace OHOS
135