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 #ifndef OHOS_ABILITY_RUNTIME_APPLICATION_CLEANER_H
17 #define OHOS_ABILITY_RUNTIME_APPLICATION_CLEANER_H
18 
19 #include <singleton.h>
20 #include <string>
21 #include <vector>
22 
23 #include "application_context.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class ApplicationCleaner final : public std::enable_shared_from_this<ApplicationCleaner>,
28                                  public DelayedSingleton<ApplicationCleaner> {
29 public:
ApplicationCleaner()30     ApplicationCleaner() {}
~ApplicationCleaner()31     ~ApplicationCleaner() {}
32 
SetRuntimeContext(std::shared_ptr<AbilityRuntime::ApplicationContext> context)33     void SetRuntimeContext(std::shared_ptr<AbilityRuntime::ApplicationContext> context)
34     {
35         context_ = context;
36     }
37     void RenameTempData();
38     void ClearTempData();
39 
40 private:
41     int GetRootPath(std::vector<std::string> &rootPath);
42     int GetObsoleteBundleTempPath(const std::vector<std::string> &rootPath, std::vector<std::string> &tempPath);
43     bool RemoveDir(const std::string &tempPath);
44     void TraverseObsoleteTempDirectory(const std::string &currentPath, std::vector<std::string> &tempDirs);
45     bool CheckFileSize(const std::vector<std::string> &bundlePath);
46 
47 private:
48     std::shared_ptr<AbilityRuntime::ApplicationContext> context_ = nullptr;
49 };
50 } // namespace AppExecFwk
51 } // namespace OHOS
52 #endif // OHOS_ABILITY_RUNTIME_APPLICATION_CLEANER_H
53