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 
16 #ifndef RUST_PROJ_MOCK_CONNECT_H
17 #define RUST_PROJ_MOCK_CONNECT_H
18 
19 #include <mutex>
20 #include <condition_variable>
21 #include "ability_connect_callback_stub.h"
22 #include "extension_manager_client.h"
23 #include "extension_manager_proxy.h"
24 #include "hilog/log_cpp.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 #include "iremote_broker.h"
28 
29 static const OHOS::HiviewDFX::HiLogLabel lable = { LOG_CORE, 0xD001610, "Connect"};
30 
31 #define ZLOGD(fmt, ...)                                                       \
32     do {                                                                      \
33         if (HiLogIsLoggable(lable.domain, lable.tag, LOG_DEBUG)) {            \
34             ((void)HILOG_IMPL(lable.type, LOG_DEBUG, lable.domain, lable.tag, \
35                 LOG_TAG "::%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));  \
36         }                                                                     \
37     } while (0)
38 
39 #define ZLOGI(fmt, ...)                                                       \
40     do {                                                                      \
41         if (HiLogIsLoggable(lable.domain, lable.tag, LOG_INFO)) {             \
42             ((void)HILOG_IMPL(lable.type, LOG_INFO, lable.domain, lable.tag,  \
43                 LOG_TAG "::%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));  \
44         }                                                                     \
45     } while (0)
46 
47 #define ZLOGW(fmt, ...)                                                       \
48     do {                                                                      \
49         if (HiLogIsLoggable(lable.domain, lable.tag, LOG_WARN)) {             \
50             ((void)HILOG_IMPL(lable.type, LOG_WARN, lable.domain, lable.tag,  \
51                 LOG_TAG "::%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));  \
52         }                                                                     \
53     } while (0)
54 
55 #define ZLOGE(fmt, ...)                                                       \
56     do {                                                                      \
57         if (HiLogIsLoggable(lable.domain, lable.tag, LOG_ERROR)) {            \
58             ((void)HILOG_IMPL(lable.type, LOG_ERROR, lable.domain, lable.tag, \
59                 LOG_TAG "::%{public}s: " fmt, __FUNCTION__, ##__VA_ARGS__));  \
60         }                                                                     \
61     } while (0)
62 
63 class Connect : public OHOS::AAFwk::AbilityConnectionStub {
64 public:
65     void OnAbilityConnectDone(const OHOS::AppExecFwk::ElementName &element,
66                               const OHOS::sptr<OHOS::IRemoteObject> &remoteObject,
67                               int resultCode) override;
68 
69     void OnAbilityDisconnectDone(const OHOS::AppExecFwk::ElementName &element,
70                                  int resultCode) override;
71     bool IsConnect();
72     void WaitConnect();
73 
74 public:
75     OHOS::sptr<OHOS::IRemoteObject> remoteObject_ = nullptr;
76     std::mutex mtx_;
77     std::condition_variable cv_;
78     // Used to avoid wake-up loss
79     bool flag_ = false;
80     static constexpr int32_t CONNECT_TIMEOUT = 5;
81 };
82 
83 namespace ConnectInner {
84     void DoConnect(int userId);
85     OHOS::sptr<OHOS::IRemoteObject> ConnectServiceInner(int userId);
86 }
87 #endif // RUST_PROJ_MOCK_CONNECT_H
88