• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "os_account_event_stub.h"
17 
18 #include "account_log_wrapper.h"
19 #include "ipc_skeleton.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 const uid_t ACCOUNT_UID = 3058;
25 }
26 
OsAccountEventStub()27 OsAccountEventStub::OsAccountEventStub()
28 {}
29 
~OsAccountEventStub()30 OsAccountEventStub::~OsAccountEventStub()
31 {}
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int OsAccountEventStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
34 {
35     int32_t callingUid = IPCSkeleton::GetCallingUid();
36     if (callingUid != ACCOUNT_UID) {
37         ACCOUNT_LOGE("Permission denied, callingUid: %{public}d", callingUid);
38         return ERR_ACCOUNT_COMMON_PERMISSION_DENIED;
39     }
40     if (data.ReadInterfaceToken() != GetDescriptor()) {
41         ACCOUNT_LOGE("check descriptor failed! code %{public}u.", code);
42         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
43     }
44 
45     switch (code) {
46         case static_cast<uint32_t>(OsAccountEventInterfaceCode::ON_STATE_CHANGED): {
47             std::shared_ptr<OsAccountStateParcel> stateParcelPtr(data.ReadParcelable<OsAccountStateParcel>());
48             if (stateParcelPtr == nullptr) {
49                 ACCOUNT_LOGE("Failed to read state parcel");
50                 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
51             }
52             return OnStateChanged(*stateParcelPtr);
53         }
54         case static_cast<uint32_t>(OsAccountEventInterfaceCode::ACCOUNT_CHANGED): {
55             int id;
56             if (!data.ReadInt32(id)) {
57                 ACCOUNT_LOGE("failed to read localId");
58                 return ERR_OSACCOUNT_KIT_READ_IN_LOCAL_ID_ERROR;
59             }
60             OnAccountsChanged(id);
61             break;
62         }
63         case static_cast<uint32_t>(OsAccountEventInterfaceCode::ACCOUNT_SWITCHED): {
64             int newId;
65             if (!data.ReadInt32(newId)) {
66                 ACCOUNT_LOGE("Read newId failed.");
67                 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
68             }
69             int oldId;
70             if (!data.ReadInt32(oldId)) {
71                 ACCOUNT_LOGE("Read oldId failed.");
72                 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
73             }
74             OnAccountsSwitch(newId, oldId);
75             break;
76         }
77         default:
78             ACCOUNT_LOGI("default, code = %{public}u, flags = %{public}u", code, option.GetFlags());
79             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
80     }
81 
82     return ERR_OK;
83 }
84 }  // namespace AccountSA
85 }  // namespace OHOS
86