• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "MediaLibraryRestoreService"
17 
18 #include <context.h>
19 #include <context_impl.h>
20 
21 #include "backup_file_utils.h"
22 #include "backup_restore_service.h"
23 #include "ffrt_inner.h"
24 #include "media_log.h"
25 #include "clone_restore.h"
26 #include "upgrade_restore.h"
27 #include "others_clone_restore.h"
28 
29 namespace OHOS {
30 namespace Media {
31 const int DUAL_FIRST_NUMBER = 65;
32 const int DUAL_SECOND_NUMBER = 110;
33 const int DUAL_THIRD_NUMBER = 100;
34 const int DUAL_FOURTH_NUMBER = 114;
35 const int DUAL_FIFTH_NUMBER = 111;
36 const int DUAL_SIXTH_NUMBER = 105;
37 const int DUAL_SEVENTH_NUMBER = 100;
GetInstance(void)38 BackupRestoreService &BackupRestoreService::GetInstance(void)
39 {
40     static BackupRestoreService inst;
41     return inst;
42 }
43 
GetDualDirName()44 std::string GetDualDirName()
45 {
46     int arr[] = { DUAL_FIRST_NUMBER, DUAL_SECOND_NUMBER, DUAL_THIRD_NUMBER, DUAL_FOURTH_NUMBER, DUAL_FIFTH_NUMBER,
47         DUAL_SIXTH_NUMBER, DUAL_SEVENTH_NUMBER };
48     int len = sizeof(arr) / sizeof(arr[0]);
49     std::string dualDirName = "";
50     for (int i = 0; i < len; i++) {
51         dualDirName += char(arr[i]);
52     }
53     return dualDirName;
54 }
55 
Init(const RestoreInfo & info)56 void BackupRestoreService::Init(const RestoreInfo &info)
57 {
58     if (restoreService_ != nullptr) {
59         return;
60     }
61     serviceBackupDir_ = info.backupDir;
62     if (info.sceneCode == UPGRADE_RESTORE_ID) {
63         restoreService_ = std::make_unique<UpgradeRestore>(info.galleryAppName, info.mediaAppName, UPGRADE_RESTORE_ID,
64             GetDualDirName());
65         serviceBackupDir_ = RESTORE_SANDBOX_DIR;
66     } else if (info.sceneCode == DUAL_FRAME_CLONE_RESTORE_ID) {
67         restoreService_ = std::make_unique<UpgradeRestore>(info.galleryAppName, info.mediaAppName,
68             DUAL_FRAME_CLONE_RESTORE_ID);
69     } else if (info.sceneCode == I_PHONE_CLONE_RESTORE) {
70         restoreService_ = std::make_unique<OthersCloneRestore>(I_PHONE_CLONE_RESTORE, info.mediaAppName,
71             info.bundleInfo);
72     } else if (info.sceneCode == OTHERS_PHONE_CLONE_RESTORE) {
73         restoreService_ = std::make_unique<OthersCloneRestore>(OTHERS_PHONE_CLONE_RESTORE, info.mediaAppName);
74     } else if (info.sceneCode == LITE_PHONE_CLONE_RESTORE) {
75         restoreService_ = std::make_unique<OthersCloneRestore>(LITE_PHONE_CLONE_RESTORE, info.mediaAppName);
76     } else {
77         restoreService_ = std::make_unique<CloneRestore>();
78     }
79     restoreService_ -> restoreInfo_ = info.bundleInfo;
80 }
81 
StartRestore(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info)82 void BackupRestoreService::StartRestore(const std::shared_ptr<AbilityRuntime::Context> &context,
83     const RestoreInfo &info)
84 {
85     MEDIA_INFO_LOG("Start restore service: %{public}d", info.sceneCode);
86     Init(info);
87     if (restoreService_ == nullptr) {
88         MEDIA_ERR_LOG("Create media restore service failed.");
89         return;
90     }
91     if (context != nullptr) {
92         BackupFileUtils::CreateDataShareHelper(context->GetToken());
93     }
94     restoreService_->StartRestore(serviceBackupDir_, UPGRADE_FILE_DIR);
95 }
96 
StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info,std::string & restoreExInfo)97 void BackupRestoreService::StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> &context,
98     const RestoreInfo &info, std::string &restoreExInfo)
99 {
100     MEDIA_INFO_LOG("Start restoreEx service: %{public}d", info.sceneCode);
101     Init(info);
102     if (restoreService_ == nullptr) {
103         MEDIA_ERR_LOG("Create media restore service failed.");
104         restoreExInfo = "";
105         return;
106     }
107     if (context != nullptr) {
108         BackupFileUtils::CreateDataShareHelper(context->GetToken());
109     }
110     restoreService_->StartRestoreEx(serviceBackupDir_, UPGRADE_FILE_DIR, restoreExInfo);
111 }
112 
GetBackupInfo(int32_t sceneCode,std::string & backupInfo)113 void BackupRestoreService::GetBackupInfo(int32_t sceneCode, std::string &backupInfo)
114 {
115     MEDIA_INFO_LOG("Start restore service: %{public}d", sceneCode);
116     if (sceneCode != CLONE_RESTORE_ID) {
117         MEDIA_ERR_LOG("StartRestoreEx current scene is not supported");
118         backupInfo = "";
119         return;
120     }
121     Init({CLONE_RESTORE_ID, "", "", "", ""});
122     if (restoreService_ == nullptr) {
123         MEDIA_ERR_LOG("Create media restore service failed.");
124         backupInfo = "";
125         return;
126     }
127     backupInfo = restoreService_->GetBackupInfo();
128 }
129 
GetProgressInfo(std::string & progressInfo)130 void BackupRestoreService::GetProgressInfo(std::string &progressInfo)
131 {
132     MEDIA_INFO_LOG("Start get progressInfo");
133     if (restoreService_ == nullptr) {
134         MEDIA_WARN_LOG("Media restore service not created.");
135         progressInfo = "";
136         return;
137     }
138     progressInfo = restoreService_->GetProgressInfo();
139 }
140 
StartBackup(int32_t sceneCode,const std::string & galleryAppName,const std::string & mediaAppName)141 void BackupRestoreService::StartBackup(int32_t sceneCode, const std::string &galleryAppName,
142     const std::string &mediaAppName)
143 {
144     MEDIA_INFO_LOG("Start backup service: %{public}d", sceneCode);
145     if (sceneCode != CLONE_RESTORE_ID) {
146         MEDIA_ERR_LOG("StartBackup current scene is not supported");
147         return;
148     }
149     Init({CLONE_RESTORE_ID, galleryAppName, mediaAppName, "", ""});
150     if (restoreService_ == nullptr) {
151         MEDIA_ERR_LOG("Create media backup service failed.");
152         return;
153     }
154     restoreService_->StartBackup();
155 }
156 } // namespace Media
157 } // namespace OHOS
158