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