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