• 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 "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_mgr_proxy.h"
24 #include "log_print.h"
25 
26 namespace OHOS::DataShare {
Connect(std::shared_ptr<Context> context)27 bool ExtensionConnectAdaptor::Connect(std::shared_ptr<Context> context)
28 {
29     for (auto const &extension : context->bundleInfo.extensionInfos) {
30         if (extension.type == AppExecFwk::ExtensionAbilityType::DATASHARE) {
31             return DoConnect(context);
32         }
33     }
34     return false;
35 }
36 
ExtensionConnectAdaptor()37 ExtensionConnectAdaptor::ExtensionConnectAdaptor() : data_(1) {}
38 
DoConnect(std::shared_ptr<Context> context)39 bool ExtensionConnectAdaptor::DoConnect(std::shared_ptr<Context> context)
40 {
41     data_.Clear();
42     callback_ = new (std::nothrow) CallbackImpl(data_);
43     if (callback_ == nullptr) {
44         ZLOGE("new failed");
45         return false;
46     }
47     ZLOGI("Start connect %{public}s", context->uri.c_str());
48     ErrCode ret = ExtensionMgrProxy::GetInstance()->Connect(context->uri, callback_->AsObject(), nullptr);
49     if (ret != ERR_OK) {
50         ZLOGE("connect ability failed, ret = %{public}d", ret);
51         return false;
52     }
53     bool result = data_.GetValue();
54     if (result) {
55         ZLOGI("connect ability ended successfully");
56     }
57     return result;
58 }
59 
TryAndWait(std::shared_ptr<Context> context,int maxWaitTime)60 bool ExtensionConnectAdaptor::TryAndWait(std::shared_ptr<Context> context, int maxWaitTime)
61 {
62     ExtensionConnectAdaptor strategy;
63     return AppConnectManager::Wait(
64         context->calledBundleName, maxWaitTime,
65         [&context, &strategy]() {
66             return strategy.Connect(context);
67         },
68         [&strategy]() {
69             strategy.Disconnect();
70             return true;
71         });
72 }
73 
Disconnect()74 void ExtensionConnectAdaptor::Disconnect()
75 {
76     if (callback_ == nullptr) {
77         ZLOGE("callback null");
78         return;
79     }
80     data_.Clear();
81     ZLOGI("Start disconnect");
82     ExtensionMgrProxy::GetInstance()->DisConnect(callback_->AsObject());
83     if (!data_.GetValue()) {
84         ZLOGI("disconnect ability ended successfully");
85     }
86     callback_ = nullptr;
87 }
88 } // namespace OHOS::DataShare