• 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 "feature_stub_impl.h"
25 #include "ikvstore_data_service.h"
26 #include "ithread_pool.h"
27 #include "kvstore_device_listener.h"
28 #include "kvstore_meta_manager.h"
29 #include "kvstore_data_service_stub.h"
30 #include "metadata/store_meta_data.h"
31 #include "reporter.h"
32 #include "runtime_config.h"
33 #include "security/security.h"
34 #include "system_ability.h"
35 #include "executor_pool.h"
36 #include "types.h"
37 
38 namespace OHOS::DistributedKv {
39 class KvStoreAccountObserver;
40 class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub {
41     DECLARE_SYSTEM_ABILITY(KvStoreDataService);
42     using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>;
43 
44 public:
45     struct UserInfo {
46         std::string userId;
47         std::set<std::string> bundles;
48     };
49     struct BundleInfo {
50         std::string bundleName;
51         std::string appId;
52         std::string type;
53         int32_t uid;
54         uint32_t tokenId;
55         std::string userId;
56         std::set<std::string> storeIDs;
57     };
58     using StoreMetaData = DistributedData::StoreMetaData;
59     // record kvstore meta version for compatible, should update when modify kvstore meta structure.
60     static constexpr uint32_t STORE_VERSION = 0x03000001;
61 
62     explicit KvStoreDataService(bool runOnCreate = false);
63     explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false);
64     virtual ~KvStoreDataService();
65 
66     void RegisterHandler(const std::string &name, Handler &handler);
67     void RegisterStoreInfo();
68     bool IsExist(const std::string &infoName, std::map<std::string, std::vector<std::string>> &filterInfo,
69         std::string &metaParam);
70     void DumpStoreInfo(int fd, std::map<std::string, std::vector<std::string>> &params);
71     void FilterData(std::vector<StoreMetaData> &metas, std::map<std::string, std::vector<std::string>> &filterInfo);
72     void PrintfInfo(int fd, const std::vector<StoreMetaData> &metas);
73     std::string GetIndentation(int size);
74 
75     void RegisterUserInfo();
76     void BuildData(std::map<std::string, UserInfo> &datas, const std::vector<StoreMetaData> &metas);
77     void PrintfInfo(int fd, const std::map<std::string, UserInfo> &datas);
78     void DumpUserInfo(int fd, std::map<std::string, std::vector<std::string>> &params);
79 
80     void RegisterBundleInfo();
81     void BuildData(std::map<std::string, BundleInfo> &datas, const std::vector<StoreMetaData> &metas);
82     void PrintfInfo(int fd, const std::map<std::string, BundleInfo> &datas);
83     void DumpBundleInfo(int fd, std::map<std::string, std::vector<std::string>> &params);
84 
85     Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer) override;
86 
87     sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override;
88 
89     int32_t ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex, int32_t tokenId) override;
90 
91     void OnDump() override;
92 
93     int Dump(int fd, const std::vector<std::u16string> &args) override;
94 
95     void OnStart() override;
96 
97     void OnStop() override;
98 
99     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
100 
101     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
102 
103     void AccountEventChanged(const AccountEventInfo &eventInfo);
104 
105     void SetCompatibleIdentify(const AppDistributedKv::DeviceInfo &info) const;
106 
107     void OnDeviceOnline(const AppDistributedKv::DeviceInfo &info);
108 
109     void OnDeviceOffline(const AppDistributedKv::DeviceInfo &info);
110 
111     void OnDeviceOnReady(const AppDistributedKv::DeviceInfo &info);
112 
113     int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index);
114 
115     int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index);
116 
117     int32_t OnInstall(const std::string &bundleName, int32_t user, int32_t index);
118 
119 private:
120     void NotifyAccountEvent(const AccountEventInfo &eventInfo);
121     class KvStoreClientDeathObserverImpl {
122     public:
123         KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr<IRemoteObject> observer);
124 
125         virtual ~KvStoreClientDeathObserverImpl();
126 
127         pid_t GetPid() const;
128 
129     private:
130         class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient {
131         public:
132             explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl);
133             virtual ~KvStoreDeathRecipient();
134             void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
135 
136         private:
137             KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_;
138         };
139         void NotifyClientDie();
140         pid_t uid_;
141         pid_t pid_;
142         uint32_t token_;
143         AppId appId_;
144         KvStoreDataService &dataService_;
145         sptr<IRemoteObject> observerProxy_;
146         sptr<KvStoreDeathRecipient> deathRecipient_;
147     };
148 
149     void Initialize();
150 
151     void LoadFeatures();
152 
153     void StartService();
154 
155     void InitSecurityAdapter(std::shared_ptr<ExecutorPool> executors);
156 
157     void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag);
158 
159     Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId);
160 
161     bool ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam &param);
162     void ResolveAutoLaunchCompatible(const StoreMetaData &storeMeta, const std::string &identifier);
163     static DistributedDB::SecurityOption ConvertSecurity(int securityLevel);
164     static Status InitNbDbOption(const Options &options, const std::vector<uint8_t> &cipherKey,
165                           DistributedDB::KvStoreNbDelegate::Option &dbOption);
166 
167     static constexpr int TEN_SEC = 10;
168 
169     std::mutex mutex_;
170     std::map<uint32_t, KvStoreClientDeathObserverImpl> clients_;
171     std::shared_ptr<KvStoreAccountObserver> accountEventObserver_;
172 
173     std::shared_ptr<Security> security_;
174     ConcurrentMap<std::string, sptr<DistributedData::FeatureStubImpl>> features_;
175     std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_;
176     std::shared_ptr<ExecutorPool> executors_;
177     static constexpr int VERSION_WIDTH = 11;
178     static constexpr const char *INDENTATION = "    ";
179     static constexpr int32_t FORMAT_BLANK_SIZE = 32;
180     static constexpr char FORMAT_BLANK_SPACE = ' ';
181     static constexpr int32_t PRINTF_COUNT_2 = 2;
182     static constexpr int MAXIMUM_PARAMETER_LIMIT = 3;
183 };
184 }
185 #endif  // KVSTORE_DATASERVICE_H
186