1 /*
2 * Copyright (c) 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_syscap_module.h"
17
18 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
19
20 namespace OHOS::Ace::Framework {
GetInstance()21 JsiSyscapModule* JsiSyscapModule::GetInstance()
22 {
23 static JsiSyscapModule instance;
24 return &instance;
25 }
26
AddCallBack(const shared_ptr<JsValue> & func,const std::vector<shared_ptr<JsValue>> & params)27 uint32_t JsiSyscapModule::AddCallBack(const shared_ptr<JsValue>& func, const std::vector<shared_ptr<JsValue>>& params)
28 {
29 ++callBackId_;
30 callBackFuncMap_[callBackId_] = func;
31 callBackParamsMap_[callBackId_] = params;
32 return callBackId_;
33 }
34
RemoveCallBack(uint32_t callBackId)35 void JsiSyscapModule::RemoveCallBack(uint32_t callBackId)
36 {
37 if (callBackFuncMap_.find(callBackId) != callBackFuncMap_.end()) {
38 callBackFuncMap_.erase(callBackId);
39 }
40 if (callBackParamsMap_.find(callBackId) != callBackParamsMap_.end()) {
41 callBackParamsMap_.erase(callBackId);
42 }
43 }
44
CanIUse(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)45 shared_ptr<JsValue> CanIUse(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
46 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
47 {
48 if (argc != 1) {
49 return runtime->NewNull();
50 }
51 if (!argv[0]->IsString(runtime)) {
52 return runtime->NewNull();
53 }
54
55 std::string syscapString = argv[0]->ToString(runtime);
56
57 bool ret = Ace::SystemProperties::IsSyscapExist(syscapString.c_str());
58 return runtime->NewBoolean(ret);
59 }
60
GetCallBack(uint32_t callBackId,shared_ptr<JsValue> & func,std::vector<shared_ptr<JsValue>> & params)61 bool JsiSyscapModule::GetCallBack(uint32_t callBackId, shared_ptr<JsValue>& func,
62 std::vector<shared_ptr<JsValue>>& params)
63 {
64 auto iterFunc = callBackFuncMap_.find(callBackId);
65 auto iterParams = callBackParamsMap_.find(callBackId);
66 if (iterFunc == callBackFuncMap_.end()) {
67 return false;
68 }
69 if (iterParams == callBackParamsMap_.end()) {
70 return false;
71 }
72 func = iterFunc->second;
73 params = iterParams->second;
74 return true;
75 }
76
InitSyscapModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & moduleObj)77 void JsiSyscapModule::InitSyscapModule(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& moduleObj)
78 {
79 moduleObj->SetProperty(runtime, CAN_IUSE, runtime->NewFunction(CanIUse));
80 }
81 } // namespace OHOS::Ace::Framework