1 /*
2 * Copyright (c) 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 "js_application_context_utils.h"
17
18 #include <map>
19
20 #include "ability_runtime_error_util.h"
21 #include "application_context.h"
22 #include "application_context_manager.h"
23 #include "hilog_wrapper.h"
24 #include "js_context_utils.h"
25 #include "js_data_struct_converter.h"
26 #include "js_hap_module_info_utils.h"
27 #include "js_resource_manager_utils.h"
28 #include "js_runtime_utils.h"
29 #include "running_process_info.h"
30
31 namespace OHOS {
32 namespace AbilityRuntime {
33 namespace {
34 constexpr char APPLICATION_CONTEXT_NAME[] = "__application_context_ptr__";
35 constexpr size_t ARGC_ZERO = 0;
36 constexpr size_t ARGC_ONE = 1;
37 constexpr size_t ARGC_TWO = 2;
38 constexpr size_t ARGC_THREE = 3;
39 constexpr size_t INDEX_ZERO = 0;
40 constexpr size_t INDEX_ONE = 1;
41 constexpr int32_t ERROR_CODE_ONE = 1;
42 const char* MD_NAME = "JsApplicationContextUtils";
43 } // namespace
44
CreateBundleContext(NativeEngine * engine,NativeCallbackInfo * info)45 NativeValue *JsApplicationContextUtils::CreateBundleContext(NativeEngine *engine, NativeCallbackInfo *info)
46 {
47 JsApplicationContextUtils *me =
48 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
49 return me != nullptr ? me->OnCreateBundleContext(*engine, *info) : nullptr;
50 }
51
OnCreateBundleContext(NativeEngine & engine,NativeCallbackInfo & info)52 NativeValue *JsApplicationContextUtils::OnCreateBundleContext(NativeEngine &engine, NativeCallbackInfo &info)
53 {
54 if (info.argc == 0) {
55 HILOG_ERROR("Not enough params");
56 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
57 return engine.CreateUndefined();
58 }
59
60 auto applicationContext = applicationContext_.lock();
61 if (!applicationContext) {
62 HILOG_WARN("applicationContext is already released");
63 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
64 return engine.CreateUndefined();
65 }
66
67 std::string bundleName;
68 if (!ConvertFromJsValue(engine, info.argv[0], bundleName)) {
69 HILOG_ERROR("Parse bundleName failed");
70 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
71 return engine.CreateUndefined();
72 }
73
74 auto bundleContext = applicationContext->CreateBundleContext(bundleName);
75 if (!bundleContext) {
76 HILOG_ERROR("bundleContext is nullptr");
77 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
78 return engine.CreateUndefined();
79 }
80
81 NativeValue* value = CreateJsBaseContext(engine, bundleContext, nullptr, nullptr, true);
82 auto systemModule = JsRuntime::LoadSystemModuleByEngine(&engine, "application.Context", &value, 1);
83 if (systemModule == nullptr) {
84 HILOG_WARN("invalid systemModule.");
85 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
86 return engine.CreateUndefined();
87 }
88 auto contextObj = systemModule->Get();
89 NativeObject *nativeObj = ConvertNativeValueTo<NativeObject>(contextObj);
90 if (nativeObj == nullptr) {
91 HILOG_ERROR("Failed to get context native object");
92 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
93 return engine.CreateUndefined();
94 }
95 auto workContext = new (std::nothrow) std::weak_ptr<Context>(bundleContext);
96 nativeObj->ConvertToNativeBindingObject(&engine, DetachCallbackFunc, AttachBaseContext, workContext, nullptr);
97 nativeObj->SetNativePointer(
98 workContext,
99 [](NativeEngine *, void *data, void *) {
100 HILOG_INFO("Finalizer for weak_ptr bundle context is called");
101 delete static_cast<std::weak_ptr<Context> *>(data);
102 },
103 nullptr);
104 return contextObj;
105 }
106
SwitchArea(NativeEngine * engine,NativeCallbackInfo * info)107 NativeValue *JsApplicationContextUtils::SwitchArea(NativeEngine *engine, NativeCallbackInfo *info)
108 {
109 HILOG_INFO("JsApplicationContextUtils::SwitchArea is called");
110 JsApplicationContextUtils *me =
111 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
112 return me != nullptr ? me->OnSwitchArea(*engine, *info) : nullptr;
113 }
114
OnSwitchArea(NativeEngine & engine,NativeCallbackInfo & info)115 NativeValue *JsApplicationContextUtils::OnSwitchArea(NativeEngine &engine, NativeCallbackInfo &info)
116 {
117 if (info.argc == 0) {
118 HILOG_ERROR("Not enough params");
119 return engine.CreateUndefined();
120 }
121
122 auto applicationContext = applicationContext_.lock();
123 if (!applicationContext) {
124 HILOG_WARN("applicationContext is already released");
125 return engine.CreateUndefined();
126 }
127
128 int mode = 0;
129 if (!ConvertFromJsValue(engine, info.argv[0], mode)) {
130 HILOG_ERROR("Parse mode failed");
131 return engine.CreateUndefined();
132 }
133
134 applicationContext->SwitchArea(mode);
135
136 NativeValue *thisVar = info.thisVar;
137 NativeObject *object = ConvertNativeValueTo<NativeObject>(thisVar);
138 if (object == nullptr) {
139 HILOG_ERROR("object is nullptr");
140 return engine.CreateUndefined();
141 }
142 BindNativeProperty(*object, "cacheDir", GetCacheDir);
143 BindNativeProperty(*object, "tempDir", GetTempDir);
144 BindNativeProperty(*object, "filesDir", GetFilesDir);
145 BindNativeProperty(*object, "distributedFilesDir", GetDistributedFilesDir);
146 BindNativeProperty(*object, "databaseDir", GetDatabaseDir);
147 BindNativeProperty(*object, "preferencesDir", GetPreferencesDir);
148 BindNativeProperty(*object, "bundleCodeDir", GetBundleCodeDir);
149 return engine.CreateUndefined();
150 }
151
152
CreateModuleContext(NativeEngine * engine,NativeCallbackInfo * info)153 NativeValue* JsApplicationContextUtils::CreateModuleContext(NativeEngine* engine, NativeCallbackInfo* info)
154 {
155 JsApplicationContextUtils *me =
156 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
157 return me != nullptr ? me->OnCreateModuleContext(*engine, *info) : nullptr;
158 }
159
OnCreateModuleContext(NativeEngine & engine,NativeCallbackInfo & info)160 NativeValue* JsApplicationContextUtils::OnCreateModuleContext(NativeEngine& engine, NativeCallbackInfo& info)
161 {
162 auto applicationContext = applicationContext_.lock();
163 if (!applicationContext) {
164 HILOG_WARN("applicationContext is already released");
165 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
166 return engine.CreateUndefined();
167 }
168
169 std::string moduleName;
170 std::shared_ptr<Context> moduleContext = nullptr;
171 if (!ConvertFromJsValue(engine, info.argv[1], moduleName)) {
172 HILOG_INFO("Parse inner module name.");
173 if (!ConvertFromJsValue(engine, info.argv[0], moduleName)) {
174 HILOG_ERROR("Parse moduleName failed");
175 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
176 return engine.CreateUndefined();
177 }
178 moduleContext = applicationContext->CreateModuleContext(moduleName);
179 } else {
180 std::string bundleName;
181 if (!ConvertFromJsValue(engine, info.argv[0], bundleName)) {
182 HILOG_ERROR("Parse bundleName failed");
183 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
184 return engine.CreateUndefined();
185 }
186 HILOG_INFO("Parse outer module name.");
187 moduleContext = applicationContext->CreateModuleContext(bundleName, moduleName);
188 }
189
190 if (!moduleContext) {
191 HILOG_ERROR("failed to create module context.");
192 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
193 return engine.CreateUndefined();
194 }
195
196 NativeValue* value = CreateJsBaseContext(engine, moduleContext, nullptr, nullptr, true);
197 auto systemModule = JsRuntime::LoadSystemModuleByEngine(&engine, "application.Context", &value, 1);
198 if (systemModule == nullptr) {
199 HILOG_WARN("invalid systemModule.");
200 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
201 return engine.CreateUndefined();
202 }
203 auto contextObj = systemModule->Get();
204 NativeObject *nativeObj = ConvertNativeValueTo<NativeObject>(contextObj);
205 if (nativeObj == nullptr) {
206 HILOG_ERROR("OnCreateModuleContext, Failed to get context native object");
207 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
208 return engine.CreateUndefined();
209 }
210 auto workContext = new (std::nothrow) std::weak_ptr<Context>(moduleContext);
211 nativeObj->ConvertToNativeBindingObject(&engine, DetachCallbackFunc, AttachBaseContext, workContext, nullptr);
212 nativeObj->SetNativePointer(
213 workContext,
214 [](NativeEngine *, void *data, void *) {
215 HILOG_INFO("Finalizer for weak_ptr module context is called");
216 delete static_cast<std::weak_ptr<Context> *>(data);
217 },
218 nullptr);
219 return contextObj;
220 }
221
GetArea(NativeEngine * engine,NativeCallbackInfo * info)222 NativeValue* JsApplicationContextUtils::GetArea(NativeEngine* engine, NativeCallbackInfo* info)
223 {
224 HILOG_INFO("JsApplicationContextUtils::GetArea is called");
225 JsApplicationContextUtils *me =
226 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
227 return me != nullptr ? me->OnGetArea(*engine, *info) : nullptr;
228 }
229
OnGetArea(NativeEngine & engine,NativeCallbackInfo & info)230 NativeValue* JsApplicationContextUtils::OnGetArea(NativeEngine& engine, NativeCallbackInfo& info)
231 {
232 auto applicationContext = applicationContext_.lock();
233 if (!applicationContext) {
234 HILOG_WARN("applicationContext is already released");
235 return engine.CreateUndefined();
236 }
237 int area = applicationContext->GetArea();
238 return engine.CreateNumber(area);
239 }
240
GetCacheDir(NativeEngine * engine,NativeCallbackInfo * info)241 NativeValue *JsApplicationContextUtils::GetCacheDir(NativeEngine *engine, NativeCallbackInfo *info)
242 {
243 HILOG_INFO("JsApplicationContextUtils::GetCacheDir is called");
244 JsApplicationContextUtils *me =
245 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
246 return me != nullptr ? me->OnGetCacheDir(*engine, *info) : nullptr;
247 }
248
OnGetCacheDir(NativeEngine & engine,NativeCallbackInfo & info)249 NativeValue *JsApplicationContextUtils::OnGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info)
250 {
251 auto applicationContext = applicationContext_.lock();
252 if (!applicationContext) {
253 HILOG_WARN("applicationContext is already released");
254 return engine.CreateUndefined();
255 }
256 std::string path = applicationContext->GetCacheDir();
257 return engine.CreateString(path.c_str(), path.length());
258 }
259
GetTempDir(NativeEngine * engine,NativeCallbackInfo * info)260 NativeValue *JsApplicationContextUtils::GetTempDir(NativeEngine *engine, NativeCallbackInfo *info)
261 {
262 HILOG_INFO("JsApplicationContextUtils::GetTempDir is called");
263 JsApplicationContextUtils *me =
264 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
265 return me != nullptr ? me->OnGetTempDir(*engine, *info) : nullptr;
266 }
267
OnGetTempDir(NativeEngine & engine,NativeCallbackInfo & info)268 NativeValue *JsApplicationContextUtils::OnGetTempDir(NativeEngine &engine, NativeCallbackInfo &info)
269 {
270 auto applicationContext = applicationContext_.lock();
271 if (!applicationContext) {
272 HILOG_WARN("applicationContext is already released");
273 return engine.CreateUndefined();
274 }
275 std::string path = applicationContext->GetTempDir();
276 return engine.CreateString(path.c_str(), path.length());
277 }
278
GetFilesDir(NativeEngine * engine,NativeCallbackInfo * info)279 NativeValue *JsApplicationContextUtils::GetFilesDir(NativeEngine *engine, NativeCallbackInfo *info)
280 {
281 HILOG_INFO("JsApplicationContextUtils::GetFilesDir is called");
282 JsApplicationContextUtils *me =
283 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
284 return me != nullptr ? me->OnGetFilesDir(*engine, *info) : nullptr;
285 }
286
OnGetFilesDir(NativeEngine & engine,NativeCallbackInfo & info)287 NativeValue *JsApplicationContextUtils::OnGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info)
288 {
289 auto applicationContext = applicationContext_.lock();
290 if (!applicationContext) {
291 HILOG_WARN("applicationContext is already released");
292 return engine.CreateUndefined();
293 }
294 std::string path = applicationContext->GetFilesDir();
295 return engine.CreateString(path.c_str(), path.length());
296 }
297
GetDistributedFilesDir(NativeEngine * engine,NativeCallbackInfo * info)298 NativeValue *JsApplicationContextUtils::GetDistributedFilesDir(NativeEngine *engine, NativeCallbackInfo *info)
299 {
300 HILOG_INFO("JsApplicationContextUtils::GetDistributedFilesDir is called");
301 JsApplicationContextUtils *me =
302 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
303 return me != nullptr ? me->OnGetDistributedFilesDir(*engine, *info) : nullptr;
304 }
305
OnGetDistributedFilesDir(NativeEngine & engine,NativeCallbackInfo & info)306 NativeValue *JsApplicationContextUtils::OnGetDistributedFilesDir(NativeEngine &engine, NativeCallbackInfo &info)
307 {
308 auto applicationContext = applicationContext_.lock();
309 if (!applicationContext) {
310 HILOG_WARN("applicationContext is already released");
311 return engine.CreateUndefined();
312 }
313 std::string path = applicationContext->GetDistributedFilesDir();
314 return engine.CreateString(path.c_str(), path.length());
315 }
316
GetDatabaseDir(NativeEngine * engine,NativeCallbackInfo * info)317 NativeValue *JsApplicationContextUtils::GetDatabaseDir(NativeEngine *engine, NativeCallbackInfo *info)
318 {
319 HILOG_INFO("JsApplicationContextUtils::GetDatabaseDir is called");
320 JsApplicationContextUtils *me =
321 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
322 return me != nullptr ? me->OnGetDatabaseDir(*engine, *info) : nullptr;
323 }
324
OnGetDatabaseDir(NativeEngine & engine,NativeCallbackInfo & info)325 NativeValue *JsApplicationContextUtils::OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info)
326 {
327 auto applicationContext = applicationContext_.lock();
328 if (!applicationContext) {
329 HILOG_WARN("applicationContext is already released");
330 return engine.CreateUndefined();
331 }
332 std::string path = applicationContext->GetDatabaseDir();
333 return engine.CreateString(path.c_str(), path.length());
334 }
335
GetPreferencesDir(NativeEngine * engine,NativeCallbackInfo * info)336 NativeValue *JsApplicationContextUtils::GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info)
337 {
338 HILOG_INFO("JsApplicationContextUtils::GetPreferencesDir is called");
339 JsApplicationContextUtils *me =
340 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
341 return me != nullptr ? me->OnGetPreferencesDir(*engine, *info) : nullptr;
342 }
343
OnGetPreferencesDir(NativeEngine & engine,NativeCallbackInfo & info)344 NativeValue *JsApplicationContextUtils::OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info)
345 {
346 auto applicationContext = applicationContext_.lock();
347 if (!applicationContext) {
348 HILOG_WARN("applicationContext is already released");
349 return engine.CreateUndefined();
350 }
351 std::string path = applicationContext->GetPreferencesDir();
352 return engine.CreateString(path.c_str(), path.length());
353 }
354
GetBundleCodeDir(NativeEngine * engine,NativeCallbackInfo * info)355 NativeValue *JsApplicationContextUtils::GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info)
356 {
357 HILOG_INFO("JsApplicationContextUtils::GetBundleCodeDir is called");
358 JsApplicationContextUtils *me =
359 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
360 return me != nullptr ? me->OnGetBundleCodeDir(*engine, *info) : nullptr;
361 }
362
OnGetBundleCodeDir(NativeEngine & engine,NativeCallbackInfo & info)363 NativeValue *JsApplicationContextUtils::OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info)
364 {
365 auto applicationContext = applicationContext_.lock();
366 if (!applicationContext) {
367 HILOG_WARN("applicationContext is already released");
368 return engine.CreateUndefined();
369 }
370 std::string path = applicationContext->GetBundleCodeDir();
371 return engine.CreateString(path.c_str(), path.length());
372 }
373
KillProcessBySelf(NativeEngine * engine,NativeCallbackInfo * info)374 NativeValue *JsApplicationContextUtils::KillProcessBySelf(NativeEngine *engine, NativeCallbackInfo *info)
375 {
376 JsApplicationContextUtils *me =
377 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
378 return me != nullptr ? me->OnKillProcessBySelf(*engine, *info) : nullptr;
379 }
380
OnKillProcessBySelf(NativeEngine & engine,NativeCallbackInfo & info)381 NativeValue *JsApplicationContextUtils::OnKillProcessBySelf(NativeEngine &engine, NativeCallbackInfo &info)
382 {
383 // only support 0 or 1 params
384 if (info.argc != ARGC_ZERO && info.argc != ARGC_ONE) {
385 HILOG_ERROR("Not enough params");
386 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
387 return engine.CreateUndefined();
388 }
389 HILOG_DEBUG("kill self process");
390 AsyncTask::CompleteCallback complete =
391 [applicationContext = applicationContext_](NativeEngine& engine, AsyncTask& task, int32_t status) {
392 auto context = applicationContext.lock();
393 if (!context) {
394 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST,
395 "applicationContext if already released."));
396 return;
397 }
398 context->KillProcessBySelf();
399 task.ResolveWithNoError(engine, engine.CreateUndefined());
400 };
401 NativeValue* lastParam = (info.argc = ARGC_ONE) ? info.argv[INDEX_ZERO] : nullptr;
402 NativeValue* result = nullptr;
403 AsyncTask::Schedule("JsApplicationContextUtils::OnkillProcessBySelf",
404 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
405 return result;
406 }
407
GetRunningProcessInformation(NativeEngine * engine,NativeCallbackInfo * info)408 NativeValue *JsApplicationContextUtils::GetRunningProcessInformation(NativeEngine *engine, NativeCallbackInfo *info)
409 {
410 JsApplicationContextUtils *me =
411 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
412 return me != nullptr ? me->OnGetRunningProcessInformation(*engine, *info) : nullptr;
413 }
414
OnGetRunningProcessInformation(NativeEngine & engine,NativeCallbackInfo & info)415 NativeValue *JsApplicationContextUtils::OnGetRunningProcessInformation(NativeEngine &engine, NativeCallbackInfo &info)
416 {
417 // only support 0 or 1 params
418 if (info.argc != ARGC_ZERO && info.argc != ARGC_ONE) {
419 HILOG_ERROR("Not enough params");
420 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
421 return engine.CreateUndefined();
422 }
423 HILOG_DEBUG("Get Process Info");
424 auto complete = [applicationContext = applicationContext_](NativeEngine& engine, AsyncTask& task, int32_t status) {
425 auto context = applicationContext.lock();
426 if (!context) {
427 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST,
428 "applicationContext if already released."));
429 return;
430 }
431 AppExecFwk::RunningProcessInfo processInfo;
432 auto ret = context->GetProcessRunningInformation(processInfo);
433 if (ret == 0) {
434 NativeValue* objValue = engine.CreateObject();
435 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
436 object->SetProperty("processName", CreateJsValue(engine, processInfo.processName_));
437 object->SetProperty("pid", CreateJsValue(engine, processInfo.pid_));
438 object->SetProperty("uid", CreateJsValue(engine, processInfo.uid_));
439 object->SetProperty("bundleNames", CreateNativeArray(engine, processInfo.bundleNames));
440 object->SetProperty("state", CreateJsValue(engine, processInfo.state_));
441 object->SetProperty("isContinuousTask", CreateJsValue(engine, processInfo.isContinuousTask));
442 object->SetProperty("isKeepAlive", CreateJsValue(engine, processInfo.isKeepAlive));
443 object->SetProperty("isFocused", CreateJsValue(engine, processInfo.isFocused));
444 task.ResolveWithNoError(engine, objValue);
445 } else {
446 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR,
447 "Get process infos failed."));
448 }
449 };
450
451 NativeValue* lastParam = (info.argc == ARGC_ONE) ? info.argv[INDEX_ZERO] : nullptr;
452 NativeValue* result = nullptr;
453 AsyncTask::Schedule("JsApplicationContextUtils::OnGetRunningProcessInformation",
454 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
455 return result;
456 }
457
Finalizer(NativeEngine * engine,void * data,void * hint)458 void JsApplicationContextUtils::Finalizer(NativeEngine *engine, void *data, void *hint)
459 {
460 HILOG_INFO("JsApplicationContextUtils::Finalizer is called");
461 std::unique_ptr<JsApplicationContextUtils>(static_cast<JsApplicationContextUtils *>(data));
462 }
463
RegisterAbilityLifecycleCallback(NativeEngine * engine,NativeCallbackInfo * info)464 NativeValue *JsApplicationContextUtils::RegisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info)
465 {
466 JsApplicationContextUtils *me =
467 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
468 return me != nullptr ? me->OnRegisterAbilityLifecycleCallback(*engine, *info) : nullptr;
469 }
470
UnregisterAbilityLifecycleCallback(NativeEngine * engine,NativeCallbackInfo * info)471 NativeValue *JsApplicationContextUtils::UnregisterAbilityLifecycleCallback(
472 NativeEngine *engine, NativeCallbackInfo *info)
473 {
474 JsApplicationContextUtils *me =
475 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
476 return me != nullptr ? me->OnUnregisterAbilityLifecycleCallback(*engine, *info) : nullptr;
477 }
478
OnRegisterAbilityLifecycleCallback(NativeEngine & engine,NativeCallbackInfo & info)479 NativeValue *JsApplicationContextUtils::OnRegisterAbilityLifecycleCallback(
480 NativeEngine &engine, NativeCallbackInfo &info)
481 {
482 HILOG_INFO("OnRegisterAbilityLifecycleCallback is called");
483 // only support one params
484 if (info.argc != ARGC_ONE) {
485 HILOG_ERROR("Not enough params.");
486 return engine.CreateUndefined();
487 }
488
489 auto applicationContext = applicationContext_.lock();
490 if (applicationContext == nullptr) {
491 HILOG_ERROR("ApplicationContext is nullptr.");
492 return engine.CreateUndefined();
493 }
494 if (callback_ != nullptr) {
495 HILOG_DEBUG("callback_ is not nullptr.");
496 return engine.CreateNumber(callback_->Register(info.argv[0]));
497 }
498 callback_ = std::make_shared<JsAbilityLifecycleCallback>(&engine);
499 int32_t callbackId = callback_->Register(info.argv[INDEX_ZERO]);
500 applicationContext->RegisterAbilityLifecycleCallback(callback_);
501 HILOG_INFO("OnRegisterAbilityLifecycleCallback is end");
502 return engine.CreateNumber(callbackId);
503 }
504
OnUnregisterAbilityLifecycleCallback(NativeEngine & engine,NativeCallbackInfo & info)505 NativeValue *JsApplicationContextUtils::OnUnregisterAbilityLifecycleCallback(
506 NativeEngine &engine, NativeCallbackInfo &info)
507 {
508 HILOG_INFO("OnUnregisterAbilityLifecycleCallback is called");
509 int32_t errCode = 0;
510 auto applicationContext = applicationContext_.lock();
511 if (applicationContext == nullptr) {
512 HILOG_ERROR("ApplicationContext is nullptr.");
513 errCode = ERROR_CODE_ONE;
514 }
515 int32_t callbackId = -1;
516 if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
517 HILOG_ERROR("OnUnregisterAbilityLifecycleCallback, Not enough params");
518 errCode = ERROR_CODE_ONE;
519 } else {
520 napi_get_value_int32(reinterpret_cast<napi_env>(&engine),
521 reinterpret_cast<napi_value>(info.argv[INDEX_ZERO]), &callbackId);
522 HILOG_DEBUG("callbackId is %{public}d.", callbackId);
523 }
524 std::weak_ptr<JsAbilityLifecycleCallback> callbackWeak(callback_);
525 AsyncTask::CompleteCallback complete = [callbackWeak, callbackId, errCode](
526 NativeEngine &engine, AsyncTask &task, int32_t status) {
527 if (errCode != 0) {
528 task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params."));
529 return;
530 }
531 auto callback = callbackWeak.lock();
532 if (callback == nullptr) {
533 HILOG_ERROR("callback is nullptr");
534 task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "callback is nullptr"));
535 return;
536 }
537
538 HILOG_DEBUG("OnUnregisterAbilityLifecycleCallback begin");
539 if (!callback->UnRegister(callbackId)) {
540 HILOG_ERROR("call UnRegister failed!");
541 task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "call UnRegister failed!"));
542 return;
543 }
544
545 task.Resolve(engine, engine.CreateUndefined());
546 };
547 NativeValue *lastParam = (info.argc <= ARGC_ONE) ? nullptr : info.argv[INDEX_ONE];
548 NativeValue *result = nullptr;
549 AsyncTask::Schedule("JsApplicationContextUtils::OnUnregisterAbilityLifecycleCallback", engine,
550 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
551 return result;
552 }
553
RegisterEnvironmentCallback(NativeEngine * engine,NativeCallbackInfo * info)554 NativeValue *JsApplicationContextUtils::RegisterEnvironmentCallback(NativeEngine *engine, NativeCallbackInfo *info)
555 {
556 JsApplicationContextUtils *me =
557 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
558 return me != nullptr ? me->OnRegisterEnvironmentCallback(*engine, *info) : nullptr;
559 }
560
UnregisterEnvironmentCallback(NativeEngine * engine,NativeCallbackInfo * info)561 NativeValue *JsApplicationContextUtils::UnregisterEnvironmentCallback(
562 NativeEngine *engine, NativeCallbackInfo *info)
563 {
564 JsApplicationContextUtils *me =
565 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
566 return me != nullptr ? me->OnUnregisterEnvironmentCallback(*engine, *info) : nullptr;
567 }
568
OnRegisterEnvironmentCallback(NativeEngine & engine,NativeCallbackInfo & info)569 NativeValue *JsApplicationContextUtils::OnRegisterEnvironmentCallback(
570 NativeEngine &engine, NativeCallbackInfo &info)
571 {
572 HILOG_DEBUG("OnRegisterEnvironmentCallback is called");
573 // only support one params
574 if (info.argc != ARGC_ONE) {
575 HILOG_ERROR("Not enough params.");
576 return engine.CreateUndefined();
577 }
578
579 auto applicationContext = applicationContext_.lock();
580 if (applicationContext == nullptr) {
581 HILOG_ERROR("ApplicationContext is nullptr.");
582 return engine.CreateUndefined();
583 }
584 if (envCallback_ != nullptr) {
585 HILOG_DEBUG("envCallback_ is not nullptr.");
586 return engine.CreateNumber(envCallback_->Register(info.argv[0]));
587 }
588 envCallback_ = std::make_shared<JsEnvironmentCallback>(&engine);
589 int32_t callbackId = envCallback_->Register(info.argv[INDEX_ZERO]);
590 applicationContext->RegisterEnvironmentCallback(envCallback_);
591 HILOG_DEBUG("OnRegisterEnvironmentCallback is end");
592 return engine.CreateNumber(callbackId);
593 }
594
OnUnregisterEnvironmentCallback(NativeEngine & engine,NativeCallbackInfo & info)595 NativeValue *JsApplicationContextUtils::OnUnregisterEnvironmentCallback(
596 NativeEngine &engine, NativeCallbackInfo &info)
597 {
598 HILOG_DEBUG("OnUnregisterEnvironmentCallback is called");
599 int32_t errCode = 0;
600 auto applicationContext = applicationContext_.lock();
601 if (applicationContext == nullptr) {
602 HILOG_ERROR("ApplicationContext is nullptr.");
603 errCode = ERROR_CODE_ONE;
604 }
605 int32_t callbackId = -1;
606 if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) {
607 HILOG_ERROR("OnUnregisterEnvironmentCallback, Not enough params");
608 errCode = ERROR_CODE_ONE;
609 } else {
610 napi_get_value_int32(
611 reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(info.argv[INDEX_ZERO]), &callbackId);
612 HILOG_DEBUG("callbackId is %{public}d.", callbackId);
613 }
614 std::weak_ptr<JsEnvironmentCallback> envCallbackWeak(envCallback_);
615 AsyncTask::CompleteCallback complete = [envCallbackWeak, callbackId, errCode](
616 NativeEngine &engine, AsyncTask &task, int32_t status) {
617 if (errCode != 0) {
618 task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params."));
619 return;
620 }
621 auto env_callback = envCallbackWeak.lock();
622 if (env_callback == nullptr) {
623 HILOG_ERROR("env_callback is nullptr");
624 task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "env_callback is nullptr"));
625 return;
626 }
627
628 HILOG_DEBUG("OnUnregisterEnvironmentCallback begin");
629 if (!env_callback->UnRegister(callbackId)) {
630 HILOG_ERROR("call UnRegister failed!");
631 task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "call UnRegister failed!"));
632 return;
633 }
634
635 task.Resolve(engine, engine.CreateUndefined());
636 };
637 NativeValue *lastParam = (info.argc <= ARGC_ONE) ? nullptr : info.argv[INDEX_ONE];
638 NativeValue *result = nullptr;
639 AsyncTask::Schedule("JsApplicationContextUtils::OnUnregisterEnvironmentCallback", engine,
640 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
641 return result;
642 }
643
On(NativeEngine * engine,NativeCallbackInfo * info)644 NativeValue *JsApplicationContextUtils::On(NativeEngine *engine, NativeCallbackInfo *info)
645 {
646 JsApplicationContextUtils *me =
647 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
648 return me != nullptr ? me->OnOn(*engine, *info) : nullptr;
649 }
650
Off(NativeEngine * engine,NativeCallbackInfo * info)651 NativeValue *JsApplicationContextUtils::Off(NativeEngine *engine, NativeCallbackInfo *info)
652 {
653 JsApplicationContextUtils *me =
654 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
655 return me != nullptr ? me->OnOff(*engine, *info) : nullptr;
656 }
657
OnOn(NativeEngine & engine,NativeCallbackInfo & info)658 NativeValue *JsApplicationContextUtils::OnOn(NativeEngine &engine, NativeCallbackInfo &info)
659 {
660 HILOG_INFO("OnOn is called");
661
662 if (info.argc != ARGC_TWO) {
663 HILOG_ERROR("Not enough params.");
664 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
665 return engine.CreateUndefined();
666 }
667
668 if (info.argv[0]->TypeOf() != NATIVE_STRING) {
669 HILOG_ERROR("param0 is invalid");
670 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
671 return engine.CreateUndefined();
672 }
673 std::string type;
674 if (!ConvertFromJsValue(engine, info.argv[0], type)) {
675 HILOG_ERROR("convert type failed!");
676 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
677 return engine.CreateUndefined();
678 }
679
680 if (type == "abilityLifecycle") {
681 return OnOnAbilityLifecycle(engine, info);
682 }
683 if (type == "environment") {
684 return OnOnEnvironment(engine, info);
685 }
686 HILOG_ERROR("on function type not match.");
687 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
688 return engine.CreateUndefined();
689 }
690
OnOff(NativeEngine & engine,const NativeCallbackInfo & info)691 NativeValue *JsApplicationContextUtils::OnOff(NativeEngine &engine, const NativeCallbackInfo &info)
692 {
693 HILOG_INFO("OnOff is called");
694
695 int32_t callbackId = -1;
696 if (info.argc != ARGC_TWO && info.argc != ARGC_THREE) {
697 HILOG_ERROR("Not enough params");
698 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
699 return engine.CreateUndefined();
700 } else {
701 napi_get_value_int32(
702 reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(info.argv[1]), &callbackId);
703 HILOG_DEBUG("callbackId is %{public}d.", callbackId);
704 }
705
706 if (info.argv[0]->TypeOf() != NATIVE_STRING) {
707 HILOG_ERROR("param0 is invalid");
708 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
709 return engine.CreateUndefined();
710 }
711 std::string type;
712 if (!ConvertFromJsValue(engine, info.argv[0], type)) {
713 HILOG_ERROR("convert type failed!");
714 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
715 return engine.CreateUndefined();
716 }
717
718 if (type == "abilityLifecycle") {
719 return OnOffAbilityLifecycle(engine, info, callbackId);
720 }
721 if (type == "environment") {
722 return OnOffEnvironment(engine, info, callbackId);
723 }
724 HILOG_ERROR("off function type not match.");
725 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
726 return engine.CreateUndefined();
727 }
728
OnOnAbilityLifecycle(NativeEngine & engine,NativeCallbackInfo & info)729 NativeValue *JsApplicationContextUtils::OnOnAbilityLifecycle(
730 NativeEngine &engine, NativeCallbackInfo &info)
731 {
732 HILOG_INFO("OnOnAbilityLifecycle is called");
733
734 auto applicationContext = applicationContext_.lock();
735 if (applicationContext == nullptr) {
736 HILOG_ERROR("ApplicationContext is nullptr.");
737 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
738 return engine.CreateUndefined();
739 }
740
741 if (callback_ != nullptr) {
742 HILOG_DEBUG("callback_ is not nullptr.");
743 return engine.CreateNumber(callback_->Register(info.argv[1]));
744 }
745 callback_ = std::make_shared<JsAbilityLifecycleCallback>(&engine);
746 int32_t callbackId = callback_->Register(info.argv[1]);
747 applicationContext->RegisterAbilityLifecycleCallback(callback_);
748 HILOG_INFO("OnOnAbilityLifecycle is end");
749 return engine.CreateNumber(callbackId);
750 }
751
OnOffAbilityLifecycle(NativeEngine & engine,const NativeCallbackInfo & info,int32_t callbackId)752 NativeValue *JsApplicationContextUtils::OnOffAbilityLifecycle(
753 NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId)
754 {
755 HILOG_INFO("OnOffAbilityLifecycle is called");
756
757 auto applicationContext = applicationContext_.lock();
758 if (applicationContext == nullptr) {
759 HILOG_ERROR("ApplicationContext is nullptr.");
760 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
761 return engine.CreateUndefined();
762 }
763
764 std::weak_ptr<JsAbilityLifecycleCallback> callbackWeak(callback_);
765 AsyncTask::CompleteCallback complete = [callbackWeak, callbackId](
766 NativeEngine &engine, AsyncTask &task, int32_t status) {
767 auto callback = callbackWeak.lock();
768 if (callback == nullptr) {
769 HILOG_ERROR("callback is nullptr");
770 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER,
771 "callback is nullptr"));
772 return;
773 }
774
775 HILOG_DEBUG("OnOffAbilityLifecycle begin");
776 if (!callback->UnRegister(callbackId)) {
777 HILOG_ERROR("call UnRegister failed!");
778 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER,
779 "call UnRegister failed!"));
780 return;
781 }
782
783 task.ResolveWithNoError(engine, engine.CreateUndefined());
784 };
785 NativeValue *lastParam = (info.argc <= ARGC_TWO) ? nullptr : info.argv[INDEX_ONE];
786 NativeValue *result = nullptr;
787 AsyncTask::Schedule("JsApplicationContextUtils::OnOffAbilityLifecycle", engine,
788 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
789 return result;
790 }
791
OnOnEnvironment(NativeEngine & engine,NativeCallbackInfo & info)792 NativeValue *JsApplicationContextUtils::OnOnEnvironment(
793 NativeEngine &engine, NativeCallbackInfo &info)
794 {
795 HILOG_DEBUG("OnOnEnvironment is called");
796
797 auto applicationContext = applicationContext_.lock();
798 if (applicationContext == nullptr) {
799 HILOG_ERROR("ApplicationContext is nullptr.");
800 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
801 return engine.CreateUndefined();
802 }
803
804 if (envCallback_ != nullptr) {
805 HILOG_DEBUG("envCallback_ is not nullptr.");
806 return engine.CreateNumber(envCallback_->Register(info.argv[1]));
807 }
808 envCallback_ = std::make_shared<JsEnvironmentCallback>(&engine);
809 int32_t callbackId = envCallback_->Register(info.argv[1]);
810 applicationContext->RegisterEnvironmentCallback(envCallback_);
811 HILOG_DEBUG("OnOnEnvironment is end");
812 return engine.CreateNumber(callbackId);
813 }
814
OnOffEnvironment(NativeEngine & engine,const NativeCallbackInfo & info,int32_t callbackId)815 NativeValue *JsApplicationContextUtils::OnOffEnvironment(
816 NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId)
817 {
818 HILOG_DEBUG("OnOffEnvironment is called");
819
820 auto applicationContext = applicationContext_.lock();
821 if (applicationContext == nullptr) {
822 HILOG_ERROR("ApplicationContext is nullptr.");
823 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
824 return engine.CreateUndefined();
825 }
826
827 std::weak_ptr<JsEnvironmentCallback> envCallbackWeak(envCallback_);
828 AsyncTask::CompleteCallback complete = [envCallbackWeak, callbackId](
829 NativeEngine &engine, AsyncTask &task, int32_t status) {
830 auto env_callback = envCallbackWeak.lock();
831 if (env_callback == nullptr) {
832 HILOG_ERROR("env_callback is nullptr");
833 task.Reject(engine,
834 CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER,
835 "env_callback is nullptr"));
836 return;
837 }
838
839 HILOG_DEBUG("OnOffEnvironment begin");
840 if (!env_callback->UnRegister(callbackId)) {
841 HILOG_ERROR("call UnRegister failed!");
842 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER,
843 "call UnRegister failed!"));
844 return;
845 }
846
847 task.ResolveWithNoError(engine, engine.CreateUndefined());
848 };
849 NativeValue *lastParam = (info.argc <= ARGC_TWO) ? nullptr : info.argv[INDEX_ONE];
850 NativeValue *result = nullptr;
851 AsyncTask::Schedule("JsApplicationContextUtils::OnOffEnvironment", engine,
852 CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
853 return result;
854 }
855
GetApplicationContext(NativeEngine * engine,NativeCallbackInfo * info)856 NativeValue* JsApplicationContextUtils::GetApplicationContext(NativeEngine* engine, NativeCallbackInfo* info)
857 {
858 JsApplicationContextUtils *me =
859 CheckParamsAndGetThis<JsApplicationContextUtils>(engine, info, APPLICATION_CONTEXT_NAME);
860 return me != nullptr ? me->OnGetApplicationContext(*engine, *info) : nullptr;
861 }
862
OnGetApplicationContext(NativeEngine & engine,NativeCallbackInfo & info)863 NativeValue* JsApplicationContextUtils::OnGetApplicationContext(NativeEngine& engine, NativeCallbackInfo& info)
864 {
865 HILOG_INFO("GetApplicationContext start");
866 auto applicationContext = applicationContext_.lock();
867 if (!applicationContext) {
868 HILOG_WARN("applicationContext is already released");
869 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
870 return engine.CreateUndefined();
871 }
872
873 NativeValue* value = CreateJsApplicationContext(engine);
874 auto systemModule = JsRuntime::LoadSystemModuleByEngine(&engine, "application.ApplicationContext", &value, 1);
875 if (systemModule == nullptr) {
876 HILOG_WARN("OnGetApplicationContext, invalid systemModule.");
877 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
878 return engine.CreateUndefined();
879 }
880 auto contextObj = systemModule->Get();
881 NativeObject *nativeObj = ConvertNativeValueTo<NativeObject>(contextObj);
882 if (nativeObj == nullptr) {
883 HILOG_ERROR("OnGetApplicationContext, Failed to get context native object");
884 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
885 return engine.CreateUndefined();
886 }
887 auto workContext = new (std::nothrow) std::weak_ptr<ApplicationContext>(applicationContext);
888 nativeObj->ConvertToNativeBindingObject(&engine, DetachCallbackFunc, AttachApplicationContext,
889 workContext, nullptr);
890 nativeObj->SetNativePointer(
891 workContext,
892 [](NativeEngine *, void *data, void *) {
893 HILOG_INFO("Finalizer for weak_ptr application context is called");
894 delete static_cast<std::weak_ptr<ApplicationContext> *>(data);
895 },
896 nullptr);
897 return contextObj;
898 }
899
CreateJsApplicationContext(NativeEngine & engine)900 NativeValue* JsApplicationContextUtils::CreateJsApplicationContext(NativeEngine &engine)
901 {
902 HILOG_DEBUG("CreateJsApplicationContext start");
903
904 NativeValue* objValue = engine.CreateObject();
905 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
906 if (object == nullptr) {
907 return objValue;
908 }
909
910 std::shared_ptr<ApplicationContext> applicationContext = ApplicationContext::GetInstance();
911 if (applicationContext == nullptr) {
912 return objValue;
913 }
914
915 auto jsApplicationContextUtils = std::make_unique<JsApplicationContextUtils>(applicationContext);
916 SetNamedNativePointer(engine, *object, APPLICATION_CONTEXT_NAME, jsApplicationContextUtils.release(),
917 JsApplicationContextUtils::Finalizer);
918
919 auto appInfo = applicationContext->GetApplicationInfo();
920 if (appInfo != nullptr) {
921 object->SetProperty("applicationInfo", CreateJsApplicationInfo(engine, *appInfo));
922 }
923 auto resourceManager = applicationContext->GetResourceManager();
924 std::shared_ptr<Context> context = std::dynamic_pointer_cast<Context>(applicationContext);
925 if (resourceManager != nullptr) {
926 object->SetProperty("resourceManager", CreateJsResourceManager(engine, resourceManager, context));
927 }
928
929 BindNativeApplicationContext(engine, object);
930
931 ApplicationContextManager::GetApplicationContextManager()
932 .AddGlobalObject(std::shared_ptr<NativeReference>(engine.CreateReference(objValue, 1)));
933
934 return objValue;
935 }
936
BindNativeApplicationContext(NativeEngine & engine,NativeObject * object)937 void JsApplicationContextUtils::BindNativeApplicationContext(NativeEngine &engine, NativeObject* object)
938 {
939 BindNativeProperty(*object, "cacheDir", JsApplicationContextUtils::GetCacheDir);
940 BindNativeProperty(*object, "tempDir", JsApplicationContextUtils::GetTempDir);
941 BindNativeProperty(*object, "filesDir", JsApplicationContextUtils::GetFilesDir);
942 BindNativeProperty(*object, "distributedFilesDir", JsApplicationContextUtils::GetDistributedFilesDir);
943 BindNativeProperty(*object, "databaseDir", JsApplicationContextUtils::GetDatabaseDir);
944 BindNativeProperty(*object, "preferencesDir", JsApplicationContextUtils::GetPreferencesDir);
945 BindNativeProperty(*object, "bundleCodeDir", JsApplicationContextUtils::GetBundleCodeDir);
946 BindNativeFunction(engine, *object, "registerAbilityLifecycleCallback", MD_NAME,
947 JsApplicationContextUtils::RegisterAbilityLifecycleCallback);
948 BindNativeFunction(engine, *object, "unregisterAbilityLifecycleCallback", MD_NAME,
949 JsApplicationContextUtils::UnregisterAbilityLifecycleCallback);
950 BindNativeFunction(engine, *object, "registerEnvironmentCallback", MD_NAME,
951 JsApplicationContextUtils::RegisterEnvironmentCallback);
952 BindNativeFunction(engine, *object, "unregisterEnvironmentCallback", MD_NAME,
953 JsApplicationContextUtils::UnregisterEnvironmentCallback);
954 BindNativeFunction(engine, *object, "createBundleContext", MD_NAME, JsApplicationContextUtils::CreateBundleContext);
955 BindNativeFunction(engine, *object, "switchArea", MD_NAME, JsApplicationContextUtils::SwitchArea);
956 BindNativeFunction(engine, *object, "getArea", MD_NAME, JsApplicationContextUtils::GetArea);
957 BindNativeFunction(engine, *object, "createModuleContext", MD_NAME, JsApplicationContextUtils::CreateModuleContext);
958 BindNativeFunction(engine, *object, "on", MD_NAME, JsApplicationContextUtils::On);
959 BindNativeFunction(engine, *object, "off", MD_NAME, JsApplicationContextUtils::Off);
960 BindNativeFunction(engine, *object, "getApplicationContext", MD_NAME,
961 JsApplicationContextUtils::GetApplicationContext);
962 BindNativeFunction(engine, *object, "killProcessesBySelf", MD_NAME,
963 JsApplicationContextUtils::KillProcessBySelf);
964 BindNativeFunction(engine, *object, "killAllProcesses", MD_NAME, JsApplicationContextUtils::KillProcessBySelf);
965 BindNativeFunction(engine, *object, "getProcessRunningInformation", MD_NAME,
966 JsApplicationContextUtils::GetRunningProcessInformation);
967 BindNativeFunction(engine, *object, "getRunningProcessInformation", MD_NAME,
968 JsApplicationContextUtils::GetRunningProcessInformation);
969 }
970 } // namespace AbilityRuntime
971 } // namespace OHOS
972