1 /*
2 * Copyright (c) 2022 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 #include "flat_object_store.h"
17
18 #include "accesstoken_kit.h"
19 #include "block_data.h"
20 #include "bytes_utils.h"
21 #include "client_adaptor.h"
22 #include "ipc_skeleton.h"
23 #include "object_callback_impl.h"
24 #include "object_radar_reporter.h"
25 #include "string_utils.h"
26
27 namespace OHOS::ObjectStore {
FlatObjectStore(const std::string & bundleName)28 FlatObjectStore::FlatObjectStore(const std::string &bundleName)
29 {
30 bundleName_ = bundleName;
31 storageEngine_ = std::make_shared<FlatObjectStorageEngine>();
32 uint32_t status = storageEngine_->Open(bundleName);
33 if (status != SUCCESS) {
34 LOG_ERROR("FlatObjectStore: Failed to open, error: open storage engine failure %{public}d", status);
35 }
36 cacheManager_ = new CacheManager();
37 }
38
~FlatObjectStore()39 FlatObjectStore::~FlatObjectStore()
40 {
41 if (storageEngine_ != nullptr) {
42 storageEngine_->Close();
43 storageEngine_ = nullptr;
44 }
45 delete cacheManager_;
46 cacheManager_ = nullptr;
47 }
48
CreateObject(const std::string & sessionId)49 uint32_t FlatObjectStore::CreateObject(const std::string &sessionId)
50 {
51 if (!cacheManager_->IsContinue()) { // NOT IN CONTINUE, CHECK PERMISSION
52 auto tokenId = IPCSkeleton::GetSelfTokenID();
53 int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, DISTRIBUTED_DATASYNC);
54 if (ret != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
55 return ERR_NO_PERMISSION;
56 }
57 }
58 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
59 LOG_ERROR("FlatObjectStore::DB has not inited");
60 return ERR_DB_NOT_INIT;
61 }
62 uint32_t status = storageEngine_->CreateTable(sessionId);
63 if (status != SUCCESS) {
64 LOG_ERROR("FlatObjectStore::CreateObject createTable err %{public}d", status);
65 return status;
66 }
67 SubscribeDataChange(sessionId);
68 ResumeObject(sessionId);
69 return SUCCESS;
70 }
71
ResumeObject(const std::string & sessionId)72 void FlatObjectStore::ResumeObject(const std::string &sessionId)
73 {
74 std::function<void(const std::map<std::string, std::vector<uint8_t>> &data, bool allReady)> callback =
75 [sessionId, this](
76 const std::map<std::string, std::vector<uint8_t>> &data, bool allReady) {
77 if (data.size() == 0) {
78 LOG_INFO("retrieve empty");
79 return;
80 }
81 LOG_INFO("retrieve success, data.size:%{public}zu, allReady:%{public}d", data.size(), allReady);
82 auto result = storageEngine_->UpdateItems(sessionId, data);
83 if (result != SUCCESS) {
84 LOG_ERROR("UpdateItems failed, status = %{public}d", result);
85 }
86 if (allReady) {
87 std::lock_guard<std::mutex> lck(mutex_);
88 if (find(retrievedCache_.begin(), retrievedCache_.end(), sessionId) == retrievedCache_.end()) {
89 retrievedCache_.push_back(sessionId);
90 storageEngine_->NotifyStatus(sessionId, "local", "restored");
91 }
92 }
93 };
94 cacheManager_->ResumeObject(bundleName_, sessionId, callback);
95 }
96
SubscribeDataChange(const std::string & sessionId)97 void FlatObjectStore::SubscribeDataChange(const std::string &sessionId)
98 {
99 std::function<void(const std::map<std::string, std::vector<uint8_t>> &data, bool allReady)> remoteResumeCallback =
100 [sessionId, this](const std::map<std::string, std::vector<uint8_t>> &data, bool allReady) {
101 LOG_INFO("DataChange callback. data.size:%{public}zu, allReady:%{public}d", data.size(), allReady);
102 std::map<std::string, std::vector<uint8_t>> filteredData = data;
103 FilterData(sessionId, filteredData);
104 if (!filteredData.empty()) {
105 auto status = storageEngine_->UpdateItems(sessionId, filteredData);
106 if (status != SUCCESS) {
107 LOG_ERROR("UpdateItems failed, status = %{public}d", status);
108 }
109 storageEngine_->NotifyChange(sessionId, filteredData);
110 }
111 if (allReady) {
112 std::lock_guard<std::mutex> lck(mutex_);
113 if (find(retrievedCache_.begin(), retrievedCache_.end(), sessionId) == retrievedCache_.end()) {
114 retrievedCache_.push_back(sessionId);
115 storageEngine_->NotifyStatus(sessionId, "local", "restored");
116 }
117 }
118 };
119 cacheManager_->SubscribeDataChange(bundleName_, sessionId, remoteResumeCallback);
120 }
121
Delete(const std::string & sessionId)122 uint32_t FlatObjectStore::Delete(const std::string &sessionId)
123 {
124 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
125 LOG_ERROR("FlatObjectStore::DB has not inited");
126 return ERR_DB_NOT_INIT;
127 }
128 uint32_t status = storageEngine_->DeleteTable(sessionId);
129 if (status != SUCCESS) {
130 LOG_ERROR("FlatObjectStore: Failed to delete object %{public}d", status);
131 return status;
132 }
133 cacheManager_->UnregisterDataChange(bundleName_, sessionId);
134 cacheManager_->DeleteSnapshot(bundleName_, sessionId);
135 return SUCCESS;
136 }
137
Watch(const std::string & sessionId,std::shared_ptr<FlatObjectWatcher> watcher)138 uint32_t FlatObjectStore::Watch(const std::string &sessionId, std::shared_ptr<FlatObjectWatcher> watcher)
139 {
140 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
141 LOG_ERROR("FlatObjectStore::DB has not inited");
142 return ERR_DB_NOT_INIT;
143 }
144 uint32_t status = storageEngine_->RegisterObserver(sessionId, watcher);
145 if (status != SUCCESS) {
146 LOG_ERROR("FlatObjectStore::Watch failed %{public}d", status);
147 }
148 return status;
149 }
150
UnWatch(const std::string & sessionId)151 uint32_t FlatObjectStore::UnWatch(const std::string &sessionId)
152 {
153 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
154 LOG_ERROR("FlatObjectStore::DB has not inited");
155 return ERR_DB_NOT_INIT;
156 }
157 uint32_t status = storageEngine_->UnRegisterObserver(sessionId);
158 if (status != SUCCESS) {
159 LOG_ERROR("FlatObjectStore::Watch failed %{public}d", status);
160 }
161 return status;
162 }
163
Put(const std::string & sessionId,const std::string & key,std::vector<uint8_t> value)164 uint32_t FlatObjectStore::Put(const std::string &sessionId, const std::string &key, std::vector<uint8_t> value)
165 {
166 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
167 LOG_ERROR("FlatObjectStore::DB has not inited");
168 return ERR_DB_NOT_INIT;
169 }
170 return storageEngine_->UpdateItem(sessionId, key, value);
171 }
172
Get(const std::string & sessionId,const std::string & key,Bytes & value)173 uint32_t FlatObjectStore::Get(const std::string &sessionId, const std::string &key, Bytes &value)
174 {
175 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
176 LOG_ERROR("FlatObjectStore::DB has not inited");
177 return ERR_DB_NOT_INIT;
178 }
179 return storageEngine_->GetItem(sessionId, key, value);
180 }
181
SetStatusNotifier(std::shared_ptr<StatusWatcher> notifier)182 uint32_t FlatObjectStore::SetStatusNotifier(std::shared_ptr<StatusWatcher> notifier)
183 {
184 if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) {
185 LOG_ERROR("FlatObjectStore::DB has not inited");
186 return ERR_DB_NOT_INIT;
187 }
188 return storageEngine_->SetStatusNotifier(notifier);
189 }
190
Save(const std::string & sessionId,const std::string & deviceId)191 uint32_t FlatObjectStore::Save(const std::string &sessionId, const std::string &deviceId)
192 {
193 RadarReporter::ReportStateStart(std::string(__FUNCTION__), SAVE, SAVE_TO_SERVICE, IDLE, START, bundleName_);
194 if (cacheManager_ == nullptr) {
195 LOG_ERROR("FlatObjectStore::cacheManager_ is null");
196 return ERR_NULL_PTR;
197 }
198 std::map<std::string, std::vector<uint8_t>> objectData;
199 uint32_t status = storageEngine_->GetItems(sessionId, objectData);
200 if (status != SUCCESS) {
201 LOG_ERROR("FlatObjectStore::GetItems fail");
202 RadarReporter::ReportStateError(std::string(__FUNCTION__), SAVE, SAVE_TO_SERVICE,
203 RADAR_FAILED, status, FINISHED);
204 return status;
205 }
206 return cacheManager_->Save(bundleName_, sessionId, deviceId, objectData);
207 }
208
RevokeSave(const std::string & sessionId)209 uint32_t FlatObjectStore::RevokeSave(const std::string &sessionId)
210 {
211 if (cacheManager_ == nullptr) {
212 LOG_ERROR("FlatObjectStore::cacheManager_ is null");
213 return ERR_NULL_PTR;
214 }
215 return cacheManager_->RevokeSave(bundleName_, sessionId);
216 }
217
BindAssetStore(const std::string & sessionId,AssetBindInfo & bindInfo,Asset & assetValue)218 uint32_t FlatObjectStore::BindAssetStore(const std::string &sessionId, AssetBindInfo &bindInfo, Asset &assetValue)
219 {
220 std::unique_lock<std::mutex> lck(mutex_);
221 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
222 if (proxy == nullptr) {
223 LOG_ERROR("proxy is nullptr.");
224 return ERR_PROCESSING;
225 }
226 int32_t status = proxy->BindAssetStore(bundleName_, sessionId, assetValue, bindInfo);
227 if (status != SUCCESS) {
228 LOG_ERROR("object bind asset failed code=%{public}d.", static_cast<int>(status));
229 }
230 LOG_DEBUG("object bind asset successful sessionId: %{public}s and assetName %{public}s", sessionId.c_str(),
231 assetValue.name.c_str());
232 return status;
233 }
234
CheckRetrieveCache(const std::string & sessionId)235 void FlatObjectStore::CheckRetrieveCache(const std::string &sessionId)
236 {
237 std::lock_guard<std::mutex> lck(mutex_);
238 auto iter = find(retrievedCache_.begin(), retrievedCache_.end(), sessionId);
239 if (iter != retrievedCache_.end()) {
240 storageEngine_->NotifyStatus(*iter, "local", "restored");
241 retrievedCache_.erase(iter);
242 }
243 }
244
FilterData(const std::string & sessionId,std::map<std::string,std::vector<uint8_t>> & data)245 void FlatObjectStore::FilterData(const std::string &sessionId,
246 std::map<std::string, std::vector<uint8_t>> &data)
247 {
248 std::map<std::string, std::vector<uint8_t>> allData {};
249 storageEngine_->GetItems(sessionId, allData);
250 for (const auto &item : allData) {
251 data.erase(item.first);
252 }
253 }
254
PutDouble(const std::string & sessionId,const std::string & key,double value)255 uint32_t FlatObjectStore::PutDouble(const std::string &sessionId, const std::string &key, double value)
256 {
257 Bytes data;
258 Type type = Type::TYPE_DOUBLE;
259 BytesUtils::PutNum(&type, 0, sizeof(type), data);
260 BytesUtils::PutNum(&value, sizeof(type), sizeof(value), data);
261 return Put(sessionId, FIELDS_PREFIX + key, data);
262 }
263
PutBoolean(const std::string & sessionId,const std::string & key,bool value)264 uint32_t FlatObjectStore::PutBoolean(const std::string &sessionId, const std::string &key, bool value)
265 {
266 Bytes data;
267 Type type = Type::TYPE_BOOLEAN;
268 BytesUtils::PutNum(&type, 0, sizeof(type), data);
269 BytesUtils::PutNum(&value, sizeof(type), sizeof(value), data);
270 return Put(sessionId, FIELDS_PREFIX + key, data);
271 }
272
PutString(const std::string & sessionId,const std::string & key,const std::string & value)273 uint32_t FlatObjectStore::PutString(const std::string &sessionId, const std::string &key, const std::string &value)
274 {
275 Bytes data;
276 Type type = Type::TYPE_STRING;
277 BytesUtils::PutNum(&type, 0, sizeof(type), data);
278 Bytes dst = StringUtils::StrToBytes(value);
279 data.insert(data.end(), dst.begin(), dst.end());
280 return Put(sessionId, FIELDS_PREFIX + key, data);
281 }
282
GetDouble(const std::string & sessionId,const std::string & key,double & value)283 uint32_t FlatObjectStore::GetDouble(const std::string &sessionId, const std::string &key, double &value)
284 {
285 Bytes data;
286 Bytes keyBytes = StringUtils::StrToBytes(key);
287 uint32_t status = Get(sessionId, FIELDS_PREFIX + key, data);
288 if (status != SUCCESS) {
289 LOG_ERROR("GetDouble field not exist. %{public}d %{public}s", status, key.c_str());
290 return status;
291 }
292 status = BytesUtils::GetNum(data, sizeof(Type), &value, sizeof(value));
293 if (status != SUCCESS) {
294 LOG_ERROR("GetDouble getNum err. %{public}d", status);
295 }
296 return status;
297 }
298
GetBoolean(const std::string & sessionId,const std::string & key,bool & value)299 uint32_t FlatObjectStore::GetBoolean(const std::string &sessionId, const std::string &key, bool &value)
300 {
301 Bytes data;
302 Bytes keyBytes = StringUtils::StrToBytes(key);
303 uint32_t status = Get(sessionId, FIELDS_PREFIX + key, data);
304 if (status != SUCCESS) {
305 LOG_ERROR("GetBoolean field not exist. %{public}d %{public}s", status, key.c_str());
306 return status;
307 }
308 status = BytesUtils::GetNum(data, sizeof(Type), &value, sizeof(value));
309 if (status != SUCCESS) {
310 LOG_ERROR("GetBoolean getNum err. %{public}d", status);
311 return status;
312 }
313 return SUCCESS;
314 }
315
GetString(const std::string & sessionId,const std::string & key,std::string & value)316 uint32_t FlatObjectStore::GetString(const std::string &sessionId, const std::string &key, std::string &value)
317 {
318 Bytes data;
319 uint32_t status = Get(sessionId, FIELDS_PREFIX + key, data);
320 if (status != SUCCESS) {
321 LOG_ERROR("GetString field not exist. %{public}d %{public}s", status, key.c_str());
322 return status;
323 }
324 status = StringUtils::BytesToStrWithType(data, value);
325 if (status != SUCCESS) {
326 LOG_ERROR("GetString dataToVal err. %{public}d", status);
327 }
328 return status;
329 }
330
PutComplex(const std::string & sessionId,const std::string & key,const std::vector<uint8_t> & value)331 uint32_t FlatObjectStore::PutComplex(const std::string &sessionId, const std::string &key,
332 const std::vector<uint8_t> &value)
333 {
334 Bytes data;
335 Type type = Type::TYPE_COMPLEX;
336 BytesUtils::PutNum(&type, 0, sizeof(type), data);
337 data.insert(data.end(), value.begin(), value.end());
338 uint32_t status = Put(sessionId, FIELDS_PREFIX + key, data);
339 if (status != SUCCESS) {
340 LOG_ERROR("PutBoolean setField err %{public}d", status);
341 }
342 return status;
343 }
344
GetComplex(const std::string & sessionId,const std::string & key,std::vector<uint8_t> & value)345 uint32_t FlatObjectStore::GetComplex(const std::string &sessionId, const std::string &key,
346 std::vector<uint8_t> &value)
347 {
348 uint32_t status = Get(sessionId, FIELDS_PREFIX + key, value);
349 if (status != SUCCESS) {
350 LOG_ERROR("GetString field not exist. %{public}d %{public}s", status, key.c_str());
351 return status;
352 }
353 value.erase(value.begin(), value.begin() + sizeof(Type));
354 return status;
355 }
356
GetType(const std::string & sessionId,const std::string & key,Type & type)357 uint32_t FlatObjectStore::GetType(const std::string &sessionId, const std::string &key, Type &type)
358 {
359 Bytes data;
360 uint32_t status = Get(sessionId, FIELDS_PREFIX + key, data);
361 if (status != SUCCESS) {
362 LOG_ERROR("GetString field not exist. %{public}d %{public}s", status, key.c_str());
363 return status;
364 }
365 status = BytesUtils::GetNum(data, 0, &type, sizeof(type));
366 if (status != SUCCESS) {
367 LOG_ERROR("GetBoolean getNum err. %{public}d", status);
368 return status;
369 }
370 return SUCCESS;
371 }
372
GetBundleName()373 std::string FlatObjectStore::GetBundleName()
374 {
375 return bundleName_;
376 }
377
CacheManager()378 CacheManager::CacheManager()
379 {
380 }
381
Save(const std::string & bundleName,const std::string & sessionId,const std::string & deviceId,const std::map<std::string,std::vector<uint8_t>> & objectData)382 uint32_t CacheManager::Save(const std::string &bundleName, const std::string &sessionId, const std::string &deviceId,
383 const std::map<std::string, std::vector<uint8_t>> &objectData)
384 {
385 std::unique_lock<std::mutex> lck(mutex_);
386 auto block = std::make_shared<BlockData<std::tuple<bool, int32_t>>>(WAIT_TIME, std::tuple{ true, ERR_DB_GET_FAIL });
387 int32_t status = SaveObject(bundleName, sessionId, deviceId, objectData,
388 [deviceId, block](const std::map<std::string, int32_t> &results) {
389 LOG_INFO("CacheManager::task callback");
390 if (results.count(deviceId) != 0) {
391 block->SetValue({ false, results.at(deviceId) });
392 } else {
393 block->SetValue({ false, ERR_DB_GET_FAIL });
394 }
395 });
396 if (status != SUCCESS) {
397 LOG_ERROR("SaveObject failed");
398 return status;
399 }
400 LOG_INFO("CacheManager::start wait");
401 auto [timeout, res] = block->GetValue();
402 LOG_INFO("CacheManager::end wait, timeout: %{public}d, result: %{public}d", timeout, res);
403 return res;
404 }
405
RevokeSave(const std::string & bundleName,const std::string & sessionId)406 uint32_t CacheManager::RevokeSave(const std::string &bundleName, const std::string &sessionId)
407 {
408 std::unique_lock<std::mutex> lck(mutex_);
409 auto block = std::make_shared<BlockData<std::tuple<bool, int32_t>>>(WAIT_TIME, std::tuple{ true, ERR_DB_GET_FAIL });
410 int32_t status = RevokeSaveObject(bundleName, sessionId, [block](int32_t result) {
411 LOG_INFO("CacheManager::task callback");
412 block->SetValue({ false, result });
413 });
414 if (status != SUCCESS) {
415 LOG_ERROR("RevokeSaveObject failed");
416 return status;
417 }
418 LOG_INFO("CacheManager::start wait");
419 auto [timeout, res] = block->GetValue();
420 LOG_INFO("CacheManager::end wait, timeout: %{public}d, result: %{public}d", timeout, res);
421 return res;
422 }
423
SaveObject(const std::string & bundleName,const std::string & sessionId,const std::string & deviceId,const std::map<std::string,std::vector<uint8_t>> & objectData,const std::function<void (const std::map<std::string,int32_t> &)> & callback)424 int32_t CacheManager::SaveObject(const std::string &bundleName, const std::string &sessionId,
425 const std::string &deviceId, const std::map<std::string, std::vector<uint8_t>> &objectData,
426 const std::function<void(const std::map<std::string, int32_t> &)> &callback)
427 {
428 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
429 if (proxy == nullptr) {
430 LOG_ERROR("proxy is nullptr.");
431 RadarReporter::ReportStateError(std::string(__FUNCTION__), SAVE, SAVE_TO_SERVICE,
432 RADAR_FAILED, SA_DIED, FINISHED);
433 return ERR_PROCESSING;
434 }
435 sptr<ObjectSaveCallbackBroker> objectSaveCallback = new (std::nothrow) ObjectSaveCallback(callback);
436 if (objectSaveCallback == nullptr) {
437 LOG_ERROR("CacheManager::SaveObject no memory for ObjectSaveCallback malloc!");
438 RadarReporter::ReportStateError(std::string(__FUNCTION__), SAVE, SAVE_TO_SERVICE,
439 RADAR_FAILED, NO_MEMORY, FINISHED);
440 return ERR_NULL_PTR;
441 }
442 int32_t status = proxy->ObjectStoreSave(
443 bundleName, sessionId, deviceId, objectData, objectSaveCallback->AsObject().GetRefPtr());
444 if (status != SUCCESS) {
445 LOG_ERROR("object save failed code=%d.", static_cast<int>(status));
446 RadarReporter::ReportStateError(std::string(__FUNCTION__), SAVE, SAVE_TO_SERVICE,
447 RADAR_FAILED, IPC_ERROR, FINISHED);
448 } else {
449 RadarReporter::ReportStage(std::string(__FUNCTION__), SAVE, SAVE_TO_SERVICE, RADAR_SUCCESS);
450 }
451 return status;
452 }
453
RevokeSaveObject(const std::string & bundleName,const std::string & sessionId,const std::function<void (int32_t)> & callback)454 int32_t CacheManager::RevokeSaveObject(
455 const std::string &bundleName, const std::string &sessionId, const std::function<void(int32_t)> &callback)
456 {
457 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
458 if (proxy == nullptr) {
459 LOG_ERROR("proxy is nullptr.");
460 return ERR_PROCESSING;
461 }
462 sptr<ObjectRevokeSaveCallbackBroker> objectRevokeSaveCallback = new (std::nothrow)
463 ObjectRevokeSaveCallback(callback);
464 if (objectRevokeSaveCallback == nullptr) {
465 LOG_ERROR("CacheManager::RevokeSaveObject no memory for ObjectRevokeSaveCallback malloc!");
466 return ERR_NULL_PTR;
467 }
468 int32_t status = proxy->ObjectStoreRevokeSave(
469 bundleName, sessionId, objectRevokeSaveCallback->AsObject().GetRefPtr());
470 if (status != SUCCESS) {
471 LOG_ERROR("object revoke save failed code=%d.", static_cast<int>(status));
472 }
473 LOG_INFO("object revoke save successful");
474 return status;
475 }
476
ResumeObject(const std::string & bundleName,const std::string & sessionId,std::function<void (const std::map<std::string,std::vector<uint8_t>> & data,bool allReady)> & callback)477 int32_t CacheManager::ResumeObject(const std::string &bundleName, const std::string &sessionId,
478 std::function<void(const std::map<std::string, std::vector<uint8_t>> &data, bool allReady)> &callback)
479 {
480 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
481 if (proxy == nullptr) {
482 LOG_ERROR("proxy is nullptr.");
483 return ERR_NULL_PTR;
484 }
485 sptr<ObjectRetrieveCallbackBroker> objectRetrieveCallback = new (std::nothrow) ObjectRetrieveCallback(callback);
486 if (objectRetrieveCallback == nullptr) {
487 LOG_ERROR("CacheManager::ResumeObject no memory for ObjectRetrieveCallback malloc!");
488 return ERR_NULL_PTR;
489 }
490 int32_t status = proxy->ObjectStoreRetrieve(
491 bundleName, sessionId, objectRetrieveCallback->AsObject().GetRefPtr());
492 if (status != SUCCESS) {
493 LOG_ERROR("object resume failed code=%d.", static_cast<int>(status));
494 }
495 LOG_INFO("object resume successful");
496 return status;
497 }
498
SubscribeDataChange(const std::string & bundleName,const std::string & sessionId,std::function<void (const std::map<std::string,std::vector<uint8_t>> & data,bool allReady)> & callback)499 int32_t CacheManager::SubscribeDataChange(const std::string &bundleName, const std::string &sessionId,
500 std::function<void(const std::map<std::string, std::vector<uint8_t>> &data, bool allReady)> &callback)
501 {
502 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
503 if (proxy == nullptr) {
504 LOG_ERROR("proxy is nullptr.");
505 return ERR_NULL_PTR;
506 }
507 sptr<ObjectChangeCallbackBroker> objectRemoteResumeCallback = new (std::nothrow) ObjectChangeCallback(callback);
508 if (objectRemoteResumeCallback == nullptr) {
509 LOG_ERROR("CacheManager::SubscribeDataChange no memory for ObjectChangeCallback malloc!");
510 return ERR_NULL_PTR;
511 }
512 ClientAdaptor::RegisterClientDeathListener(bundleName, objectRemoteResumeCallback->AsObject());
513 int32_t status = proxy->RegisterDataObserver(
514 bundleName, sessionId, objectRemoteResumeCallback->AsObject().GetRefPtr());
515 if (status != SUCCESS) {
516 LOG_ERROR("object remote resume failed code=%d.", static_cast<int>(status));
517 }
518 LOG_INFO("object remote resume successful");
519 return status;
520 }
521
UnregisterDataChange(const std::string & bundleName,const std::string & sessionId)522 int32_t CacheManager::UnregisterDataChange(const std::string &bundleName, const std::string &sessionId)
523 {
524 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
525 if (proxy == nullptr) {
526 LOG_ERROR("proxy is nullptr.");
527 return ERR_NULL_PTR;
528 }
529 int32_t status = proxy->UnregisterDataChangeObserver(bundleName, sessionId);
530 if (status != SUCCESS) {
531 LOG_ERROR("object remote resume failed code=%d.", static_cast<int>(status));
532 }
533 LOG_INFO("object unregister data change observer successful");
534 return status;
535 }
536
DeleteSnapshot(const std::string & bundleName,const std::string & sessionId)537 int32_t CacheManager::DeleteSnapshot(const std::string &bundleName, const std::string &sessionId)
538 {
539 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
540 if (proxy == nullptr) {
541 LOG_ERROR("proxy is nullptr.");
542 return ERR_NULL_PTR;
543 }
544 int32_t status = proxy->DeleteSnapshot(bundleName, sessionId);
545 if (status != SUCCESS) {
546 LOG_ERROR("object delete snapshot failed code=%d.", static_cast<int>(status));
547 }
548 LOG_INFO("object delete snapshot successful");
549 return status;
550 }
551
IsContinue()552 bool CacheManager::IsContinue()
553 {
554 sptr<OHOS::DistributedObject::IObjectService> proxy = ClientAdaptor::GetObjectService();
555 if (proxy == nullptr) {
556 LOG_ERROR("Object service proxy is nullptr");
557 return false;
558 }
559 bool isContinue = false;
560 int32_t status = proxy->IsContinue(isContinue);
561 if (status != SUCCESS) {
562 LOG_ERROR("Get continue state failed, status: %{public}d, isContinue: %{public}d", status, isContinue);
563 }
564 return isContinue;
565 }
566 } // namespace OHOS::ObjectStore
567