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 #include "kvstore_death_recipient_callback.h"
17
18 #include <unistd.h>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_mgr_service.h"
22
23 using namespace OHOS::DistributedKv;
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const int32_t CHECK_TIMES = 300;
29 const int32_t CHECK_INTERVAL = 100000; // 100ms
30 } // namespace
31
KvStoreDeathRecipientCallback()32 KvStoreDeathRecipientCallback::KvStoreDeathRecipientCallback()
33 {
34 APP_LOGI("create kvstore death recipient callback instance %{public}p", this);
35 }
36
~KvStoreDeathRecipientCallback()37 KvStoreDeathRecipientCallback::~KvStoreDeathRecipientCallback()
38 {
39 APP_LOGI("destroy kvstore death recipient callback instance %{public}p", this);
40 }
41
OnRemoteDied()42 void KvStoreDeathRecipientCallback::OnRemoteDied()
43 {
44 APP_LOGI("OnRemoteDied, register data change listener begin");
45 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
46 if (!dataMgr) {
47 APP_LOGE("dataMgr is nullptr");
48 return;
49 }
50
51 auto dataStorage = dataMgr->GetDataStorage();
52 if (!dataStorage) {
53 APP_LOGE("dataStorage is nullptr");
54 return;
55 }
56
57 std::thread([dataStorage] {
58 int32_t times = 0;
59 while (times < CHECK_TIMES) {
60 times++;
61 // init kvStore.
62 if (dataStorage && dataStorage->ResetKvStore()) {
63 // register data change listener again.
64 APP_LOGI("current times is %{public}d", times);
65 break;
66 }
67 usleep(CHECK_INTERVAL);
68 }
69 }).detach();
70
71 APP_LOGI("OnRemoteDied, register data change listener end");
72 }
73 } // namespace AppExecFwk
74 } // namespace OHOS