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 "ExtensionConnectAdaptor"
16 
17 #include "extension_connect_adaptor.h"
18 
19 #include <thread>
20 
21 #include "app_connect_manager.h"
22 #include "callback_impl.h"
23 #include "extension_ability_manager.h"
24 #include "extension_ability_info.h"
25 #include "extension_mgr_proxy.h"
26 #include "log_print.h"
27 #include "uri_utils.h"
28 
29 namespace OHOS::DataShare {
ExtensionConnectAdaptor()30 ExtensionConnectAdaptor::ExtensionConnectAdaptor() : data_(std::make_shared<BlockData<bool>>(1))
31 {
32     callback_ = new (std::nothrow) CallbackImpl(data_);
33 }
34 
DoConnect(const std::string & uri,const std::string & bundleName,AAFwk::WantParams & wantParams)35 bool ExtensionConnectAdaptor::DoConnect(const std::string &uri, const std::string &bundleName,
36     AAFwk::WantParams &wantParams)
37 {
38     data_->Clear();
39     if (callback_ == nullptr) {
40         return false;
41     }
42     ErrCode ret = ExtensionAbilityManager::GetInstance().ConnectExtension(uri, bundleName,
43         callback_->AsObject(), wantParams);
44     if (ret != ERR_OK) {
45         ZLOGE("connect ability failed, ret = %{public}d, uri: %{public}s", ret,
46             URIUtils::Anonymous(uri).c_str());
47         return false;
48     }
49     bool result = data_->GetValue();
50     ZLOGI("Do connect, result: %{public}d,  uri: %{public}s", result,
51         URIUtils::Anonymous(uri).c_str());
52     return result;
53 }
54 
TryAndWait(const std::string & uri,const std::string & bundleName,AAFwk::WantParams & wantParams,int maxWaitTime)55 bool ExtensionConnectAdaptor::TryAndWait(const std::string &uri, const std::string &bundleName,
56     AAFwk::WantParams &wantParams, int maxWaitTime)
57 {
58     ExtensionConnectAdaptor strategy;
59     return AppConnectManager::Wait(
60         bundleName, maxWaitTime,
61         [&uri, &bundleName, &strategy, &wantParams]() {
62             return strategy.DoConnect(uri, bundleName, wantParams);
63         },
64         [&strategy]() {
65             strategy.Disconnect();
66             return true;
67         });
68 }
69 
Disconnect()70 void ExtensionConnectAdaptor::Disconnect()
71 {
72     if (callback_ == nullptr) {
73         ZLOGE("callback null");
74         return;
75     }
76     data_->Clear();
77     bool result = data_->GetValue();
78     ZLOGI("Do disconnect, result: %{public}d", result);
79     callback_ = nullptr;
80 }
81 } // namespace OHOS::DataShare