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 OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H
17 #define OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H
18
19 #include <condition_variable>
20 #include <mutex>
21 #include <chrono>
22
23 #include "ability_manager_client.h"
24 #include "ability_connect_callback_stub.h"
25 #include "want.h"
26
27 #include "i_appgallery_service.h"
28 #include "module_external/bms_adapter.h"
29 #include "filemgmt_libhilog.h"
30
31 namespace OHOS::FileManagement::Backup {
32 using namespace OHOS::AppExecFwk;
33 const int32_t CONNECT_TIME = 3;
34
35 template <typename T> class AppGralleryConnection : public AbilityConnectionStub {
36 public:
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)37 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
38 int resultCode)
39 {
40 std::string uri = element.GetURI();
41 HILOGI("OnAbilityConnectDone, uri = %{public}s", uri.c_str());
42 T::appRemoteObj_ = remoteObject;
43 T::conditionVal_.notify_one();
44 }
45
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)46 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
47 {
48 std::string uri = element.GetURI();
49 HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str());
50 T::appRemoteObj_ = nullptr;
51 }
52 };
53
ConnectExtAbility()54 template <typename T> bool ConnectExtAbility()
55 {
56 HILOGI("ConnectExtAbility called");
57 std::lock_guard<std::mutex> autoLock(T::appRemoteObjLock_);
58 if (T::appRemoteObj_ != nullptr) {
59 return true;
60 }
61
62 auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName();
63 if (appGalleryBundleName.empty()) {
64 HILOGI("ConnectExtAbility GetAppGalleryBundleName failed");
65 return false;
66 }
67
68 Want want;
69 want.SetElementName(appGalleryBundleName.c_str(), T::abilityName);
70 sptr<IAbilityConnection> connect = new AppGralleryConnection<T>();
71 auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, connect, -1);
72
73 std::unique_lock<std::mutex> uniqueLock(T::connectMutex);
74 T::conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME));
75 if (ret != IAppGalleryService::ERR_OK || T::appRemoteObj_ == nullptr) {
76 HILOGI("ConnectExtAbility failed, ret=%{public}d", ret);
77 T::appRemoteObj_ = nullptr;
78 return false;
79 }
80 HILOGI("ConnectExtAbility success, ret=%{public}d", ret);
81 return true;
82 };
83 } // namespace OHOS::FileManagement::Backup
84 #endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H