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 "ability_manager_access_proxy.h"
17 #include "access_token_error.h"
18 #include "accesstoken_log.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AbilityManagerAccessProxy"};
25 }
26
StartAbility(const AAFwk::Want & want,const sptr<IRemoteObject> & callerToken,int requestCode,int32_t userId)27 int AbilityManagerAccessProxy::StartAbility(const AAFwk::Want &want, const sptr<IRemoteObject> &callerToken,
28 int requestCode, int32_t userId)
29 {
30 MessageParcel data;
31 MessageParcel reply;
32 MessageOption option;
33
34 if (!data.WriteInterfaceToken(GetDescriptor())) {
35 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
36 return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
37 }
38
39 if (!data.WriteParcelable(&want)) {
40 ACCESSTOKEN_LOG_ERROR(LABEL, "Want write failed.");
41 return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
42 }
43 if (callerToken) {
44 if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
45 ACCESSTOKEN_LOG_ERROR(LABEL, "CallerToken and flag write failed.");
46 return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
47 }
48 } else {
49 if (!data.WriteBool(false)) {
50 ACCESSTOKEN_LOG_ERROR(LABEL, "Flag write failed.");
51 return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
52 }
53 }
54 if (!data.WriteInt32(userId)) {
55 ACCESSTOKEN_LOG_ERROR(LABEL, "UserId write failed.");
56 return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
57 }
58 if (!data.WriteInt32(requestCode)) {
59 ACCESSTOKEN_LOG_ERROR(LABEL, "RequestCode write failed.");
60 return AccessTokenError::ERR_WRITE_PARCEL_FAILED;
61 }
62
63 sptr<IRemoteObject> remote = Remote();
64 if (remote == nullptr) {
65 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
66 return AccessTokenError::ERR_REMOTE_CONNECTION;
67 }
68 int error = remote->SendRequest(
69 static_cast<uint32_t>(AccessAbilityServiceInterfaceCode::START_ABILITY_ADD_CALLER), data, reply, option);
70 if (error != 0) {
71 ACCESSTOKEN_LOG_ERROR(LABEL, "Send request error: %{public}d", error);
72 return error;
73 }
74 return reply.ReadInt32();
75 }
76 } // namespace AccessToken
77 } // namespace Security
78 } // namespace OHOS
79