• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_mgr.h"
17 
18 #include <pthread.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include "hilog_wrapper.h"
24 #include "if_system_ability_manager.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "js_runtime_utils.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace AbilityRuntime {
32 namespace {
33 constexpr int32_t INDEX_ONE = 1;
34 constexpr int32_t ERROR_CODE_ONE = -2;
35 constexpr size_t ARGC_ONE = 1;
36 constexpr size_t ARGC_TWO = 2;
37 
38 class JsAppManager final {
39 public:
JsAppManager(sptr<OHOS::AAFwk::IAbilityManager> abilityManager)40     explicit JsAppManager(sptr<OHOS::AAFwk::IAbilityManager> abilityManager) : abilityManager_(abilityManager) {}
41     ~JsAppManager() = default;
42 
Finalizer(NativeEngine * engine,void * data,void * hint)43     static void Finalizer(NativeEngine *engine, void *data, void *hint)
44     {
45         HILOG_DEBUG("JsAppManager::Finalizer is called");
46         std::unique_ptr<JsAppManager>(static_cast<JsAppManager*>(data));
47     }
48 
KillProcessesByBundleName(NativeEngine * engine,NativeCallbackInfo * info)49     static NativeValue* KillProcessesByBundleName(NativeEngine *engine, NativeCallbackInfo *info)
50     {
51         JsAppManager *me = CheckParamsAndGetThis<JsAppManager>(engine, info);
52         return (me != nullptr) ? me->OnKillProcessByBundleName(*engine, *info) : nullptr;
53     }
54 
55 private:
56     sptr<OHOS::AAFwk::IAbilityManager> abilityManager_ = nullptr;
57 
OnKillProcessByBundleName(NativeEngine & engine,const NativeCallbackInfo & info)58     NativeValue* OnKillProcessByBundleName(NativeEngine &engine, const NativeCallbackInfo &info)
59     {
60         HILOG_DEBUG("%{public}s is called", __FUNCTION__);
61         std::string bundleName;
62 
63         if (info.argc < ARGC_ONE || info.argc > ARGC_TWO) {
64             HILOG_ERROR("Not enough params");
65             return engine.CreateUndefined();
66         }
67 
68         if (!ConvertFromJsValue(engine, info.argv[0], bundleName)) {
69             HILOG_ERROR("get bundleName failed!");
70             return engine.CreateUndefined();
71         }
72 
73         HILOG_DEBUG("kill process [%{public}s]", bundleName.c_str());
74         AsyncTask::CompleteCallback complete =
75             [bundleName, abilityManager = abilityManager_](NativeEngine &engine, AsyncTask &task,
76                 int32_t status) {
77             if (abilityManager == nullptr) {
78                 HILOG_ERROR("abilityManager nullptr");
79                 task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "abilityManager nullptr"));
80                 return;
81             }
82             auto ret = abilityManager->KillProcess(bundleName);
83             if (ret == 0) {
84                 task.Resolve(engine, CreateJsValue(engine, ret));
85             } else {
86                 task.Reject(engine, CreateJsError(engine, ret, "kill process failed."));
87             }
88         };
89 
90         NativeValue *lastParam = (info.argc == ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
91         NativeValue *result = nullptr;
92         AsyncTask::Schedule("JSAppManager::OnKillProcessByBundleName",
93             engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
94         return result;
95     }
96 };
97 } // namespace
98 
GetAbilityManagerInstance()99 OHOS::sptr<OHOS::AAFwk::IAbilityManager> GetAbilityManagerInstance()
100 {
101     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
102         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103     OHOS::sptr<OHOS::IRemoteObject> abilityObject =
104         systemAbilityManager->GetSystemAbility(OHOS::ABILITY_MGR_SERVICE_ID);
105     return OHOS::iface_cast<OHOS::AAFwk::IAbilityManager>(abilityObject);
106 }
107 
JsAppMgrInit(NativeEngine * engine,NativeValue * exportObj)108 NativeValue* JsAppMgrInit(NativeEngine *engine, NativeValue *exportObj)
109 {
110     HILOG_DEBUG("JsAppMgrInit is called");
111 
112     if (engine == nullptr || exportObj == nullptr) {
113         HILOG_ERROR("engine or exportObj null");
114         return nullptr;
115     }
116 
117     NativeObject *object = ConvertNativeValueTo<NativeObject>(exportObj);
118     if (object == nullptr) {
119         HILOG_ERROR("object null");
120         return nullptr;
121     }
122 
123     std::unique_ptr<JsAppManager> jsAppManager = std::make_unique<JsAppManager>(GetAbilityManagerInstance());
124     object->SetNativePointer(jsAppManager.release(), JsAppManager::Finalizer, nullptr);
125 
126     const char *moduleName = "JsAppManager";
127     BindNativeFunction(*engine, *object, "killProcessesByBundleName", moduleName,
128         JsAppManager::KillProcessesByBundleName);
129     HILOG_DEBUG("JsAppMgrInit end");
130     return exportObj;
131 }
132 }  // namespace AbilityRuntime
133 }  // namespace OHOS