1 /*
2  * Copyright (c) 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_incremental_backup_session.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 
~BIncrementalBackupSession()27 BIncrementalBackupSession::~BIncrementalBackupSession()
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<BIncrementalBackupSession> BIncrementalBackupSession::Init(Callbacks callbacks)
45 {
46     try {
47         HILOGI("Init IncrementalBackupSession Begin");
48         auto backup = make_unique<BIncrementalBackupSession>();
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->InitIncrementalBackupSession(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, "BIncrementalBackupSession::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(function<void ()> functor)73 void BIncrementalBackupSession::RegisterBackupServiceDied(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 
AppendBundles(vector<BIncrementalData> bundlesToBackup)93 ErrCode BIncrementalBackupSession::AppendBundles(vector<BIncrementalData> bundlesToBackup)
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     ErrCode res = proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup);
101     if (res != ERR_OK) {
102         std::string ss;
103         for (const auto &bundle : bundlesToBackup) {
104             ss += bundle.bundleName + ", ";
105         }
106         AppRadar::Info info(ss.c_str(), "", "");
107         AppRadar::GetInstance().RecordBackupFuncRes(info, "BIncrementalBackupSession::AppendBundles",
108             AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
109     }
110     return res;
111 }
112 
AppendBundles(vector<BIncrementalData> bundlesToBackup,std::vector<std::string> infos)113 ErrCode BIncrementalBackupSession::AppendBundles(vector<BIncrementalData> bundlesToBackup,
114     std::vector<std::string> infos)
115 {
116     auto proxy = ServiceProxy::GetInstance();
117     if (proxy == nullptr) {
118         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
119     }
120 
121     int32_t res = proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup, infos);
122     if (res != ERR_OK) {
123         std::string ss;
124         for (const auto &bundle : bundlesToBackup) {
125             ss += bundle.bundleName + ", ";
126         }
127         AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
128         AppRadar::GetInstance().RecordBackupFuncRes(info, "BIncrementalBackupSession::AppendBundles",
129             AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
130     }
131     return res;
132 }
133 
Release()134 ErrCode BIncrementalBackupSession::Release()
135 {
136     auto proxy = ServiceProxy::GetInstance();
137     if (proxy == nullptr) {
138         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
139     }
140 
141     return proxy->Release();
142 }
143 } // namespace OHOS::FileManagement::Backup