• 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 "cloud_backup_restore.h"
26 #include "clone_restore.h"
27 #include "upgrade_restore.h"
28 #include "others_clone_restore.h"
29 #include "rdb_sql_statistic.h"
30 #include "rdb_types.h"
31 
32 namespace OHOS {
33 namespace Media {
34 class MediaLibraryBackupObserver : public DistributedRdb::SqlObserver {
35 public:
~MediaLibraryBackupObserver()36     virtual ~MediaLibraryBackupObserver() {}
37 
OnStatistic(const SqlExecutionInfo & info)38     void OnStatistic(const SqlExecutionInfo &info) override
39     {
40         for (auto sql : info.sql_) {
41             MEDIA_DEBUG_LOG("DEBUG_MediaLibraryBackup totaltime: %{public}lld waitTime: %{public}lld "
42                 "prepareTime: %{public}lld executeTime: %{public}lld sql: %{public}s",
43                 (long long) info.totalTime_, (long long) info.waitTime_, (long long) info.prepareTime_,
44                 (long long) info.executeTime_, sql.c_str());
45         }
46     };
47 };
48 
49 const int DUAL_FIRST_NUMBER = 65;
50 const int DUAL_SECOND_NUMBER = 110;
51 const int DUAL_THIRD_NUMBER = 100;
52 const int DUAL_FOURTH_NUMBER = 114;
53 const int DUAL_FIFTH_NUMBER = 111;
54 const int DUAL_SIXTH_NUMBER = 105;
55 const int DUAL_SEVENTH_NUMBER = 100;
GetInstance(void)56 BackupRestoreService &BackupRestoreService::GetInstance(void)
57 {
58     static BackupRestoreService inst;
59     return inst;
60 }
61 
GetDualDirName()62 std::string GetDualDirName()
63 {
64     int arr[] = { DUAL_FIRST_NUMBER, DUAL_SECOND_NUMBER, DUAL_THIRD_NUMBER, DUAL_FOURTH_NUMBER, DUAL_FIFTH_NUMBER,
65         DUAL_SIXTH_NUMBER, DUAL_SEVENTH_NUMBER };
66     int len = sizeof(arr) / sizeof(arr[0]);
67     std::string dualDirName = "";
68     for (int i = 0; i < len; i++) {
69         dualDirName += char(arr[i]);
70     }
71     return dualDirName;
72 }
73 
Init(const RestoreInfo & info)74 void BackupRestoreService::Init(const RestoreInfo &info)
75 {
76     if (restoreService_ != nullptr) {
77         return;
78     }
79     serviceBackupDir_ = info.backupDir;
80     switch (info.sceneCode) {
81         case UPGRADE_RESTORE_ID:
82             restoreService_ = std::make_unique<UpgradeRestore>(info.galleryAppName, info.mediaAppName, info.sceneCode,
83                 GetDualDirName());
84             serviceBackupDir_ = RESTORE_SANDBOX_DIR;
85             break;
86         case DUAL_FRAME_CLONE_RESTORE_ID:
87             restoreService_ = std::make_unique<UpgradeRestore>(info.galleryAppName, info.mediaAppName, info.sceneCode);
88             break;
89         case CLOUD_BACKUP_RESTORE_ID:
90             restoreService_ = std::make_unique<CloudBackupRestore>(info.galleryAppName, info.mediaAppName,
91                 info.sceneCode);
92             break;
93         case I_PHONE_CLONE_RESTORE:
94             restoreService_ = std::make_unique<OthersCloneRestore>(info.sceneCode, info.mediaAppName, info.bundleInfo);
95             break;
96         case OTHERS_PHONE_CLONE_RESTORE:
97         case LITE_PHONE_CLONE_RESTORE:
98             restoreService_ = std::make_unique<OthersCloneRestore>(info.sceneCode, info.mediaAppName);
99             break;
100         default:
101             restoreService_ = std::make_unique<CloneRestore>();
102     }
103     restoreService_->restoreInfo_ = info.bundleInfo;
104 }
105 
StartRestore(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info)106 void BackupRestoreService::StartRestore(const std::shared_ptr<AbilityRuntime::Context> &context,
107     const RestoreInfo &info)
108 {
109     MEDIA_INFO_LOG("Start restore service: %{public}d", info.sceneCode);
110     Init(info);
111     CHECK_AND_RETURN_LOG(restoreService_ != nullptr, "Create media restore service failed.");
112     CHECK_AND_EXECUTE(context == nullptr, BackupFileUtils::CreateDataShareHelper(context->GetToken()));
113     restoreService_->StartRestore(serviceBackupDir_, UPGRADE_FILE_DIR);
114 }
115 
StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info,std::string & restoreExInfo)116 void BackupRestoreService::StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> &context,
117     const RestoreInfo &info, std::string &restoreExInfo)
118 {
119     shared_ptr<MediaLibraryBackupObserver> sqlPrintObserver = std::make_shared<MediaLibraryBackupObserver>();
120     DistributedRdb::SqlStatistic::Subscribe(sqlPrintObserver);
121     MEDIA_INFO_LOG("Start restoreEx service: %{public}d", info.sceneCode);
122     Init(info);
123     if (restoreService_ == nullptr) {
124         MEDIA_ERR_LOG("Create media restore service failed.");
125         restoreExInfo = "";
126         return;
127     }
128     CHECK_AND_EXECUTE(context == nullptr, BackupFileUtils::CreateDataShareHelper(context->GetToken()));
129     restoreService_->StartRestoreEx(serviceBackupDir_, UPGRADE_FILE_DIR, restoreExInfo);
130 }
131 
GetBackupInfo(int32_t sceneCode,std::string & backupInfo)132 void BackupRestoreService::GetBackupInfo(int32_t sceneCode, std::string &backupInfo)
133 {
134     MEDIA_INFO_LOG("Start restore service: %{public}d", sceneCode);
135     if (sceneCode != CLONE_RESTORE_ID) {
136         MEDIA_ERR_LOG("StartRestoreEx current scene is not supported");
137         backupInfo = "";
138         return;
139     }
140     Init({CLONE_RESTORE_ID, "", "", "", ""});
141     if (restoreService_ == nullptr) {
142         MEDIA_ERR_LOG("Create media restore service failed.");
143         backupInfo = "";
144         return;
145     }
146     backupInfo = restoreService_->GetBackupInfo();
147 }
148 
GetProgressInfo(std::string & progressInfo)149 void BackupRestoreService::GetProgressInfo(std::string &progressInfo)
150 {
151     MEDIA_INFO_LOG("Start get progressInfo");
152     if (restoreService_ == nullptr) {
153         MEDIA_WARN_LOG("Media restore service not created.");
154         progressInfo = "";
155         return;
156     }
157     progressInfo = restoreService_->GetProgressInfo();
158 }
159 
StartBackup(int32_t sceneCode,const std::string & galleryAppName,const std::string & mediaAppName)160 void BackupRestoreService::StartBackup(int32_t sceneCode, const std::string &galleryAppName,
161     const std::string &mediaAppName)
162 {
163     MEDIA_INFO_LOG("Start backup service: %{public}d", sceneCode);
164     if (sceneCode != CLONE_RESTORE_ID) {
165         MEDIA_ERR_LOG("StartBackup current scene is not supported");
166         return;
167     }
168     Init({CLONE_RESTORE_ID, galleryAppName, mediaAppName, "", ""});
169     CHECK_AND_RETURN_LOG(restoreService_ != nullptr, "Create media backup service failed.");
170     restoreService_->StartBackup();
171 }
172 
StartBackupEx(int32_t sceneCode,const std::string & galleryAppName,const std::string & mediaAppName,const std::string & backupInfo,std::string & backupExInfo)173 void BackupRestoreService::StartBackupEx(int32_t sceneCode, const std::string &galleryAppName,
174     const std::string &mediaAppName, const std::string& backupInfo, std::string& backupExInfo)
175 {
176     MEDIA_INFO_LOG("Start backupEx service: %{public}d", sceneCode);
177     if (sceneCode != CLONE_RESTORE_ID) {
178         MEDIA_ERR_LOG("StartBackupEx current scene is not supported");
179         backupExInfo = "";
180         return;
181     }
182     Init({CLONE_RESTORE_ID, galleryAppName, mediaAppName, "", backupInfo});
183     if (restoreService_ == nullptr) {
184         MEDIA_ERR_LOG("Create media backup service failed.");
185         backupExInfo = "";
186         return;
187     }
188     restoreService_->StartBackupEx(backupExInfo);
189 }
190 
Release(int32_t sceneCode,int32_t releaseSceneInt)191 void BackupRestoreService::Release(int32_t sceneCode, int32_t releaseSceneInt)
192 {
193     MEDIA_INFO_LOG("Start Release service, releaseScene:%{public}d", releaseSceneInt);
194     CHECK_AND_RETURN_LOG(INT_RELEASE_SCENE_MAP.count(releaseSceneInt),
195         "invalid releaseScene: %{public}d", releaseSceneInt);
196     ReleaseScene releaseScene = INT_RELEASE_SCENE_MAP.at(releaseSceneInt);
197     CHECK_AND_RETURN_LOG(sceneCode == CLONE_RESTORE_ID,
198         "current release scene is not supported, sceneCode: %{public}d releasescene: %{public}d",
199         sceneCode, releaseSceneInt);
200     Init({CLONE_RESTORE_ID, "", "", "", ""});
201     CHECK_AND_RETURN_LOG(restoreService_ != nullptr, "Create media backup service failed.");
202     restoreService_->Release(releaseScene);
203 }
204 } // namespace Media
205 } // namespace OHOS
206