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 "component_interception_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 {
ComponentInterceptionStub()24 ComponentInterceptionStub::ComponentInterceptionStub()
25 {
26 requestFuncMap_[static_cast<uint32_t>(
27 IComponentInterception::Message::TRANSACT_ON_ALLOW_COMPONENT_START)] =
28 &ComponentInterceptionStub::HandleAllowComponentStart;
29 }
30
~ComponentInterceptionStub()31 ComponentInterceptionStub::~ComponentInterceptionStub()
32 {
33 requestFuncMap_.clear();
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int ComponentInterceptionStub::OnRemoteRequest(
37 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39 HILOG_INFO("ComponentInterceptionStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
40 std::u16string descriptor = ComponentInterceptionStub::GetDescriptor();
41 std::u16string remoteDescriptor = data.ReadInterfaceToken();
42 if (descriptor != remoteDescriptor) {
43 HILOG_ERROR("local descriptor is not equal to remote");
44 return ERR_INVALID_STATE;
45 }
46
47 auto itFunc = requestFuncMap_.find(code);
48 if (itFunc != requestFuncMap_.end()) {
49 auto requestFunc = itFunc->second;
50 if (requestFunc != nullptr) {
51 return (this->*requestFunc)(data, reply);
52 }
53 }
54 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55 }
56
AllowComponentStart(const Want & want,const sptr<IRemoteObject> & callerToken,int requestCode,int componentStatus,sptr<Want> & extraParam)57 bool ComponentInterceptionStub::AllowComponentStart(const Want &want, const sptr<IRemoteObject> &callerToken,
58 int requestCode, int componentStatus, sptr<Want> &extraParam)
59 {
60 return true;
61 }
62
HandleAllowComponentStart(MessageParcel & data,MessageParcel & reply)63 int32_t ComponentInterceptionStub::HandleAllowComponentStart(MessageParcel &data, MessageParcel &reply)
64 {
65 HILOG_INFO("HandleAllowComponentStart");
66 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
67 if (!want) {
68 HILOG_ERROR("ReadParcelable<Want> failed");
69 return ERR_APPEXECFWK_PARCEL_ERROR;
70 }
71 sptr<IRemoteObject> token = nullptr;
72 bool hasRemoteToken = data.ReadBool();
73 if (hasRemoteToken) {
74 token = data.ReadRemoteObject();
75 }
76
77 int requestCode = data.ReadInt32();
78 int componentStatus = data.ReadInt32();
79
80 sptr<Want> extraParam = new (std::nothrow) Want();
81 bool result = AllowComponentStart(*want, token, requestCode, componentStatus, extraParam);
82 if (want->GetRemoteObject(Want::PARAM_RESV_ABILITY_INFO_CALLBACK)) {
83 reply.WriteBool(true);
84 reply.WriteParcelable(extraParam);
85 } else {
86 reply.WriteBool(false);
87 }
88 reply.WriteBool(result);
89 return NO_ERROR;
90 }
91 } // namespace AppExecFwk
92 } // namespace OHOS
93