• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 <chrono>
19 #include <iomanip>
20 
21 #include "ability_manager_client.h"
22 #include "filemgmt_libhilog.h"
23 #include "hisysevent.h"
24 #include "extension_proxy.h"
25 #include "module_ipc/svc_session_manager.h"
26 
27 namespace OHOS::FileManagement::Backup {
28 constexpr int WAIT_TIME = 3;
29 constexpr int32_t INDEX = 3;
30 constexpr int32_t MS_1000 = 1000;
31 const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS";
32 using namespace std;
33 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)34 void SvcBackupConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
35                                                const sptr<IRemoteObject> &remoteObject,
36                                                int resultCode)
37 {
38     HILOGI("called begin");
39     connectSpend_.End();
40     if (remoteObject == nullptr) {
41         HILOGE("Failed to ability connect done, remote is nullptr");
42         return;
43     }
44     backupProxy_ = iface_cast<ExtensionProxy>(remoteObject);
45     if (backupProxy_ == nullptr) {
46         HILOGE("Failed to ability connect done, backupProxy_ is nullptr");
47         return;
48     }
49     isConnected_.store(true);
50     string bundleName = element.GetBundleName();
51     HILOGI("bundleName:%{public}s, OnAbilityConnectDone, bundleNameIndexInfo:%{public}s", bundleName.c_str(),
52         bundleNameIndexInfo_.c_str());
53     auto now = std::chrono::system_clock::now();
54     auto time = std::chrono::system_clock::to_time_t(now);
55     auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
56     std::stringstream strTime;
57     strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0'))
58         << (std::setw(INDEX)) << (ms.count() % MS_1000);
59     HiSysEventWrite(
60         OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT,
61         FILE_BACKUP_EVENTS,
62         OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
63         "PROC_NAME", "ohos.appfileservice",
64         "BUNDLENAME", bundleName,
65         "PID", getpid(),
66         "TIME", strTime.str()
67     );
68     if (bundleNameIndexInfo_.find(bundleName) == string::npos) {
69         HILOGE("Current bundle name is wrong, bundleNameIndexInfo:%{public}s, bundleName:%{public}s",
70             bundleNameIndexInfo_.c_str(), bundleName.c_str());
71         return;
72     }
73     bundleName = bundleNameIndexInfo_;
74     callConnected_(move(bundleName));
75     HILOGI("called end");
76 }
77 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)78 void SvcBackupConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
79 {
80     HILOGI("called begin");
81     isConnected_.store(false);
82     string bundleName = element.GetBundleName();
83     HILOGI("bundleName:%{public}s, OnAbilityDisconnectDone, bundleNameIndexInfo:%{public}s", bundleName.c_str(),
84         bundleNameIndexInfo_.c_str());
85     if (bundleNameIndexInfo_.find(bundleName) == string::npos) {
86         HILOGE("Current bundle name is wrong, bundleNameIndexInfo:%{public}s, bundleName:%{public}s",
87             bundleNameIndexInfo_.c_str(), bundleName.c_str());
88         return;
89     }
90     bundleName = bundleNameIndexInfo_;
91     if (isConnectCalled_ == true) {
92         error_ = BError(resultCode, BError::Codes::EXT_ABILITY_DIED);
93         if (isCleanCalled_ == true) {
94             HILOGE("It's error that the backup extension clean died before the backup sa. name : %{public}s",
95                 bundleName.data());
96             callDied_(move(bundleName), true);
97         } else {
98             HILOGE("It's error that the backup extension died before the backup sa. name : %{public}s",
99                 bundleName.data());
100             callDied_(move(bundleName), false);
101         }
102     }
103     condition_.notify_all();
104     waitCondition_.notify_all();
105     HILOGI("called end, name: %{public}s", bundleNameIndexInfo_.c_str());
106 }
107 
ConnectBackupExtAbility(AAFwk::Want & want,int32_t userId,bool isCleanCalled)108 ErrCode SvcBackupConnection::ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId, bool isCleanCalled)
109 {
110     HILOGI("Called begin");
111     isCleanCalled_.store(isCleanCalled);
112     isConnectCalled_.store(true);
113     std::unique_lock<std::mutex> lock(mutex_);
114     connectSpend_.Start();
115     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, userId);
116     HILOGI("Called end, ret=%{public}d, userId=%{public}d.", ret, userId);
117     return ret;
118 }
119 
DisconnectBackupExtAbility()120 ErrCode SvcBackupConnection::DisconnectBackupExtAbility()
121 {
122     HILOGI("called begin");
123     isConnectCalled_.store(false);
124     std::unique_lock<std::mutex> lock(mutex_);
125     ErrCode ret = AppExecFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this);
126     auto callback = [extConn {wptr(this)}] {
127         auto extPtr = extConn.promote();
128         if (!extPtr) {
129             HILOGE("Dis connect failed");
130             return false;
131         }
132         return extPtr->isConnected_.load() == false;
133     };
134     if (condition_.wait_for(lock, std::chrono::seconds(WAIT_TIME), callback)) {
135         HILOGI("Wait until the connection ends");
136     }
137     HILOGI("called end, ret=%{public}d", ret);
138     return ret;
139 }
140 
WaitDisconnectDone()141 bool SvcBackupConnection::WaitDisconnectDone()
142 {
143     std::unique_lock<std::mutex> lock(waitMutex_);
144     if (waitCondition_.wait_for(lock, std::chrono::seconds(WAIT_TIME),
145         [this]() { return isConnected_.load() == false; })) {
146         HILOGI("Wait disconnected done success");
147         return true;
148     }
149     return false;
150 }
151 
IsExtAbilityConnected()152 bool SvcBackupConnection::IsExtAbilityConnected()
153 {
154     return isConnected_.load();
155 }
156 
GetBackupExtProxy()157 sptr<IExtension> SvcBackupConnection::GetBackupExtProxy()
158 {
159     return backupProxy_;
160 }
161 
SetCallback(function<void (const std::string &&)> callConnected)162 void SvcBackupConnection::SetCallback(function<void(const std::string &&)> callConnected)
163 {
164     callConnected_ = callConnected;
165 }
166 
SetCallDied(function<void (const std::string &&,bool)> callDied)167 void SvcBackupConnection::SetCallDied(function<void(const std::string &&, bool)> callDied)
168 {
169     callDied_ = callDied;
170 }
171 } // namespace OHOS::FileManagement::Backup