1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "AppConnectManager"
16 #include "app_connect_manager.h"
17 #include "log_print.h"
18
19 namespace OHOS::DataShare {
20 ConcurrentMap<std::string, BlockData<bool> *> AppConnectManager::blockCache_;
Wait(const std::string & bundleName,int maxWaitTime,std::function<bool ()> connect,std::function<void ()> disconnect)21 bool AppConnectManager::Wait(const std::string &bundleName,
22 int maxWaitTime, std::function<bool()> connect, std::function<void()> disconnect)
23 {
24 BlockData<bool> block(maxWaitTime, false);
25 auto result = blockCache_.ComputeIfAbsent(bundleName, [&block](const std::string &key) {
26 return █
27 });
28 if (!result) {
29 ZLOGE("is running %{public}s", bundleName.c_str());
30 return false;
31 }
32 ZLOGI("start connect %{public}s", bundleName.c_str());
33 result = connect();
34 if (!result) {
35 ZLOGE("connect failed %{public}s", bundleName.c_str());
36 blockCache_.Erase(bundleName);
37 return false;
38 }
39 result = block.GetValue();
40 ZLOGI("end wait %{public}s", bundleName.c_str());
41 blockCache_.Erase(bundleName);
42 disconnect();
43 return result;
44 }
45
Notify(const std::string & bundleName)46 void AppConnectManager::Notify(const std::string &bundleName)
47 {
48 ZLOGI("notify %{public}s", bundleName.c_str());
49 blockCache_.ComputeIfPresent(bundleName, [&bundleName](const std::string &key, BlockData<bool> *value) {
50 if (value == nullptr) {
51 ZLOGI("nullptr %{public}s", bundleName.c_str());
52 return false;
53 }
54 value->SetValue(true);
55 return true;
56 });
57 }
58 } // namespace OHOS::DataShare