• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "module_ipc/svc_backup_connection.h"
17 
18 #include "ability_manager_client.h"
19 #include "filemgmt_libhilog.h"
20 #include "module_ipc/svc_extension_proxy.h"
21 #include "module_ipc/svc_session_manager.h"
22 
23 namespace OHOS::FileManagement::Backup {
24 constexpr int WAIT_TIME = 3;
25 using namespace std;
26 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)27 void SvcBackupConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
28                                                const sptr<IRemoteObject> &remoteObject,
29                                                int resultCode)
30 {
31     HILOGI("called begin");
32     if (remoteObject == nullptr) {
33         HILOGE("Failed to ability connect done, remote is nullptr");
34         return;
35     }
36     backupProxy_ = iface_cast<SvcExtensionProxy>(remoteObject);
37     if (backupProxy_ == nullptr) {
38         HILOGE("Failed to ability connect done, backupProxy_ is nullptr");
39         return;
40     }
41     isConnected_.store(true);
42     string bundleName = element.GetBundleName();
43     callConnDone_(move(bundleName));
44     HILOGI("called end");
45 }
46 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)47 void SvcBackupConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
48 {
49     HILOGI("called begin");
50     if (isConnectedDone_ == false) {
51         string bundleName = element.GetBundleName();
52         HILOGE("It's error that the backup extension dies before the backup sa. name : %{public}s", bundleName.data());
53         callDied_(move(bundleName));
54     }
55     backupProxy_ = nullptr;
56     isConnected_.store(false);
57     condition_.notify_all();
58     HILOGI("called end");
59 }
60 
ConnectBackupExtAbility(AAFwk::Want & want,int32_t userId)61 ErrCode SvcBackupConnection::ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId)
62 {
63     HILOGI("called begin");
64     std::unique_lock<std::mutex> lock(mutex_);
65     ErrCode ret =
66         AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, userId);
67     HILOGI("called end, ret=%{public}d", ret);
68     return ret;
69 }
70 
DisconnectBackupExtAbility()71 ErrCode SvcBackupConnection::DisconnectBackupExtAbility()
72 {
73     HILOGI("called begin");
74     isConnectedDone_.store(true);
75     std::unique_lock<std::mutex> lock(mutex_);
76     ErrCode ret = AppExecFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this);
77     auto callback = [extConn {wptr(this)}] {
78         auto extPtr = extConn.promote();
79         if (!extPtr) {
80             HILOGE("Dis connect failed");
81             return false;
82         }
83         return extPtr->GetBackupExtProxy() == nullptr;
84     };
85     if (condition_.wait_for(lock, std::chrono::seconds(WAIT_TIME), callback)) {
86         HILOGI("Wait until the connection ends");
87     }
88     HILOGI("called end, ret=%{public}d", ret);
89     return ret;
90 }
91 
IsExtAbilityConnected()92 bool SvcBackupConnection::IsExtAbilityConnected()
93 {
94     return isConnected_.load();
95 }
96 
GetBackupExtProxy()97 sptr<IExtension> SvcBackupConnection::GetBackupExtProxy()
98 {
99     return backupProxy_;
100 }
101 } // namespace OHOS::FileManagement::Backup