1/*
2 * Copyright (c) 2020 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 "app_manager.h"
17
18#define __STDC_FORMAT_MACROS
19#include <cinttypes>
20#include <cstring>
21
22#include "token_generate.h"
23#include "util/abilityms_log.h"
24
25namespace OHOS {
26AppRecord *AppManager::StartAppProcess(const BundleInfo &bundleInfo)
27{
28    CHECK_NULLPTR_RETURN_PTR(bundleInfo.bundleName, "AppManager", "invalid argument");
29    AppRecord *appRecord = GetAppRecordByBundleName(bundleInfo.bundleName);
30    if (appRecord != nullptr) {
31        PRINTI("AppManager", "%{public}s AppRecord is already exist", bundleInfo.bundleName);
32        return appRecord;
33    }
34    uint64_t token = TokenGenerate::GenerateToken();
35    appRecord = new AppRecord(bundleInfo, token);
36    AbilityMsStatus status = spawnClient_.SpawnProcess(*appRecord);
37    if (!status.IsOk()) {
38        status.LogStatus();
39        delete appRecord;
40        return nullptr;
41    }
42    PRINTD("AppManager", "start app name:%{public}s, token: %{private}" PRIu64,
43        appRecord->GetBundleInfo().bundleName, token);
44    appRecords_.emplace_back(appRecord);
45    return appRecord;
46}
47
48void AppManager::RemoveAppRecord(const AppRecord &appRecord)
49{
50    for (auto iterator = appRecords_.begin(); iterator != appRecords_.end();) {
51        AppRecord *current = *iterator;
52        if (current != nullptr && current->GetIdentityId() == appRecord.GetIdentityId()) {
53            PRINTD("AppManager", "remove process %{private}" PRIu64, current->GetIdentityId());
54            current->UnloadPermission();
55            iterator = appRecords_.erase(iterator);
56            delete current;
57        } else {
58            ++iterator;
59        }
60    }
61}
62
63AbilityMsStatus AppManager::TerminateAppProcess(const char *bundleName)
64{
65    AppRecord *current = GetAppRecordByBundleName(bundleName);
66    if (current == nullptr) {
67        PRINTI("AppManager", "app record is not find");
68        return AbilityMsStatus::Ok();
69    }
70    // exit app process
71    return current->AppExitTransaction();
72}
73
74void AppManager::RemoveAppRecord(const char *bundleName)
75{
76    CHECK_NULLPTR_RETURN(bundleName, "AppManager", "start");
77    for (auto iterator = appRecords_.cbegin(); iterator != appRecords_.cend();) {
78        const AppRecord *current = *iterator;
79        if (current != nullptr && current->GetBundleInfo().bundleName != nullptr &&
80            (strcmp(current->GetBundleInfo().bundleName, bundleName) == 0)) {
81            PRINTD("AppManager", "remove process name:%{public}s", bundleName);
82            current->UnloadPermission();
83            iterator = appRecords_.erase(iterator);
84            delete current;
85        } else {
86            ++iterator;
87        }
88    }
89}
90
91const AppRecord *AppManager::GetAppRecordByToken(uint64_t token, pid_t pid)
92{
93    for (const auto &appRecord : appRecords_) {
94        if (appRecord != nullptr && appRecord->GetIdentityId() == token && appRecord->GetPid() == pid) {
95            return appRecord;
96        }
97    }
98    return nullptr;
99}
100
101AppRecord *AppManager::GetAppRecordByBundleName(const char *bundleName)
102{
103    CHECK_NULLPTR_RETURN_PTR(bundleName, "AppManager", "invalid argument");
104    for (const auto &appRecord : appRecords_) {
105        if (appRecord != nullptr && appRecord->GetBundleInfo().bundleName != nullptr &&
106            (strcmp(appRecord->GetBundleInfo().bundleName, bundleName) == 0)) {
107            return appRecord;
108        }
109    }
110    return nullptr;
111}
112}
113