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 I_KV_STORE_DATA_SERVICE_H 17 #define I_KV_STORE_DATA_SERVICE_H 18 19 #include "iremote_broker.h" 20 #include "ikvstore.h" 21 #include "ikvstore_client_death_observer.h" 22 #include "ikvstore_observer.h" 23 #include "ikvstore_single.h" 24 #include "iremote_proxy.h" 25 #include "iremote_stub.h" 26 #include "message_parcel.h" 27 #include "types.h" 28 #include "idevice_status_change_listener.h" 29 30 namespace OHOS::DistributedRdb { 31 class IRdbService; 32 } 33 34 namespace OHOS::DistributedKv { 35 /* 36 * IPC-friendly Options struct without std::string schema field. 37 * Passing a struct with an std::string field is a potential security exploit. 38 * 39 */ 40 struct OptionsIpc { 41 bool createIfMissing; 42 bool encrypt; 43 bool persistant; 44 bool backup; 45 bool autoSync; 46 int securityLevel; 47 SyncPolicy syncPolicy; 48 KvStoreType kvStoreType; 49 bool syncable; // let bms delete first 50 bool dataOwnership; // true indicates the ownership of distributed data is DEVICE, otherwise, ACCOUNT 51 }; 52 53 class IKvStoreDataService : public IRemoteBroker { 54 public: 55 enum { 56 GETKVSTORE, 57 GETALLKVSTOREID, 58 CLOSEKVSTORE, 59 CLOSEALLKVSTORE, 60 DELETEKVSTORE, 61 DELETEALLKVSTORE, 62 REGISTERCLIENTDEATHOBSERVER, 63 GETSINGLEKVSTORE, 64 GETLOCALDEVICE, 65 GETDEVICELIST, 66 STARTWATCHDEVICECHANGE, 67 STOPWATCHDEVICECHANGE, 68 GET_RDB_SERVICE, 69 SERVICE_CMD_LAST, 70 DATAUSAGESTART = 20, 71 DATAUSAGEEND = 40, 72 }; 73 74 DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreDataService"); 75 /* create and open kv store instance. */ 76 virtual Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 77 std::function<void(sptr<IKvStoreImpl>)> callback) = 0; 78 79 virtual Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 80 std::function<void(sptr<ISingleKvStore>)> callback) = 0; 81 82 /* get all kv store names */ 83 virtual void GetAllKvStoreId(const AppId &appId, std::function<void(Status, std::vector<StoreId> &)> callback) = 0; 84 85 /* open kv store instance will not receive subscribe any more. */ 86 virtual Status CloseKvStore(const AppId &appId, const StoreId &id) = 0; 87 88 /* close all kvstore. */ 89 virtual Status CloseAllKvStore(const AppId &appId) = 0; 90 91 /* delete kv store */ 92 virtual Status DeleteKvStore(const AppId &appId, const StoreId &id) = 0; 93 94 /* delete kv store */ 95 virtual Status DeleteAllKvStore(const AppId &appId) = 0; 96 97 virtual Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer) = 0; 98 99 virtual Status GetLocalDevice(DeviceInfo &device) = 0; 100 virtual Status GetDeviceList(std::vector<DeviceInfo> &deviceInfoList, DeviceFilterStrategy strategy) = 0; 101 virtual Status StartWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer, 102 DeviceFilterStrategy strategy) = 0; 103 virtual Status StopWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer) = 0; 104 virtual sptr<DistributedRdb::IRdbService> GetRdbService() = 0; 105 }; 106 107 class KvStoreDataServiceStub : public IRemoteStub<IKvStoreDataService> { 108 public: 109 virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, 110 MessageParcel &reply, MessageOption &option) override; 111 private: 112 int32_t GetKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); 113 int32_t GetAllKvStoreIdOnRemote(MessageParcel &data, MessageParcel &reply); 114 int32_t CloseKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); 115 int32_t CloseAllKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); 116 int32_t DeleteKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); 117 int32_t DeleteAllKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); 118 int32_t RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply); 119 int32_t GetLocalDeviceOnRemote(MessageParcel &data, MessageParcel &reply); 120 int32_t GetDeviceListOnRemote(MessageParcel &data, MessageParcel &reply); 121 int32_t StartWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply); 122 int32_t StopWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply); 123 int32_t GetSingleKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); 124 int32_t GetRdbServiceOnRemote(MessageParcel& data, MessageParcel& reply); 125 126 using RequestHandler = int32_t(KvStoreDataServiceStub::*)(MessageParcel&, MessageParcel&); 127 static constexpr RequestHandler HANDLERS[SERVICE_CMD_LAST] = { 128 [GETKVSTORE] = &KvStoreDataServiceStub::GetKvStoreOnRemote, 129 [GETALLKVSTOREID] = &KvStoreDataServiceStub::GetAllKvStoreIdOnRemote, 130 [CLOSEKVSTORE] = &KvStoreDataServiceStub::CloseKvStoreOnRemote, 131 [CLOSEALLKVSTORE] = &KvStoreDataServiceStub::CloseAllKvStoreOnRemote, 132 [DELETEKVSTORE] = &KvStoreDataServiceStub::DeleteKvStoreOnRemote, 133 [DELETEALLKVSTORE] = &KvStoreDataServiceStub::DeleteAllKvStoreOnRemote, 134 [REGISTERCLIENTDEATHOBSERVER] = &KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote, 135 [GETSINGLEKVSTORE] = &KvStoreDataServiceStub::GetSingleKvStoreOnRemote, 136 [GETLOCALDEVICE] = &KvStoreDataServiceStub::GetLocalDeviceOnRemote, 137 [GETDEVICELIST] = &KvStoreDataServiceStub::GetDeviceListOnRemote, 138 [STARTWATCHDEVICECHANGE] = &KvStoreDataServiceStub::StartWatchDeviceChangeOnRemote, 139 [STOPWATCHDEVICECHANGE] = &KvStoreDataServiceStub::StopWatchDeviceChangeOnRemote, 140 [GET_RDB_SERVICE] = &KvStoreDataServiceStub::GetRdbServiceOnRemote, 141 }; 142 }; 143 144 class KvStoreDataServiceProxy : public IRemoteProxy<IKvStoreDataService> { 145 public: 146 explicit KvStoreDataServiceProxy(const sptr<IRemoteObject> &impl); 147 ~KvStoreDataServiceProxy() = default; 148 149 /* create and open kv store instance. */ 150 virtual Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 151 std::function<void(sptr<IKvStoreImpl>)> callback); 152 153 virtual Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 154 std::function<void(sptr<ISingleKvStore>)> callback); 155 156 /* get all kv store names */ 157 virtual void GetAllKvStoreId(const AppId &appId, std::function<void(Status, std::vector<StoreId> &)> callback); 158 159 /* open kv store instance will not receive subscribe any more. */ 160 virtual Status CloseKvStore(const AppId &appId, const StoreId &storeId); 161 162 /* close all kvstore. */ 163 virtual Status CloseAllKvStore(const AppId &appId); 164 165 /* delete kv store */ 166 virtual Status DeleteKvStore(const AppId &appId, const StoreId &id); 167 168 /* delete kv store */ 169 virtual Status DeleteAllKvStore(const AppId &appId); 170 171 virtual Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer); 172 173 virtual Status GetLocalDevice(DeviceInfo &device); 174 virtual Status GetDeviceList(std::vector<DeviceInfo> &deviceInfoList, DeviceFilterStrategy strategy); 175 virtual Status StartWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer, DeviceFilterStrategy strategy); 176 virtual Status StopWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer); 177 virtual sptr<DistributedRdb::IRdbService> GetRdbService(); 178 private: 179 static inline BrokerDelegator<KvStoreDataServiceProxy> delegator_; 180 }; 181 } 182 183 #endif // I_KV_STORE_DATA_SERVICE_H 184