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 "token_callback_stub.h"
17
18 #include "accesstoken_log.h"
19 #include "string_ex.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "TokenCallbackStub"
27 };
28 static const int32_t LIST_SIZE_MAX = 200;
29 static const int32_t FAILED = -1;
30 }
31
to_utf8(std::u16string str16)32 static std::string to_utf8(std::u16string str16)
33 {
34 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(str16);
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t TokenCallbackStub::OnRemoteRequest(
38 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
39 {
40 ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, code: 0x%{public}x", code);
41 std::u16string descriptor = data.ReadInterfaceToken();
42 if (descriptor != ITokenCallback::GetDescriptor()) {
43 ACCESSTOKEN_LOG_ERROR(LABEL, "get unexpect descriptor: %{public}s", Str16ToStr8(descriptor).c_str());
44 return FAILED;
45 }
46
47 int32_t msgCode = static_cast<int32_t>(code);
48 if (msgCode == ITokenCallback::GRANT_RESULT_CALLBACK) {
49 uint32_t permListSize = data.ReadUint32();
50 if (permListSize > LIST_SIZE_MAX) {
51 ACCESSTOKEN_LOG_ERROR(LABEL, "read permListSize fail %{public}u", permListSize);
52 return FAILED;
53 }
54 std::vector<std::string> permList;
55 for (uint32_t i = 0; i < permListSize; i++) {
56 std::u16string u16Perm = data.ReadString16();
57 std::string perm = to_utf8(u16Perm);
58 permList.emplace_back(perm);
59 }
60
61 uint32_t statusListSize = data.ReadUint32();
62 if (statusListSize != permListSize) {
63 ACCESSTOKEN_LOG_ERROR(LABEL, "read statusListSize fail %{public}u", statusListSize);
64 return FAILED;
65 }
66 std::vector<int32_t> grantResults;
67 for (uint32_t i = 0; i < permListSize; i++) {
68 int32_t res = data.ReadInt32();
69 grantResults.emplace_back(res);
70 }
71 GrantResultsCallback(permList, grantResults);
72 } else {
73 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75 return 0;
76 }
77 } // namespace AccessToken
78 } // namespace Security
79 } // namespace OHOS