• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef OHOS_TESTSAPROXYCACHEPROXY_H
17 #define OHOS_TESTSAPROXYCACHEPROXY_H
18 
19 #include "itest_sa_proxy_cache.h"
20 #include <iremote_proxy.h>
21 #include "api_cache_manager.h"
22 
23 namespace OHOS {
24 namespace {
25 constexpr int64_t EXPIRE_TIME_3000MS = 3000;
26 constexpr int64_t EXPIRE_TIME_4000MS = 4000;
27 constexpr int64_t EXPIRE_TIME_5000MS = 5000;
28 }
29 class TestSaProxyCacheProxy : public IRemoteProxy<ITestSaProxyCache> {
30 public:
TestSaProxyCacheProxy(const sptr<IRemoteObject> & remote)31     explicit TestSaProxyCacheProxy(
32         const sptr<IRemoteObject>& remote)
33         : IRemoteProxy<ITestSaProxyCache>(remote)
34     {
35         ApiCacheManager::GetInstance().AddCacheApi(GetDescriptor(), COMMAND_GET_STRING_FUNC, EXPIRE_TIME_3000MS);
36         ApiCacheManager::GetInstance().AddCacheApi(GetDescriptor(), COMMAND_GET_DOUBLE_FUNC, EXPIRE_TIME_4000MS);
37         ApiCacheManager::GetInstance().AddCacheApi(GetDescriptor(), COMMAND_GET_VECTOR_FUNC, EXPIRE_TIME_5000MS);
38 
39         if (remote) {
40             if (!remote->IsProxyObject()) {
41                 return;
42             }
43             deathRecipient_ = new (std::nothrow) TestSaProxyCacheRecipient(*this);
44             if (deathRecipient_ == nullptr) {
45                 return;
46             }
47             if (!remote->AddDeathRecipient(deathRecipient_)) {
48                 return;
49             }
50             remote_ = remote;
51         }
52     }
53 
~TestSaProxyCacheProxy()54     virtual ~TestSaProxyCacheProxy()
55     {
56         if (remote_ == nullptr) {
57             return;
58         }
59         if (deathRecipient_ == nullptr) {
60             return;
61         }
62         remote_->RemoveDeathRecipient(deathRecipient_);
63         remote_ = nullptr;
64 
65         ApiCacheManager::GetInstance().DelCacheApi(GetDescriptor(), COMMAND_GET_STRING_FUNC);
66         ApiCacheManager::GetInstance().DelCacheApi(GetDescriptor(), COMMAND_GET_DOUBLE_FUNC);
67         ApiCacheManager::GetInstance().DelCacheApi(GetDescriptor(), COMMAND_GET_VECTOR_FUNC);
68     }
69 
70     ErrCode GetStringFunc(
71         const std::string& in_str,
72         std::string& ret_str) override;
73 
74     ErrCode GetDoubleFunc(
75         int32_t number,
76         double& ret_number) override;
77 
78     ErrCode GetVectorFunc(
79         const std::vector<bool>& in_vec,
80         std::vector<int8_t>& ret_vec) override;
81 
82     uint32_t TestGetIpcSendRequestTimes() override;
83 
84     ErrCode GetSaPid(
85         int32_t& pid) override;
86 protected:
87     ErrCode ProxyReadVector(MessageParcel& reply, std::vector<int8_t>& ret_vec);
88 private:
89     class TestSaProxyCacheRecipient : public IRemoteObject::DeathRecipient {
90     public:
TestSaProxyCacheRecipient(TestSaProxyCacheProxy & client)91         explicit TestSaProxyCacheRecipient(TestSaProxyCacheProxy &client) : client_(client) {}
92         ~TestSaProxyCacheRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)93         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
94         {
95             client_.OnRemoteDied(remote);
96         }
97     private:
98         TestSaProxyCacheProxy &client_;
99     };
100 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)101     void OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
102     {
103         (void)remoteObject;
104         ApiCacheManager::GetInstance().ClearCache(GetDescriptor());
105     }
106     sptr<IRemoteObject> remote_ = nullptr;
107     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
108     static constexpr int32_t COMMAND_GET_STRING_FUNC = MIN_TRANSACTION_ID + 0;
109     static constexpr int32_t COMMAND_GET_DOUBLE_FUNC = MIN_TRANSACTION_ID + 1;
110     static constexpr int32_t COMMAND_GET_VECTOR_FUNC = MIN_TRANSACTION_ID + 2;
111     static constexpr int32_t COMMAND_GET_SA_PID = MIN_TRANSACTION_ID + 3;
112 
113     static inline BrokerDelegator<TestSaProxyCacheProxy> delegator_;
114     // only for test
115     uint32_t TestSendRequestTimes = 0;
116 };
117 } // namespace OHOS
118 #endif // OHOS_TESTSAPROXYCACHEPROXY_H
119 
120