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 "frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_timer_module.h"
17
18 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
19 #include "frameworks/bridge/js_frontend/engine/common/js_constants.h"
20 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
21
22 namespace OHOS::Ace::Framework {
23
24 namespace {
25
SetTimeoutOrInterval(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc,bool isInterval)26 shared_ptr<JsValue> SetTimeoutOrInterval(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
27 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc, bool isInterval)
28 {
29 auto instance = static_cast<JsiDeclarativeEngineInstance*>(runtime->GetEmbedderData());
30 if (instance == nullptr) {
31 return runtime->NewNull();
32 }
33 auto delegate = instance->GetDelegate();
34 if (!delegate) {
35 return runtime->NewNull();
36 }
37
38 if (argc < 1) {
39 return runtime->NewNull();
40 }
41 if (!argv[0]->IsFunction(runtime)) {
42 return runtime->NewNull();
43 }
44 uint32_t delay = 0;
45 std::vector<shared_ptr<JsValue>> callBackParams;
46 if (argc < 2 || !argv[1]->IsNumber(runtime)) {
47 uint32_t callbackId = JsiTimerModule::GetInstance()->AddCallBack(argv[0], callBackParams);
48 delegate->WaitTimer(std::to_string(callbackId), std::to_string(delay), isInterval, true);
49 return runtime->NewInt32(callbackId);
50 }
51 delay = static_cast<uint32_t>(argv[1]->ToInt32(runtime));
52
53 for (int i = 2; i < argc; ++i) {
54 callBackParams.emplace_back(argv[i]);
55 }
56 uint32_t callbackId = JsiTimerModule::GetInstance()->AddCallBack(argv[0], callBackParams);
57 delegate->WaitTimer(std::to_string(callbackId), std::to_string(delay), isInterval, true);
58 return runtime->NewInt32(callbackId);
59 }
60
ClearTimeoutOrInterval(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)61 void ClearTimeoutOrInterval(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
62 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
63 {
64 if (argc < 1) {
65 return;
66 }
67 if (!argv[0]->IsNumber(runtime)) {
68 return;
69 }
70
71 auto instance = static_cast<JsiDeclarativeEngineInstance*>(runtime->GetEmbedderData());
72 if (instance == nullptr) {
73 return;
74 }
75 auto delegate = instance->GetDelegate();
76 if (!delegate) {
77 return;
78 }
79
80 uint32_t callbackId = static_cast<uint32_t>(argv[0]->ToInt32(runtime));
81 JsiTimerModule::GetInstance()->RemoveCallBack(callbackId);
82 delegate->ClearTimer(std::to_string(callbackId));
83 }
84
85 }
86
SetTimeout(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)87 shared_ptr<JsValue> SetTimeout(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
88 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
89 {
90 return SetTimeoutOrInterval(runtime, thisObj, argv, argc, false);
91 }
92
SetInterval(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)93 shared_ptr<JsValue> SetInterval(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
94 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
95 {
96 return SetTimeoutOrInterval(runtime, thisObj, argv, argc, true);
97 }
98
ClearTimeout(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)99 shared_ptr<JsValue> ClearTimeout(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
100 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
101 {
102 ClearTimeoutOrInterval(runtime, thisObj, argv, argc);
103 return runtime->NewNull();
104 }
105
ClearInterval(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)106 shared_ptr<JsValue> ClearInterval(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
107 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
108 {
109 ClearTimeoutOrInterval(runtime, thisObj, argv, argc);
110 return runtime->NewNull();
111 }
112
GetInstance()113 JsiTimerModule* JsiTimerModule::GetInstance()
114 {
115 static JsiTimerModule instance;
116 return &instance;
117 }
118
AddCallBack(const shared_ptr<JsValue> & func,const std::vector<shared_ptr<JsValue>> & params)119 uint32_t JsiTimerModule::AddCallBack(const shared_ptr<JsValue>& func, const std::vector<shared_ptr<JsValue>>& params)
120 {
121 std::lock_guard<std::mutex> lock(moduleMutex_);
122 ++callBackId_;
123 callBackFuncMap_[callBackId_] = func;
124 callBackParamsMap_[callBackId_] = params;
125 return callBackId_;
126 }
127
RemoveCallBack(uint32_t callBackId)128 void JsiTimerModule::RemoveCallBack(uint32_t callBackId)
129 {
130 std::lock_guard<std::mutex> lock(moduleMutex_);
131 if (callBackFuncMap_.find(callBackId) != callBackFuncMap_.end()) {
132 callBackFuncMap_.erase(callBackId);
133 }
134 if (callBackParamsMap_.find(callBackId) != callBackParamsMap_.end()) {
135 callBackParamsMap_.erase(callBackId);
136 }
137 }
138
GetCallBack(uint32_t callBackId,shared_ptr<JsValue> & func,std::vector<shared_ptr<JsValue>> & params)139 bool JsiTimerModule::GetCallBack(uint32_t callBackId, shared_ptr<JsValue>& func,
140 std::vector<shared_ptr<JsValue>>& params)
141 {
142 std::lock_guard<std::mutex> lock(moduleMutex_);
143 auto iterFunc = callBackFuncMap_.find(callBackId);
144 auto iterParams = callBackParamsMap_.find(callBackId);
145 if (iterFunc == callBackFuncMap_.end()) {
146 return false;
147 }
148 if (iterParams == callBackParamsMap_.end()) {
149 return false;
150 }
151 func = iterFunc->second;
152 params = iterParams->second;
153 return true;
154 }
155
InitTimerModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & moduleObj)156 void JsiTimerModule::InitTimerModule(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& moduleObj)
157 {
158 moduleObj->SetProperty(runtime, SET_TIMEOUT, runtime->NewFunction(SetTimeout));
159 moduleObj->SetProperty(runtime, SET_INTERVAL, runtime->NewFunction(SetInterval));
160 moduleObj->SetProperty(runtime, CLEAR_TIMEOUT, runtime->NewFunction(ClearTimeout));
161 moduleObj->SetProperty(runtime, CLEAR_INTERVAL, runtime->NewFunction(ClearInterval));
162 }
163
164 } // namespace OHOS::Ace::Framework