• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 KVSTORE_DATASERVICE_H
17 #define KVSTORE_DATASERVICE_H
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 
23 #include "account_delegate.h"
24 #include "constant.h"
25 #include "ikvstore_data_service.h"
26 #include "kvstore_device_listener.h"
27 #include "kvstore_meta_manager.h"
28 #include "metadata/corrupted_meta_data.h"
29 #include "metadata/store_meta_data.h"
30 #include "reporter.h"
31 #include "security/security.h"
32 #include "system_ability.h"
33 #include "types.h"
34 #include "dump_helper.h"
35 #include "feature_stub_impl.h"
36 
37 namespace OHOS::DistributedKv {
38 class KvStoreAccountObserver;
39 class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub {
40     DECLARE_SYSTEM_ABILITY(KvStoreDataService);
41 
42 public:
43     using CorruptedMetaData = DistributedData::CorruptedMetaData;
44     using StoreMetaData = DistributedData::StoreMetaData;
45     // record kvstore meta version for compatible, should update when modify kvstore meta structure.
46     static constexpr uint32_t STORE_VERSION = 0x03000001;
47 
48     explicit KvStoreDataService(bool runOnCreate = false);
49     explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false);
50     virtual ~KvStoreDataService();
51 
52     Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer) override;
53 
54     sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override;
55 
56     void OnDump() override;
57 
58     int Dump(int fd, const std::vector<std::u16string> &args) override;
59 
60     void OnStart() override;
61 
62     void OnStop() override;
63 
64     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
65 
66     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
67 
68     void AccountEventChanged(const AccountEventInfo &eventInfo);
69 
70     void SetCompatibleIdentify(const AppDistributedKv::DeviceInfo &info) const;
71 
72     void OnDeviceOnline(const AppDistributedKv::DeviceInfo &info);
73 
74     int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId);
75 
76 private:
77     void NotifyAccountEvent(const AccountEventInfo &eventInfo);
78     class KvStoreClientDeathObserverImpl {
79     public:
80         KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr<IRemoteObject> observer);
81 
82         virtual ~KvStoreClientDeathObserverImpl();
83 
84         pid_t GetPid() const;
85 
86     private:
87         class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient {
88         public:
89             explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl);
90             virtual ~KvStoreDeathRecipient();
91             void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
92 
93         private:
94             KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_;
95         };
96         void NotifyClientDie();
97         pid_t uid_;
98         pid_t pid_;
99         uint32_t token_;
100         AppId appId_;
101         KvStoreDataService &dataService_;
102         sptr<IRemoteObject> observerProxy_;
103         sptr<KvStoreDeathRecipient> deathRecipient_;
104     };
105 
106     void Initialize();
107 
108     void InitObjectStore();
109 
110     void StartService();
111 
112     void InitSecurityAdapter();
113 
114     void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag);
115 
116     Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId);
117 
118     bool ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam &param);
119     static void ResolveAutoLaunchCompatible(const MetaData &meta, const std::string &identifier);
120     static DistributedDB::SecurityOption ConvertSecurity(int securityLevel);
121     static Status InitNbDbOption(const Options &options, const std::vector<uint8_t> &cipherKey,
122                           DistributedDB::KvStoreNbDelegate::Option &dbOption);
123 
124     static constexpr int TEN_SEC = 10;
125 
126     std::mutex mutex_;
127     std::map<uint32_t, KvStoreClientDeathObserverImpl> clients_;
128     std::shared_ptr<KvStoreAccountObserver> accountEventObserver_;
129 
130     std::shared_ptr<Security> security_;
131     ConcurrentMap<std::string, sptr<DistributedData::FeatureStubImpl>> features_;
132     std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_;
133 };
134 
135 class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate {
136 public:
137     using Option = DistributedDB::KvStoreNbDelegate::Option;
~DbMetaCallbackDelegateMgr()138     virtual ~DbMetaCallbackDelegateMgr() {}
139 
DbMetaCallbackDelegateMgr(DistributedDB::KvStoreDelegateManager * delegate)140     explicit DbMetaCallbackDelegateMgr(DistributedDB::KvStoreDelegateManager *delegate)
141         : delegate_(delegate) {}
142     bool GetKvStoreDiskSize(const std::string &storeId, uint64_t &size) override;
143     void GetKvStoreKeys(std::vector<StoreInfo> &dbStats) override;
IsDestruct()144     bool IsDestruct()
145     {
146         return delegate_ == nullptr;
147     }
148 
149 private:
Split(const std::string & str,const std::string & delimiter,std::vector<std::string> & out)150     void Split(const std::string &str, const std::string &delimiter, std::vector<std::string> &out)
151     {
152         size_t start;
153         size_t end = 0;
154         while ((start = str.find_first_not_of(delimiter, end)) != std::string::npos) {
155             end = str.find(delimiter, start);
156             if (end == std::string::npos) {
157                 end = str.size();
158             }
159             out.push_back(str.substr(start, end - start));
160         }
161     }
162 
163     DistributedDB::KvStoreDelegateManager *delegate_ {};
164     static const inline int USER_ID = 0;
165     static const inline int APP_ID = 1;
166     static const inline int STORE_ID = 2;
167     static const inline int VECTOR_SIZE = 2;
168 };
169 }
170 #endif  // KVSTORE_DATASERVICE_H
171