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 "on_permission_used_record_callback_stub.h"
17 
18 #include "accesstoken_log.h"
19 #include "privacy_error.h"
20 #include "permission_used_result_parcel.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
28     LOG_CORE, SECURITY_DOMAIN_PRIVACY, "OnPermissionUsedRecordCallbackStub"
29 };
30 static constexpr int32_t RET_NOK = -1;
31 }
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t OnPermissionUsedRecordCallbackStub::OnRemoteRequest(
34     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
35 {
36     ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, code: 0x%{public}x", code);
37     std::u16string descriptor = data.ReadInterfaceToken();
38     if (descriptor != OnPermissionUsedRecordCallback::GetDescriptor()) {
39         ACCESSTOKEN_LOG_ERROR(LABEL, "Get unexpect descriptor: %{public}s", Str16ToStr8(descriptor).c_str());
40         return ERROR_IPC_REQUEST_FAIL;
41     }
42 
43     int32_t msgCode =  static_cast<int32_t>(code);
44     if (msgCode == static_cast<int32_t>(PrivacyPermissionRecordInterfaceCode::ON_QUERIED)) {
45         ErrCode errCode = data.ReadInt32();
46         PermissionUsedResult result;
47         if (errCode != NO_ERROR) {
48             OnQueried(errCode, result);
49             return RET_NOK;
50         }
51         sptr<PermissionUsedResultParcel> resultSptr = data.ReadParcelable<PermissionUsedResultParcel>();
52         if (resultSptr == nullptr) {
53             ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail");
54             return RET_NOK;
55         }
56         ACCESSTOKEN_LOG_INFO(LABEL, "ErrCode: %{public}d", errCode);
57         OnQueried(errCode, resultSptr->result);
58     } else {
59         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60     }
61     return NO_ERROR;
62 }
63 } // namespace AccessToken
64 } // namespace Security
65 } // namespace OHOS
66