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