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 "Connect"
16 
17 #include "connect.h"
18 #include <thread>
19 #include "iostream"
20 
21 const int RESULT_OK = 0;
22 
23 using namespace OHOS;
24 
25 namespace {
26     constexpr const char *BUNDLE_NAME = "com.example.cloudsync";
27     constexpr const char *ABILITY_NAME = "ServiceExtAbility";
28     static OHOS::sptr<Connect> g_connect = nullptr;
29     static std::mutex mutex_;
30 }
31 
OnAbilityConnectDone(const OHOS::AppExecFwk::ElementName & element,const OHOS::sptr<OHOS::IRemoteObject> & remoteObject,int resultCode)32 void Connect::OnAbilityConnectDone(const OHOS::AppExecFwk::ElementName &element,
33                                    const OHOS::sptr<OHOS::IRemoteObject> &remoteObject,
34                                    int resultCode)
35 {
36     if (resultCode != RESULT_OK) {
37         ZLOGE("ability connect failed, error code:%{public}d", resultCode);
38     }
39 
40     ZLOGD("ability connect success, ability name:%{public}s", element.GetAbilityName().c_str());
41     remoteObject_ = remoteObject;
42     std::unique_lock<std::mutex> lock(mtx_);
43     flag_ = true;
44     cv_.notify_one();
45     lock.unlock();
46 }
47 
OnAbilityDisconnectDone(const OHOS::AppExecFwk::ElementName & element,int resultCode)48 void Connect::OnAbilityDisconnectDone(const OHOS::AppExecFwk::ElementName &element, int resultCode)
49 {
50     if (resultCode != RESULT_OK) {
51         ZLOGE("ability disconnect failed, error code:%{public}d", resultCode);
52     }
53     remoteObject_ = nullptr;
54     std::unique_lock<std::mutex> lock(mtx_);
55     flag_ = true;
56     cv_.notify_one();
57     lock.unlock();
58 }
59 
IsConnect()60 bool Connect::IsConnect()
61 {
62     return remoteObject_ != nullptr;
63 }
64 
WaitConnect()65 void Connect::WaitConnect()
66 {
67     std::unique_lock<std::mutex> lock(g_connect->mtx_);
68     cv_.wait_for(lock, std::chrono::seconds(CONNECT_TIMEOUT), [this] {
69         return flag_;
70     });
71     cv_.notify_one();
72 }
73 
74 namespace ConnectInner {
DoConnect(int userId)75 void DoConnect(int userId)
76 {
77     ZLOGI("do connect");
78     AAFwk::Want want;
79     want.SetAction("");
80     want.SetElementName(BUNDLE_NAME, ABILITY_NAME);
81     auto ret = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(
82         want, g_connect->AsObject(), userId);
83     if (ret != ERR_OK) {
84         ZLOGE("connect ability fail, error code:%{public}d", ret);
85         return;
86     }
87     g_connect->WaitConnect();
88 }
89 
ConnectServiceInner(int userId)90 OHOS::sptr<OHOS::IRemoteObject> ConnectServiceInner(int userId)
91 {
92     ZLOGI("Connect Service Inner access");
93     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
94     if (g_connect == nullptr) {
95         sptr<Connect> ipcConnect = new(std::nothrow) Connect();
96         if (ipcConnect == nullptr) {
97             ZLOGE("ipc connect is null");
98             return nullptr;
99         }
100         g_connect = ipcConnect;
101     }
102     if (!g_connect->IsConnect()) {
103         DoConnect(userId);
104     }
105     return g_connect->remoteObject_;
106 }
107 }