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([pipelineContextWeak]() mutable {
67 auto pipelineContext = pipelineContextWeak.Upgrade();
68 if (pipelineContext) {
69 pipelineContext->Finish();
70 }
71 });
72 return runtime->NewNull();
73 }
74
AppSetImageCacheCount(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)75 shared_ptr<JsValue> AppSetImageCacheCount(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
76 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
77 {
78 auto container = Container::Current();
79 if (!container) {
80 return runtime->NewNull();
81 }
82 auto pipelineContext = container->GetPipelineContext();
83 if (!pipelineContext) {
84 return runtime->NewNull();
85 }
86 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
87 return runtime->NewNull();
88 }
89 int32_t size = argv[0]->ToInt32(runtime);
90 if (size < 0) {
91 return runtime->NewNull();
92 }
93 auto taskExecutor = pipelineContext->GetTaskExecutor();
94 if (!taskExecutor) {
95 return runtime->NewNull();
96 }
97 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
98 taskExecutor->PostTask([ pipelineContextWeak, size ]() mutable {
99 auto pipelineContext = pipelineContextWeak.Upgrade();
100 if (pipelineContext) {
101 auto imageCache = pipelineContext->GetImageCache();
102 if (imageCache) {
103 imageCache->SetCapacity(size);
104 }
105 }
106 }, TaskExecutor::TaskType::UI);
107 return runtime->NewNull();
108 }
109
AppSetImageRawDataCacheSize(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)110 shared_ptr<JsValue> AppSetImageRawDataCacheSize(
111 const shared_ptr<JsRuntime>& runtime,
112 const shared_ptr<JsValue>& thisObj,
113 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
114 {
115 auto container = Container::Current();
116 if (!container) {
117 return runtime->NewNull();
118 }
119 auto pipelineContext = container->GetPipelineContext();
120 if (!pipelineContext) {
121 return runtime->NewNull();
122 }
123 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
124 return runtime->NewNull();
125 }
126 int32_t size = argv[0]->ToInt32(runtime);
127 if (size < 0) {
128 return runtime->NewNull();
129 }
130 auto taskExecutor = pipelineContext->GetTaskExecutor();
131 if (!taskExecutor) {
132 return runtime->NewNull();
133 }
134 WeakPtr<PipelineBase> pipelineContextWeak(pipelineContext);
135 taskExecutor->PostTask([ pipelineContextWeak, size ]() mutable {
136 auto pipelineContext = pipelineContextWeak.Upgrade();
137 if (pipelineContext) {
138 auto imageCache = pipelineContext->GetImageCache();
139 if (imageCache) {
140 imageCache->SetDataCacheLimit(size);
141 }
142 }
143 }, TaskExecutor::TaskType::UI);
144 return runtime->NewNull();
145 }
146
AppSetImageFileCacheSize(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)147 shared_ptr<JsValue> AppSetImageFileCacheSize(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
148 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
149 {
150 if (argc != 1 || !argv[0]->IsNumber(runtime)) {
151 return runtime->NewNull();
152 }
153 int32_t size = argv[0]->ToInt32(runtime);
154 if (size < 0) {
155 return runtime->NewNull();
156 }
157 ImageFileCache::GetInstance().SetCacheFileLimit(size);
158 return runtime->NewNull();
159 }
160
AppRequestFullWindow(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)161 shared_ptr<JsValue> AppRequestFullWindow(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
162 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
163 {
164 return runtime->NewNull();
165 }
166
AppScreenOnVisible(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)167 shared_ptr<JsValue> AppScreenOnVisible(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
InitAppModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & moduleObj)173 void InitAppModule(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& moduleObj)
174 {
175 moduleObj->SetProperty(runtime, APP_GET_INFO, runtime->NewFunction(AppGetInfo));
176 moduleObj->SetProperty(runtime, APP_TERMINATE, runtime->NewFunction(AppTerminate));
177 moduleObj->SetProperty(runtime, APP_SET_IMAGE_CACHE_COUNT, runtime->NewFunction(AppSetImageCacheCount));
178 moduleObj->SetProperty(
179 runtime, APP_SET_IMAGE_RAWDATA_CACHE_SIZE, runtime->NewFunction(AppSetImageRawDataCacheSize));
180 moduleObj->SetProperty(runtime, APP_SET_IMAGE_FILE_CACHE_SIZE, runtime->NewFunction(AppSetImageFileCacheSize));
181
182 moduleObj->SetProperty(runtime, APP_REQUEST_FULL_WINDOW, runtime->NewFunction(AppRequestFullWindow));
183 moduleObj->SetProperty(runtime, APP_SCREEN_ON_VISIBLE, runtime->NewFunction(AppScreenOnVisible));
184 }
185
186 } // namespace OHOS::Ace::Framework