• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/log/log.h"
19 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
20 #include "frameworks/bridge/js_frontend/engine/common/js_constants.h"
21 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
22 
23 namespace OHOS::Ace::Framework {
GetInstance()24 JsiSyscapModule* JsiSyscapModule::GetInstance()
25 {
26     static JsiSyscapModule instance;
27     return &instance;
28 }
29 
AddCallBack(const shared_ptr<JsValue> & func,const std::vector<shared_ptr<JsValue>> & params)30 uint32_t JsiSyscapModule::AddCallBack(const shared_ptr<JsValue>& func, const std::vector<shared_ptr<JsValue>>& params)
31 {
32     ++callBackId_;
33     callBackFuncMap_[callBackId_] = func;
34     callBackParamsMap_[callBackId_] = params;
35     return callBackId_;
36 }
37 
RemoveCallBack(uint32_t callBackId)38 void JsiSyscapModule::RemoveCallBack(uint32_t callBackId)
39 {
40     if (callBackFuncMap_.find(callBackId) != callBackFuncMap_.end()) {
41         callBackFuncMap_.erase(callBackId);
42     }
43     if (callBackParamsMap_.find(callBackId) != callBackParamsMap_.end()) {
44         callBackParamsMap_.erase(callBackId);
45     }
46 }
47 
CanIUse(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)48 shared_ptr<JsValue> CanIUse(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
49     const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
50 {
51     if (argc != 1) {
52         LOGE("argc should be 1");
53         return runtime->NewNull();
54     }
55     if (!argv[0]->IsString(runtime)) {
56         LOGW("argv[0] is not IsString");
57         return runtime->NewNull();
58     }
59 
60     std::string syscapString = argv[0]->ToString(runtime);
61 
62     bool ret = Ace::SystemProperties::IsSyscapExist(syscapString.c_str());
63     return runtime->NewBoolean(ret);
64 }
65 
GetCallBack(uint32_t callBackId,shared_ptr<JsValue> & func,std::vector<shared_ptr<JsValue>> & params)66 bool JsiSyscapModule::GetCallBack(uint32_t callBackId, shared_ptr<JsValue>& func,
67     std::vector<shared_ptr<JsValue>>& params)
68 {
69     auto iterFunc = callBackFuncMap_.find(callBackId);
70     auto iterParams = callBackParamsMap_.find(callBackId);
71     if (iterFunc == callBackFuncMap_.end()) {
72         LOGE("find callback function failed, callbackId = %{public}u", callBackId);
73         return false;
74     }
75     if (iterParams == callBackParamsMap_.end()) {
76         LOGE("find callback parameters failed, callbackId = %{public}u", callBackId);
77         return false;
78     }
79     func = iterFunc->second;
80     params = iterParams->second;
81     return true;
82 }
83 
InitSyscapModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & moduleObj)84 void JsiSyscapModule::InitSyscapModule(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& moduleObj)
85 {
86     moduleObj->SetProperty(runtime, CAN_IUSE, runtime->NewFunction(CanIUse));
87 }
88 } // namespace OHOS::Ace::Framework