• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 DYNAMIC_CACHE_H
17 #define DYNAMIC_CACHE_H
18 
19 #include <map>
20 #include <string>
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 using namespace std;
25 class DynamicCache : public IRemoteObject::DeathRecipient {
26 public:
27 
OnRemoteDied(const wptr<IRemoteObject> & remote)28     void OnRemoteDied(const wptr<IRemoteObject>& remote) override
29     {
30         ClearCache();
31     }
32 
33     sptr<IRemoteObject> QueryResult(int32_t querySaId, int32_t code);
34     bool CanUseCache(int32_t querySaId, char* waterLine, std::string defaultValue);
35 
ClearCache()36     void __attribute__((no_sanitize("cfi"))) ClearCache()
37     {
38         std::lock_guard<std::mutex> autoLock(queryCacheLock_);
39         if (lastQuerySaProxy_ != nullptr) {
40             lastQuerySaProxy_->RemoveDeathRecipient(this);
41         }
42         lastQuerySaId_ = -1;
43         lastQuerySaProxy_ = nullptr;
44     }
45 
46     bool InvalidateCache();
47     bool SetKey(const std::string& key);
Recompute(int32_t querySaId,int32_t code)48     virtual sptr<IRemoteObject> Recompute(int32_t querySaId, int32_t code)
49     {
50         std::lock_guard<std::mutex> autoLock(queryCacheLock_);
51         if (lastQuerySaId_ != querySaId) {
52             return nullptr;
53         }
54         return lastQuerySaProxy_;
55     }
56 
57 private:
58     std::mutex queryCacheLock_;
59     std::map<std::string, std::string> localPara_;
60     std::string key_;
61     int32_t lastQuerySaId_;
62     sptr<IRemoteObject> lastQuerySaProxy_;
63 };
64 } // namespace OHOS
65 #endif /* DYNAMIC_CACHE_H */