• 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 "appspawn_adapter.h"
17 
18 #include "hitrace_meter.h"
19 #include "js_runtime.h"
20 #include "parameters.h"
21 #include "runtime.h"
22 
23 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/app/main_thread.h"
24 
LoadExtendLib(AppSpawnContent * content)25 void LoadExtendLib(AppSpawnContent *content)
26 {
27     const char *acelibdir("libace.z.so");
28     APPSPAWN_LOGI("LoadExtendLib: Start calling dlopen acelibdir.");
29 #ifndef APPSPAWN_TEST
30     void *aceAbilityLib = NULL;
31     aceAbilityLib = dlopen(acelibdir, RTLD_NOW | RTLD_GLOBAL);
32     APPSPAWN_CHECK(aceAbilityLib != NULL, return, "Fail to dlopen %s, [%s]", acelibdir, dlerror());
33 #endif
34     APPSPAWN_LOGI("LoadExtendLib: Success to dlopen %s", acelibdir);
35     APPSPAWN_LOGI("LoadExtendLib: End calling dlopen");
36 
37 #if defined(APPSPAWN_TEST) || defined(ASAN_DETECTOR)
38     bool preload = OHOS::system::GetBoolParameter("const.appspawn.preload", false);
39 #else
40     bool preload = OHOS::system::GetBoolParameter("const.appspawn.preload", true);
41 #endif
42     if (!preload) {
43         APPSPAWN_LOGI("LoadExtendLib: Do not preload JS VM");
44         return;
45     }
46 
47     APPSPAWN_LOGI("LoadExtendLib: Start preload JS VM");
48     SetTraceDisabled(true);
49     OHOS::AbilityRuntime::Runtime::Options options;
50     options.lang = OHOS::AbilityRuntime::Runtime::Language::JS;
51     options.loadAce = true;
52     options.preload = true;
53 
54     auto runtime = OHOS::AbilityRuntime::Runtime::Create(options);
55     if (!runtime) {
56         APPSPAWN_LOGE("LoadExtendLib: Failed to create runtime");
57         SetTraceDisabled(false);
58         return;
59     }
60 
61     // Preload napi module
62     runtime->PreloadSystemModule("application.Ability");
63     runtime->PreloadSystemModule("application.Context");
64     runtime->PreloadSystemModule("application.AbilityContext");
65     runtime->PreloadSystemModule("request");
66 
67     // Save preloaded runtime
68     OHOS::AbilityRuntime::Runtime::SavePreloaded(std::move(runtime));
69     SetTraceDisabled(false);
70     APPSPAWN_LOGI("LoadExtendLib: End preload JS VM");
71 }
72 
RunChildProcessor(AppSpawnContent * content,AppSpawnClient * client)73 void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client)
74 {
75     APPSPAWN_LOGI("AppExecFwk::MainThread::Start");
76 #ifndef APPSPAWN_TEST
77     OHOS::AppExecFwk::MainThread::Start();
78 #endif
79 }
80