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 "form_manager_access_proxy.h"
17 #include "accesstoken_log.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
22 namespace {
23 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "FormManagerAccessProxy"};
24 static constexpr int32_t ERROR = -1;
25 }
26 
RegisterAddObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)27 int32_t FormManagerAccessProxy::RegisterAddObserver(
28     const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
29 {
30     MessageParcel data;
31     MessageParcel reply;
32     MessageOption option;
33     if (!data.WriteInterfaceToken(GetDescriptor())) {
34         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed.");
35         return ERROR;
36     }
37     if (!data.WriteString(bundleName)) {
38         ACCESSTOKEN_LOG_ERROR(LABEL, "Write bundleName failed.");
39         return ERROR;
40     }
41     if (!data.WriteRemoteObject(callerToken)) {
42         ACCESSTOKEN_LOG_ERROR(LABEL, "Write callerToken failed.");
43         return ERROR;
44     }
45     sptr<IRemoteObject> remote = Remote();
46     if (remote == nullptr) {
47         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
48         return ERROR;
49     }
50     int32_t error = remote->SendRequest(
51         static_cast<uint32_t>(IFormMgr::Message::FORM_MGR_REGISTER_ADD_OBSERVER), data, reply, option);
52     if (error != ERR_NONE) {
53         ACCESSTOKEN_LOG_ERROR(LABEL, "RegisterAddObserver failed, error: %{public}d", error);
54         return ERROR;
55     }
56     return reply.ReadInt32();
57 }
58 
RegisterRemoveObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)59 int32_t FormManagerAccessProxy::RegisterRemoveObserver(
60     const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
61 {
62     MessageParcel data;
63     MessageParcel reply;
64     MessageOption option;
65     if (!data.WriteInterfaceToken(GetDescriptor())) {
66         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed.");
67         return ERROR;
68     }
69     if (!data.WriteString(bundleName)) {
70         ACCESSTOKEN_LOG_ERROR(LABEL, "Write bundleName failed.");
71         return ERROR;
72     }
73     if (!data.WriteRemoteObject(callerToken)) {
74         ACCESSTOKEN_LOG_ERROR(LABEL, "Write callerToken failed.");
75         return ERROR;
76     }
77     sptr<IRemoteObject> remote = Remote();
78     if (remote == nullptr) {
79         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
80         return ERROR;
81     }
82     int32_t error = remote->SendRequest(
83         static_cast<uint32_t>(IFormMgr::Message::FORM_MGR_REGISTER_REMOVE_OBSERVER), data, reply, option);
84     if (error != ERR_NONE) {
85         ACCESSTOKEN_LOG_ERROR(LABEL, "UnregisterAddObserver failed, error: %d", error);
86         return error;
87     }
88     return reply.ReadInt32();
89 }
90 
HasFormVisible(const uint32_t tokenId)91 bool FormManagerAccessProxy::HasFormVisible(const uint32_t tokenId)
92 {
93     MessageParcel data;
94     MessageParcel reply;
95     MessageOption option;
96     if (!data.WriteInterfaceToken(GetDescriptor())) {
97         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed.");
98         return false;
99     }
100     if (!data.WriteUint32(tokenId)) {
101         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenId.");
102         return false;
103     }
104 
105     sptr<IRemoteObject> remote = Remote();
106     if (remote == nullptr) {
107         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
108         return false;
109     }
110     int32_t error = remote->SendRequest(
111         static_cast<uint32_t>(IFormMgr::Message::FORM_MGR_HAS_FORM_VISIBLE_WITH_TOKENID), data, reply, option);
112     if (error != ERR_NONE) {
113         ACCESSTOKEN_LOG_ERROR(LABEL, "Get form visibility failed, error: %{public}d", error);
114         return false;
115     }
116     return reply.ReadBool();
117 }
118 } // namespace AccessToken
119 } // namespace Security
120 } // namespace OHOS
121