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