• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
72 
73 namespace ConnectInner {
DoConnect(int userId)74 void DoConnect(int userId)
75 {
76     ZLOGI("do connect");
77     AAFwk::Want want;
78     want.SetAction("");
79     want.SetElementName(BUNDLE_NAME, ABILITY_NAME);
80     auto ret = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(
81         want, g_connect->AsObject(), userId);
82     if (ret != ERR_OK) {
83         ZLOGE("connect ability fail, error code:%{public}d", ret);
84         return;
85     }
86     g_connect->WaitConnect();
87 }
88 
ConnectServiceInner(int userId)89 OHOS::sptr<OHOS::IRemoteObject> ConnectServiceInner(int userId)
90 {
91     ZLOGI("Connect Service Inner access");
92     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
93     if (g_connect == nullptr) {
94         sptr<Connect> ipcConnect = new(std::nothrow) Connect();
95         if (ipcConnect == nullptr) {
96             ZLOGE("ipc connect is null");
97             return nullptr;
98         }
99         g_connect = ipcConnect;
100     }
101     if (!g_connect->IsConnect()) {
102         DoConnect(userId);
103     }
104     return g_connect->remoteObject_;
105 }
106 }