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 "accessibility_window_connection.h"
17 
18 using namespace std;
19 
20 namespace OHOS {
21 namespace Accessibility {
AccessibilityWindowConnection(const int32_t windowId,const sptr<IAccessibilityElementOperator> & connection,const int32_t accountId)22 AccessibilityWindowConnection::AccessibilityWindowConnection(const int32_t windowId,
23     const sptr<IAccessibilityElementOperator> &connection, const int32_t accountId)
24 {
25     windowId_ = windowId;
26     proxy_ = connection;
27     accountId_ = accountId;
28     cardProxy_[0] = connection;
29 }
30 
AccessibilityWindowConnection(const int32_t windowId,const int32_t treeId,const sptr<IAccessibilityElementOperator> & connection,const int32_t accountId)31 AccessibilityWindowConnection::AccessibilityWindowConnection(const int32_t windowId, const int32_t treeId,
32     const sptr<IAccessibilityElementOperator> &connection, const int32_t accountId)
33 {
34     windowId_ = windowId;
35     treeId_ = treeId;
36     cardProxy_[treeId] = connection;
37     accountId_ = accountId;
38 }
39 
~AccessibilityWindowConnection()40 AccessibilityWindowConnection::~AccessibilityWindowConnection()
41 {
42 }
43 
SetCardProxy(const int32_t treeId,sptr<IAccessibilityElementOperator> operation)44 RetError AccessibilityWindowConnection::SetCardProxy(const int32_t treeId,
45     sptr<IAccessibilityElementOperator> operation)
46 {
47     if (!operation) {
48         HILOG_DEBUG("SetCardProxy : operation is nullptr");
49         return RET_ERR_FAILED;
50     }
51     cardProxy_[treeId] = operation;
52     return RET_OK;
53 }
54 
GetCardProxy(const int32_t treeId)55 sptr<IAccessibilityElementOperator> AccessibilityWindowConnection::GetCardProxy(const int32_t treeId)
56 {
57     auto iter = cardProxy_.find(treeId);
58     if (iter != cardProxy_.end()) {
59         HILOG_DEBUG("GetCardProxy : operation is ok");
60         return cardProxy_[treeId];
61     }
62     HILOG_DEBUG("GetCardProxy : operation is no");
63     return nullptr;
64 }
65 
SetTokenIdMap(const int32_t treeId,const uint32_t tokenId)66 RetError AccessibilityWindowConnection::SetTokenIdMap(const int32_t treeId,
67     const uint32_t tokenId)
68 {
69     HILOG_DEBUG("treeId : %{public}d", treeId);
70     tokenIdMap_[treeId] = tokenId;
71     return RET_OK;
72 }
73 
GetTokenIdMap(const int32_t treeId)74 uint32_t AccessibilityWindowConnection::GetTokenIdMap(const int32_t treeId)
75 {
76     HILOG_DEBUG("treeId : %{public}d", treeId);
77     return tokenIdMap_[treeId];
78 }
79 
GetAllTreeId(std::vector<int32_t> & treeIds)80 void AccessibilityWindowConnection::GetAllTreeId(std::vector<int32_t> &treeIds)
81 {
82     for (auto &treeId: cardProxy_) {
83         treeIds.emplace_back(treeId.first);
84     }
85 }
86 
GetRootParentId(int32_t treeId,int64_t & elementId)87 RetError AccessibilityWindowConnection::GetRootParentId(int32_t treeId, int64_t &elementId)
88 {
89     auto iter = treeIdParentId_.find(treeId);
90     if (iter != treeIdParentId_.end()) {
91         elementId = iter->second;
92         return RET_OK;
93     }
94     return RET_ERR_FAILED;
95 }
96 
SetRootParentId(const int32_t treeId,const int64_t elementId)97 RetError AccessibilityWindowConnection::SetRootParentId(const int32_t treeId, const int64_t elementId)
98 {
99     treeIdParentId_[treeId] = elementId;
100     return RET_OK;
101 }
102 
EraseProxy(const int32_t treeId)103 void AccessibilityWindowConnection::EraseProxy(const int32_t treeId)
104 {
105     cardProxy_.erase(treeId);
106 }
107 } // namespace Accessibility
108 } // namespace OHOS
109