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 "base/log/log.h"
19 #include "bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
20 #include "bridge/js_frontend/engine/common/js_constants.h"
21 #include "core/common/container.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 LOGE("get jsi engine instance failed");
31 return runtime->NewNull();
32 }
33 auto delegate = instance->GetDelegate();
34 if (!delegate) {
35 LOGE("get frontend delegate failed");
36 return runtime->NewNull();
37 }
38
39 shared_ptr<JsValue> appInfo = runtime->NewObject();
40 auto appId = delegate->GetAppID();
41 auto appName = delegate->GetAppName();
42 auto versionName = delegate->GetVersionName();
43 auto versionCode = delegate->GetVersionCode();
44 appInfo->SetProperty(runtime, "appID", runtime->NewString(appId));
45 appInfo->SetProperty(runtime, "appName", runtime->NewString(appName));
46 appInfo->SetProperty(runtime, "versionName", runtime->NewString(versionName));
47 appInfo->SetProperty(runtime, "versionCode", runtime->NewNumber(versionCode));
48 return appInfo;
49 }
50
AppTerminate(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)51 shared_ptr<JsValue> AppTerminate(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
52 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
53 {
54 auto instance = static_cast<JsiDeclarativeEngineInstance*>(runtime->GetEmbedderData());
55 if (instance == nullptr) {
56 LOGE("get jsi engine instance failed");
57 return runtime->NewNull();
58 }
59 auto delegate = instance->GetDelegate();
60 if (!delegate) {
61 LOGE("get frontend delegate failed");
62 return runtime->NewNull();
63 }
64 auto pipelineContext = delegate->GetPipelineContext();
65 if (!pipelineContext) {
66 LOGE("get frontend pipelineContext failed");
67 return runtime->NewNull();
68 }
69 auto uiTaskExecutor = delegate->GetUiTask();
70 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
71 uiTaskExecutor.PostTask([pipelineContextWeak]() mutable {
72 auto pipelineContext = pipelineContextWeak.Upgrade();
73 if (pipelineContext) {
74 pipelineContext->Finish();
75 }
76 });
77 return runtime->NewNull();
78 }
79
AppSetImageCacheCount(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)80 shared_ptr<JsValue> AppSetImageCacheCount(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
81 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
82 {
83 auto container = Container::Current();
84 if (!container) {
85 LOGE("current container is null");
86 return runtime->NewNull();
87 }
88 auto pipelineContext = container->GetPipelineContext();
89 if (!pipelineContext) {
90 LOGE("get pipelineContext failed");
91 return runtime->NewNull();
92 }
93 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
94 LOGE("The arguments must be one int number.");
95 return runtime->NewNull();
96 }
97 int32_t size = argv[0]->ToInt32(runtime);
98 if (size <= 0) {
99 LOGE("size: %{public}d less than zero is invalid for cache image", size);
100 return runtime->NewNull();
101 }
102 auto taskExecutor = pipelineContext->GetTaskExecutor();
103 if (!taskExecutor) {
104 LOGE("taskExecutor is null.");
105 return runtime->NewNull();
106 }
107 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
108 taskExecutor->PostTask([ pipelineContextWeak, size ]() mutable {
109 auto pipelineContext = pipelineContextWeak.Upgrade();
110 if (pipelineContext) {
111 auto imageCache = pipelineContext->GetImageCache();
112 if (imageCache) {
113 imageCache->SetCapacity(size);
114 } else {
115 LOGW("image cache is null");
116 }
117 }
118 }, TaskExecutor::TaskType::UI);
119 return runtime->NewNull();
120 }
121
AppSetImageRawDataCacheSize(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)122 shared_ptr<JsValue> AppSetImageRawDataCacheSize(
123 const shared_ptr<JsRuntime>& runtime,
124 const shared_ptr<JsValue>& thisObj,
125 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
126 {
127 auto container = Container::Current();
128 if (!container) {
129 LOGE("current container is null");
130 return runtime->NewNull();
131 }
132 auto pipelineContext = container->GetPipelineContext();
133 if (!pipelineContext) {
134 LOGE("get pipelineContext failed");
135 return runtime->NewNull();
136 }
137 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
138 LOGE("The arguments must be one int number.");
139 return runtime->NewNull();
140 }
141 int32_t size = argv[0]->ToInt32(runtime);
142 if (size <= 0) {
143 LOGE("size: %{public}d less than zero is invalid for cache image raw data", size);
144 return runtime->NewNull();
145 }
146 auto taskExecutor = pipelineContext->GetTaskExecutor();
147 if (!taskExecutor) {
148 LOGE("taskExecutor is null.");
149 return runtime->NewNull();
150 }
151 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
152 taskExecutor->PostTask([ pipelineContextWeak, size ]() mutable {
153 auto pipelineContext = pipelineContextWeak.Upgrade();
154 if (pipelineContext) {
155 auto imageCache = pipelineContext->GetImageCache();
156 if (imageCache) {
157 imageCache->SetDataCacheLimit(size);
158 } else {
159 LOGW("image cache is null");
160 }
161 }
162 }, TaskExecutor::TaskType::UI);
163 return runtime->NewNull();
164 }
165
AppSetImageFileCacheSize(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)166 shared_ptr<JsValue> AppSetImageFileCacheSize(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
167 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
168 {
169 auto container = Container::Current();
170 if (!container) {
171 LOGE("current container is null");
172 return runtime->NewNull();
173 }
174 auto pipelineContext = container->GetPipelineContext();
175 if (!pipelineContext) {
176 LOGE("get pipelineContext failed");
177 return runtime->NewNull();
178 }
179 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
180 LOGE("The arguments must be one int number.");
181 return runtime->NewNull();
182 }
183 int32_t size = argv[0]->ToInt32(runtime);
184 if (size <= 0) {
185 LOGE("size: %{public}d less than zero is invalid for cache image files", size);
186 return runtime->NewNull();
187 }
188 auto taskExecutor = pipelineContext->GetTaskExecutor();
189 if (!taskExecutor) {
190 LOGE("taskExecutor is null.");
191 return runtime->NewNull();
192 }
193 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
194 taskExecutor->PostTask([ pipelineContextWeak, size ]() mutable {
195 auto pipelineContext = pipelineContextWeak.Upgrade();
196 if (pipelineContext) {
197 auto imageCache = pipelineContext->GetImageCache();
198 if (imageCache) {
199 imageCache->SetCacheFileLimit(size);
200 } else {
201 LOGW("image cache is null");
202 }
203 }
204 }, TaskExecutor::TaskType::UI);
205 return runtime->NewNull();
206 }
207
AppRequestFullWindow(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)208 shared_ptr<JsValue> AppRequestFullWindow(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
209 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
210 {
211 return runtime->NewNull();
212 }
213
AppScreenOnVisible(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)214 shared_ptr<JsValue> AppScreenOnVisible(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
215 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
216 {
217 return runtime->NewNull();
218 }
219
InitAppModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & moduleObj)220 void InitAppModule(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& moduleObj)
221 {
222 moduleObj->SetProperty(runtime, APP_GET_INFO, runtime->NewFunction(AppGetInfo));
223 moduleObj->SetProperty(runtime, APP_TERMINATE, runtime->NewFunction(AppTerminate));
224 moduleObj->SetProperty(runtime, APP_SET_IMAGE_CACHE_COUNT, runtime->NewFunction(AppSetImageCacheCount));
225 moduleObj->SetProperty(
226 runtime, APP_SET_IMAGE_RAWDATA_CACHE_SIZE, runtime->NewFunction(AppSetImageRawDataCacheSize));
227 moduleObj->SetProperty(runtime, APP_SET_IMAGE_FILE_CACHE_SIZE, runtime->NewFunction(AppSetImageFileCacheSize));
228
229 moduleObj->SetProperty(runtime, APP_REQUEST_FULL_WINDOW, runtime->NewFunction(AppRequestFullWindow));
230 moduleObj->SetProperty(runtime, APP_SCREEN_ON_VISIBLE, runtime->NewFunction(AppScreenOnVisible));
231 }
232
233 } // namespace OHOS::Ace::Framework