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