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 "bridge/declarative_frontend/engine/jsi/modules/jsi_app_module.h"
17
18 #include "bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
19 #include "bridge/js_frontend/engine/common/js_constants.h"
20 #include "core/common/container.h"
21 #include "core/image/image_file_cache.h"
22
23 namespace OHOS::Ace::Framework {
24
AppGetInfo(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)25 shared_ptr<JsValue> AppGetInfo(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
26 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
27 {
28 auto instance = static_cast<JsiDeclarativeEngineInstance*>(runtime->GetEmbedderData());
29 if (instance == nullptr) {
30 return runtime->NewNull();
31 }
32 auto delegate = instance->GetDelegate();
33 if (!delegate) {
34 return runtime->NewNull();
35 }
36
37 shared_ptr<JsValue> appInfo = runtime->NewObject();
38 auto appId = delegate->GetAppID();
39 auto appName = delegate->GetAppName();
40 auto versionName = delegate->GetVersionName();
41 auto versionCode = delegate->GetVersionCode();
42 appInfo->SetProperty(runtime, "appID", runtime->NewString(appId));
43 appInfo->SetProperty(runtime, "appName", runtime->NewString(appName));
44 appInfo->SetProperty(runtime, "versionName", runtime->NewString(versionName));
45 appInfo->SetProperty(runtime, "versionCode", runtime->NewNumber(versionCode));
46 return appInfo;
47 }
48
AppTerminate(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)49 shared_ptr<JsValue> AppTerminate(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
50 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
51 {
52 auto instance = static_cast<JsiDeclarativeEngineInstance*>(runtime->GetEmbedderData());
53 if (instance == nullptr) {
54 return runtime->NewNull();
55 }
56 auto delegate = instance->GetDelegate();
57 if (!delegate) {
58 return runtime->NewNull();
59 }
60 auto pipelineContext = delegate->GetPipelineContext();
61 if (!pipelineContext) {
62 return runtime->NewNull();
63 }
64 auto uiTaskExecutor = delegate->GetUiTask();
65 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
66 uiTaskExecutor.PostTask(
67 [pipelineContextWeak]() mutable {
68 auto pipelineContext = pipelineContextWeak.Upgrade();
69 if (pipelineContext) {
70 pipelineContext->Finish();
71 }
72 },
73 "ArkUIAppTerminate");
74 return runtime->NewNull();
75 }
76
AppSetImageCacheCount(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)77 shared_ptr<JsValue> AppSetImageCacheCount(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
78 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
79 {
80 auto container = Container::Current();
81 if (!container) {
82 return runtime->NewNull();
83 }
84 auto pipelineContext = container->GetPipelineContext();
85 if (!pipelineContext) {
86 return runtime->NewNull();
87 }
88 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
89 return runtime->NewNull();
90 }
91 int32_t size = argv[0]->ToInt32(runtime);
92 if (size < 0) {
93 return runtime->NewNull();
94 }
95 auto taskExecutor = pipelineContext->GetTaskExecutor();
96 if (!taskExecutor) {
97 return runtime->NewNull();
98 }
99 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
100 taskExecutor->PostTask(
101 [pipelineContextWeak, size]() mutable {
102 auto pipelineContext = pipelineContextWeak.Upgrade();
103 if (pipelineContext) {
104 auto imageCache = pipelineContext->GetImageCache();
105 if (imageCache) {
106 imageCache->SetCapacity(size);
107 }
108 }
109 },
110 TaskExecutor::TaskType::UI, "ArkUISetImageCacheCount");
111 return runtime->NewNull();
112 }
113
AppSetImageRawDataCacheSize(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)114 shared_ptr<JsValue> AppSetImageRawDataCacheSize(
115 const shared_ptr<JsRuntime>& runtime,
116 const shared_ptr<JsValue>& thisObj,
117 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
118 {
119 auto container = Container::Current();
120 if (!container) {
121 return runtime->NewNull();
122 }
123 auto pipelineContext = container->GetPipelineContext();
124 if (!pipelineContext) {
125 return runtime->NewNull();
126 }
127 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
128 return runtime->NewNull();
129 }
130 int32_t size = argv[0]->ToInt32(runtime);
131 if (size < 0) {
132 return runtime->NewNull();
133 }
134 auto taskExecutor = pipelineContext->GetTaskExecutor();
135 if (!taskExecutor) {
136 return runtime->NewNull();
137 }
138 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
139 taskExecutor->PostTask(
140 [pipelineContextWeak, size]() mutable {
141 auto pipelineContext = pipelineContextWeak.Upgrade();
142 if (pipelineContext) {
143 auto imageCache = pipelineContext->GetImageCache();
144 if (imageCache) {
145 imageCache->SetDataCacheLimit(size);
146 }
147 }
148 },
149 TaskExecutor::TaskType::UI, "ArkUISetImageDataCacheSize");
150 return runtime->NewNull();
151 }
152
AppSetImageFileCacheSize(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)153 shared_ptr<JsValue> AppSetImageFileCacheSize(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
154 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
155 {
156 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
157 return runtime->NewNull();
158 }
159 int32_t size = argv[0]->ToInt32(runtime);
160 if (size < 0) {
161 return runtime->NewNull();
162 }
163 ImageFileCache::GetInstance().SetCacheFileLimit(size);
164 return runtime->NewNull();
165 }
166
AppRequestFullWindow(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)167 shared_ptr<JsValue> AppRequestFullWindow(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
168 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
169 {
170 return runtime->NewNull();
171 }
172
AppScreenOnVisible(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)173 shared_ptr<JsValue> AppScreenOnVisible(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
174 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
175 {
176 return runtime->NewNull();
177 }
178
InitAppModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & moduleObj)179 void InitAppModule(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& moduleObj)
180 {
181 moduleObj->SetProperty(runtime, APP_GET_INFO, runtime->NewFunction(AppGetInfo));
182 moduleObj->SetProperty(runtime, APP_TERMINATE, runtime->NewFunction(AppTerminate));
183 moduleObj->SetProperty(runtime, APP_SET_IMAGE_CACHE_COUNT, runtime->NewFunction(AppSetImageCacheCount));
184 moduleObj->SetProperty(
185 runtime, APP_SET_IMAGE_RAWDATA_CACHE_SIZE, runtime->NewFunction(AppSetImageRawDataCacheSize));
186 moduleObj->SetProperty(runtime, APP_SET_IMAGE_FILE_CACHE_SIZE, runtime->NewFunction(AppSetImageFileCacheSize));
187
188 moduleObj->SetProperty(runtime, APP_REQUEST_FULL_WINDOW, runtime->NewFunction(AppRequestFullWindow));
189 moduleObj->SetProperty(runtime, APP_SCREEN_ON_VISIBLE, runtime->NewFunction(AppScreenOnVisible));
190 }
191
192 } // namespace OHOS::Ace::Framework