• 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 "dump_helper.h"
25 #include "feature_stub_impl.h"
26 #include "ikvstore_data_service.h"
27 #include "ithread_pool.h"
28 #include "kvstore_device_listener.h"
29 #include "kvstore_meta_manager.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 
43 public:
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     void OnDeviceOffline(const AppDistributedKv::DeviceInfo &info);
75 
76     void OnDeviceOnReady(const AppDistributedKv::DeviceInfo &info);
77 
78     int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index);
79 
80     int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index);
81 
82 private:
83     void NotifyAccountEvent(const AccountEventInfo &eventInfo);
84     class KvStoreClientDeathObserverImpl {
85     public:
86         KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr<IRemoteObject> observer);
87 
88         virtual ~KvStoreClientDeathObserverImpl();
89 
90         pid_t GetPid() const;
91 
92     private:
93         class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient {
94         public:
95             explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl);
96             virtual ~KvStoreDeathRecipient();
97             void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
98 
99         private:
100             KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_;
101         };
102         void NotifyClientDie();
103         pid_t uid_;
104         pid_t pid_;
105         uint32_t token_;
106         AppId appId_;
107         KvStoreDataService &dataService_;
108         sptr<IRemoteObject> observerProxy_;
109         sptr<KvStoreDeathRecipient> deathRecipient_;
110     };
111 
112     void Initialize();
113 
114     void LoadFeatures();
115 
116     void StartService();
117 
118     void InitSecurityAdapter(std::shared_ptr<ExecutorPool> executors);
119 
120     void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag);
121 
122     Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId);
123 
124     bool ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam &param);
125     void ResolveAutoLaunchCompatible(const StoreMetaData &storeMeta, const std::string &identifier);
126     static DistributedDB::SecurityOption ConvertSecurity(int securityLevel);
127     static Status InitNbDbOption(const Options &options, const std::vector<uint8_t> &cipherKey,
128                           DistributedDB::KvStoreNbDelegate::Option &dbOption);
129 
130     static constexpr int TEN_SEC = 10;
131 
132     std::mutex mutex_;
133     std::map<uint32_t, KvStoreClientDeathObserverImpl> clients_;
134     std::shared_ptr<KvStoreAccountObserver> accountEventObserver_;
135 
136     std::shared_ptr<Security> security_;
137     ConcurrentMap<std::string, sptr<DistributedData::FeatureStubImpl>> features_;
138     std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_;
139     std::shared_ptr<ExecutorPool> executors_;
140 };
141 }
142 #endif  // KVSTORE_DATASERVICE_H
143