1 /*
2  * Copyright (c) 2022-2024 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 "b_session_backup.h"
17 
18 #include "b_error/b_error.h"
19 #include "b_radar/b_radar.h"
20 #include "filemgmt_libhilog.h"
21 #include "service_proxy.h"
22 #include "service_reverse.h"
23 
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26 
~BSessionBackup()27 BSessionBackup::~BSessionBackup()
28 {
29     if (!deathRecipient_) {
30         HILOGI("Death Recipient is nullptr");
31         return;
32     }
33     auto proxy = ServiceProxy::GetServiceProxyPointer();
34     if (proxy == nullptr) {
35         return;
36     }
37     auto remoteObject = proxy->AsObject();
38     if (remoteObject != nullptr) {
39         remoteObject->RemoveDeathRecipient(deathRecipient_);
40     }
41     deathRecipient_ = nullptr;
42 }
43 
Init(Callbacks callbacks)44 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks)
45 {
46     try {
47         HILOGI("Init BackupSession Begin");
48         auto backup = make_unique<BSessionBackup>();
49         ServiceProxy::InvaildInstance();
50         auto proxy = ServiceProxy::GetInstance();
51         if (proxy == nullptr) {
52             HILOGI("Failed to get backup service");
53             return nullptr;
54         }
55 
56         int32_t res = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks)));
57         if (res != ERR_OK) {
58             HILOGE("Failed to Backup because of %{public}d", res);
59             AppRadar::Info info("", "", "");
60             AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::Init",
61                 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, res);
62             return nullptr;
63         }
64 
65         backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
66         return backup;
67     } catch (const exception &e) {
68         HILOGE("Failed to Backup because of %{public}s", e.what());
69     }
70     return nullptr;
71 }
72 
RegisterBackupServiceDied(std::function<void ()> functor)73 void BSessionBackup::RegisterBackupServiceDied(std::function<void()> functor)
74 {
75     auto proxy = ServiceProxy::GetInstance();
76     if (proxy == nullptr || !functor) {
77         return;
78     }
79     auto remoteObj = proxy->AsObject();
80     if (!remoteObj) {
81         throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
82     }
83 
84     auto callback = [functor](const wptr<IRemoteObject> &obj) {
85         ServiceProxy::InvaildInstance();
86         HILOGI("Backup service died");
87         functor();
88     };
89     deathRecipient_ = sptr(new SvcDeathRecipient(callback));
90     remoteObj->AddDeathRecipient(deathRecipient_);
91 }
92 
Start()93 ErrCode BSessionBackup::Start()
94 {
95     auto proxy = ServiceProxy::GetInstance();
96     if (proxy == nullptr) {
97         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
98     }
99 
100     return proxy->Start();
101 }
102 
AppendBundles(vector<BundleName> bundlesToBackup)103 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup)
104 {
105     auto proxy = ServiceProxy::GetInstance();
106     if (proxy == nullptr) {
107         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
108     }
109 
110     int32_t res = proxy->AppendBundlesBackupSession(bundlesToBackup);
111     if (res != ERR_OK) {
112         std::string ss;
113         for (const auto &bundleName:bundlesToBackup) {
114             ss += bundleName + ", ";
115         }
116         AppRadar::Info info(ss.c_str(), "", "");
117         AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::AppendBundles",
118             AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
119     }
120     return res;
121 }
122 
AppendBundles(vector<BundleName> bundlesToBackup,vector<std::string> detailInfos)123 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup, vector<std::string> detailInfos)
124 {
125     auto proxy = ServiceProxy::GetInstance();
126     if (proxy == nullptr) {
127         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
128     }
129 
130     int32_t res = proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos);
131     if (res != ERR_OK) {
132         std::string ss;
133         for (const auto &bundleName:bundlesToBackup) {
134             ss += bundleName + ", ";
135         }
136         AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
137         AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::AppendBundles",
138             AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
139     }
140     return res;
141 }
142 
Finish()143 ErrCode BSessionBackup::Finish()
144 {
145     auto proxy = ServiceProxy::GetInstance();
146     if (proxy == nullptr) {
147         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
148     }
149 
150     return proxy->Finish();
151 }
152 
Release()153 ErrCode BSessionBackup::Release()
154 {
155     auto proxy = ServiceProxy::GetInstance();
156     if (proxy == nullptr) {
157         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
158     }
159 
160     return proxy->Release();
161 }
162 } // namespace OHOS::FileManagement::Backup